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

时间:2011-11-23 02:15:17

标签: php .htaccess redirect url-rewriting http-status-code-301

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

我想知道.htaccess

的正确301重定向规则应该是什么

其他信息:

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

以下是我想将OLD动态网址重定向到各自的静态网址的情况。

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

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

...谢谢

1 个答案:

答案 0 :(得分:1)

请尝试填写重写地图文件(请参阅here)以与网址目标进行对应。

创建一个mapfile,您可以在其中放置所需的所有类别:

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]

这应该有效。 如果它没有尝试在第一个 RewriteRule指令中将%1更改为%2。 如果它仍然没有,你可能有足够的线索来完成这项工作。 如果你没有足够的线索......

两个提示:


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

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

我最喜欢检查regexp的工具:

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

相关问题