Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 修改重定向和重写(第2部分)_Apache_Mod Rewrite_Url Rewriting - Fatal编程技术网

Apache 修改重定向和重写(第2部分)

Apache 修改重定向和重写(第2部分),apache,mod-rewrite,url-rewriting,Apache,Mod Rewrite,Url Rewriting,除了我前面的问题,我把自己弄得一团糟,所以我会尽量把我的问题摆在这里 我正在整理我的URL结构,但有一个问题,我的URL在搜索引擎中索引良好,所以我需要重写我的URL,同时也重定向它们 在前面一个问题的帮助下,我目前在.htaccess中有以下内容 Options +FollowSymlinks RewriteEngine on rewritecond %{http_host} ^mydomain.com [nc] rewriterule ^(.*)$ http://www.mydomain.

除了我前面的问题,我把自己弄得一团糟,所以我会尽量把我的问题摆在这里

我正在整理我的URL结构,但有一个问题,我的URL在搜索引擎中索引良好,所以我需要重写我的URL,同时也重定向它们

在前面一个问题的帮助下,我目前在.htaccess中有以下内容

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]

# Rewrite A newly added section rewrite ( no redirect required )
RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]

# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]
所以,这一切都如预期的那样工作,但我有另一个url,以index.php结尾,有时但并不总是有php变量,我还需要重写和重定向,通过尝试自己这样做,我创建了某种冲突,它没有按预期工作

下面是我需要用301重写和重定向的URL

http://www.mydomain.com/news/dentistry_dental/index.php?month=February&year=2011

With the previous index.php rule it currently displays as

http://www.mydomain.com/news/dentistry_dental/?month=February&year=2011

Half way there i guess... I need it to be the following

http://www.mydomain.com/dental_news/February/2011
请记住,url有时没有如下所示的变量

http://www.mydomain.com/news/dentistry_dental/index.php

Which is currently with the index.php rule above is showing as

http://www.mydomain.com/news/dentistry_dental/

This needs to be

http://www.mydomain.com/dental_news/
是的,我对这些东西一窍不通,它对我来说是全新的,所以如果我能把它整理好,我会成为一只非常快乐的兔子

谢谢大家

编辑

好的,我当前的.htaccess如下所示

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]

RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]

# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]

RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
RewriteRule ^dental_news/$ /news/dentistry_dental/index.php [L]
Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^dentalsupportuk.com [nc]
rewriterule ^(.*)$ http://www.dentalsupportuk.com/$1 [r=301,nc]

# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.dentalsupportuk.com/$1 [R=301]

RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]

# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]

RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)$ news/dentistry_dental/index.php?month=$1&year=$2&redirect [L]
鉴于此,出现了以下情况:

www.mydomain.com/news/dentistry_dental/index.php 
-> www.mydomain.com/dental_news/ 
^正确

www.mydomain.com/news/dentistry_dental/index.php?month=May&year=2011 
-> www.mydomain.com/dental_news/April/2011 
^错误:在url栏中显示正确的路径,但显示带有变量的article_detail.php而不是index.php

我猜其中两条规则是相似的,一条是使用年和月变量提取index.php并发送到article_详细信息页面。我可能是错的,但是我不知道如何修复

编辑2

当前的.htaccess如下所示

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]

RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]

# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]

RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
RewriteRule ^dental_news/$ /news/dentistry_dental/index.php [L]
Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^dentalsupportuk.com [nc]
rewriterule ^(.*)$ http://www.dentalsupportuk.com/$1 [r=301,nc]

# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.dentalsupportuk.com/$1 [R=301]

RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]

# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]

RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)$ news/dentistry_dental/index.php?month=$1&year=$2&redirect [L]
www.mydomain.com/dental_news/现在正在重写为www.mydomain.com/dental_news/,但它不存在,因此404错误应该重写为www.mydomain.com/news/dentistry_dental/

www.mydomain.com/dental_news/100/some-title正在重写,正如预期的那样,在变量起作用的情况下,将其改写为www.mydomain.com/news/dentistry_dental/article_detail.php

www.mydomain.com/news/dentistry_dental/article_detail.php?article=100&title=某些标题正在按预期重定向到www.mydomain.com/dential_news/100/some-title并正确显示

www.mydomain.com/news/dentistry_dental/index.php?month=May&year=2010正在重定向,但Firefox崩溃,表示Firefox检测到服务器正在以一种永远无法完成的方式重定向对此地址的请求。不工作,这是某种循环导致的吗

www.mydomain.com/dental_news/July/2010/重写给了我一个404错误

所以,是的,我到处都是,也许我把顺序搞错了什么的,但是我越乱,我就越害怕破坏一切。有什么想法吗

问候
M

尝试添加以下两条规则:

RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]

RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
我认为这不会引起任何冲突,似乎涵盖了您描述的两种情况

编辑

好的,如果我没有弄错,那么/dential_news/2968/redefiniting口腔卫生干预的URL应该用适当的查询字符串重写为/news/dentialstry_dential/article_detail.php。然后,/dential_news/April/2011应该重写为/news/dentialstry_dential/index.php。因此…如果是这样,您应该能够添加以下规则:

RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)$ news/dentistry_dental/index.php?month=$1&year=$2&redirect [L]
进行以下更新也可能有意义:

当前:重写规则^dental_news/[^/]*/[^/]*$news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect[L]

更新:RewriteRule^ dental_news/[0-9]*/[^/]*$news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect[L]

不同之处在于它更具体,第一个regex组将只匹配一组数字,并且将有助于避免与重写到index.php文件的规则冲突

编辑2 我相信我已经测试了您提供的所有URL,它们现在似乎都正常工作了。有几个我们没有考虑的重定向循环。希望这将有助于

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]

RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]

RewriteRule dental_news/$ /news/dentistry_dental/?rewrite [L]

# Rewrite dental news article to neat nice url
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite 
RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&rewrite [L]

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]

RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [R=301]


# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite 
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]

# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite 
RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)/?$ news/dentistry_dental/index.php?month=$1&year=$2&rewrite [L]

希望这能有所帮助。

@clmarkurt嗨,再次感谢您的投入。我已经添加了规则,你建议,当点击新链接时,它会重定向到www.mydomain.com/dental_news/但是它给出了一个404,apache日志说它实际上在寻找文件,而不仅仅是重写真实地址?没问题。您将需要添加另一个重写,将新URL指向实际文件,可能类似于:RewriteRule^dental_news/$/news/dentistry_dental/index.php[L]@clmarkurt从财务上讲,已经取得了一些进展,但仍然存在一些问题,很抱歉,我已经在底部编辑了上面的问题,也许您可以看到问题。谢谢M@clmarquart是的,你知道我需要什么了,但我仍然在得到循环错误,我现在将编辑原始问题,这样你就可以看到了。可能是我的错,毫无疑问,我做错了什么。@clmarkurt几乎没睡过觉后,我仍然有同样的问题,我就是想不出来:上面的问题中的编辑2解释道。