301 从子文件夹中的动态 URL 重定向到新的静态 URL

发布于 2024-12-17 10:47:52 字数 636 浏览 0 评论 0原文

我查看了将动态 URL 重定向到静态 URL 的所有不同解决方案,但找不到与我的问题相关的任何案例。

我想知道 .htaccess 的正确 301 重定向规则应该是什么

附加信息:

  • 启动新网站后旧 URL 已被删除。
  • 旧 URL 位于子文件夹 (/desarrollo/mantenedores/) 中,
  • “art_id”是字符串,“61”是文章

下面是我想要将旧动态 URL 重定向到其各自的静态 URL 的情况。

旧网址 = http://example.com/desarrollo/mantenedores/art_indice.asp? art_id=61

新网址 = http://example.com/comunidad/articles/2/tipos-de-sociedades

谢谢...

I reviewed all the different solutions for redirecting dynamic URLs to a static URL but I couldn't find any case related with my question.

I would like to know what should be the correct 301 redirect rule for .htaccess

Additional info:

  • The old URLs were deleted after launching the new site.
  • The Old URLs are located in a subfolder (/desarrollo/mantenedores/)
  • "art_id" is the string and the "61" is the article

Here is the case I want to redirect OLD Dynamic URLs to their individual static URLs.

Old URL = http://example.com/desarrollo/mantenedores/art_indice.asp?art_id=61

New URL = http://example.com/comunidad/articles/2/tipos-de-sociedades

Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2024-12-24 10:47:52

请尝试填写重写映射文件(请参见此处)与 URL 目标的对应关系。

创建一个映射文件,在其中放置所需的所有类别:

RewriteMap mapoldtonew \
  dbm:/web/htdocs/yoursite/rewriterules/mapoldtonew.map

关于您的示例,您的映射文件可能会填充以下内容:

art_id=61        articles/2/tipos-de-sociedades
...

然后这里是最困难的部分:

RewriteCond %{REQUEST_URI} desarrollo/(.*)
RewriteCond %{QUERY_STRING} (([^=]+)=([0-9]+))
# The following rule doesn't touch the URL, but 
# will try to search into the map file and
# create an OLDTONEW environment variable with the string found
# and if not found, assign OLDTONEW to "notfound"
RewriteRule . - [QSA,E=OLDTONEW:${mapoldtonew:%1|notfound}]

# if the OLDTONEW is not empty and is not found:
RewriteCond %{ENV:OLDTONEW} notfound
# this should never happen => 404:
RewriteRule . - [R=404,L]

# Reach here means :
# - OLDTONEW is empty
# - OLDTONEW is was found
# => if OLDTONEW is not empty:
RewriteCond %{ENV:OLDTONEW} !^$
# make the redirection to the corresponding found:
RewriteRule . /%{ENV:CATEGORYREVERSE} [QSA,L]

这应该可行。
如果它不尝试将 first RewriteRule 指令中的 %1 更改为 %2
如果仍然没有,您可能有足够的线索来完成任务。
如果您没有足够的线索...

两个提示:


请尝试使用 RewriteLog 指令:它可以帮助您追踪此类问题:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

我最喜欢的检查正则表达式的工具:

http://www.quanetic.com/Regex (不要忘记选择 ereg(POSIX) 而不是 preg(PCRE )!)

Please try to fill a rewritemap file (see here) to make a correspondance with the URL destination.

Create a mapfile where you put all the categories you need:

RewriteMap mapoldtonew \
  dbm:/web/htdocs/yoursite/rewriterules/mapoldtonew.map

Regarding your sample, your map file may be filled with things like:

art_id=61        articles/2/tipos-de-sociedades
...

Then here you go for the hard part:

RewriteCond %{REQUEST_URI} desarrollo/(.*)
RewriteCond %{QUERY_STRING} (([^=]+)=([0-9]+))
# The following rule doesn't touch the URL, but 
# will try to search into the map file and
# create an OLDTONEW environment variable with the string found
# and if not found, assign OLDTONEW to "notfound"
RewriteRule . - [QSA,E=OLDTONEW:${mapoldtonew:%1|notfound}]

# if the OLDTONEW is not empty and is not found:
RewriteCond %{ENV:OLDTONEW} notfound
# this should never happen => 404:
RewriteRule . - [R=404,L]

# Reach here means :
# - OLDTONEW is empty
# - OLDTONEW is was found
# => if OLDTONEW is not empty:
RewriteCond %{ENV:OLDTONEW} !^$
# make the redirection to the corresponding found:
RewriteRule . /%{ENV:CATEGORYREVERSE} [QSA,L]

This should work.
If it doesn't try to change %1 to %2 in the first RewriteRule directive.
If it still doesn't you may have enough clues to finish the job.
If you don't have enough clues...

Two hints:


Please try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文