什么是此重定向问题的.htaccess / mod_rewrite解决方案?

时间:2010-09-08 18:20:16

标签: .htaccess mod-rewrite

请参阅问题底部附近编辑的重要补充信息

我正在尝试获取以下网址

http://www.example.co.uk/home/templates/cnb/fb_calendar/events/view/289

重定向到

http://www.example.co.uk/home/index.php?option=com_content&task=view&id=574&Itemid=85&direct=view:289

我认为这会起作用

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^home/templates/cnb/fb_calendar/events/view/([^/\.]+)?$ http://www.example.co.uk/home/index.php?option=com_content&task=view&id=574&Itemid=85&direct=view:$1 [R=302,L]

但事实并非如此。该规则根本没有“参与” - 原始网址不变。我做错了什么?

我在下面包含了整个.htaccess文件:我正在谈论的规则是第四个。 (该文件主要由cpanel实用程序生成,而不是手工生成。)

RewriteEngine on

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^approve/([^/\.]+)/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=574\&Itemid\=85\&approve\=$1" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^disapprove/([^/\.]+)/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=574\&Itemid\=85\&disapprove\=$1" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^direct/([^/\.]+)/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=574\&Itemid\=85\&direct\=$1" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^home/templates/cnb/fb_calendar/events/view/([^/\.]+)?$ http://www.example.co.uk/home/index.php?option=com_content&task=view&id=574&Itemid=85&direct=view:$1 [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^uk/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=6\&Itemid\=7" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^inspirational/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=7\&Itemid\=8" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^gospel/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=1\&Itemid\=3" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^bible/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=1\&Itemid\=2" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^dab/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=14\&Itemid\=35" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/www\.example\.co\.uk\/home" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^abi/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=20\&Itemid\=37" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^anne/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=16\&Itemid\=55" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^ben/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=25\&Itemid\=56" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^catherine/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=22\&Itemid\=57" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^dave/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=26\&Itemid\=53" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^jan/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=21\&Itemid\=58" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^nick/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=23\&Itemid\=60" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^paul/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=18\&Itemid\=59" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^robbie/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=17\&Itemid\=61" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^roy/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=15\&Itemid\=54" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^ruth/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=19\&Itemid\=62" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^steve/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=14\&Itemid\=64" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^trevor/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=24\&Itemid\=63" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^jobs/?$ "http\:\/\/www\.ucb\.co\.uk\/vacancies" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^prayer/?$ "http\:\/\/www\.ucb\.co\.uk\/prayer" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^yousay/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=11\&Itemid\=31" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^christmasappeal/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=12\&Itemid\=32" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^luke/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=29\&Itemid\=68" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^juls/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=blogcategory\&id\=30\&Itemid\=71" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^tv/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=5\&Itemid\=6" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^canigetdab/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=14\&Itemid\=35" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^playlist\/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_wrapper\&Itemid\=74\/" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^books\/?$ "http\:\/\/www\.example\.co\.uk\/" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^bookcase\/?$ "http\:\/\/www\.example\.co\.uk\/" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^donate\/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=12\&Itemid\=32\/" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^haiti\/?$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=560\&Itemid\=78\/" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^easter$ "http\:\/\/www\.example\.co\.uk\/" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^social$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=568\&Itemid\=81" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^orgs$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=8\&Itemid\=28" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^cnb$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=574\&Itemid\=85" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^cnb\/login$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=574\&Itemid\=85\&login\=" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^noticeboard$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=574\&Itemid\=85" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^noticeboard\/login$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=574\&Itemid\=85\&login\=" [R=302,L]

RewriteCond %{HTTP_HOST} ^example.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.co.uk$
RewriteRule ^bagofhope$ "http\:\/\/www\.ucb\.co\.uk\/bagofhope" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^mp3stream$ "http\:\/\/example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=577\&Itemid\=87" [R=302,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^posters$ "http\:\/\/www\.example\.co\.uk\/home\/index\.php\?option\=com_content\&task\=view\&id\=579\&Itemid\=88" [R=302,L]

- 编辑 - 其他信息:

我已将问题缩小到fb_calendar目录 - 如果我使用带有随机名称的目录而不是fb_calendar它可以工作。所以它必须是这个目录中引起问题的东西。此目录中还有另一个.htaccess文件,其中包含以下内容:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

这会导致问题吗?我需要一个重写规则,该规则不受URL中路径上文件夹中任何其他.htaccess文件/重写规则的影响。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

默认情况下,

mod_rewrite仅对每个(内部)请求使用单个.htaccess文件中的指令,并且使用的.htaccess文件将是找到的最后一个mod_rewrite文件.../fb_calendar/.htaccess 1}}指令沿着你的请求路径。

在这种情况下,RewriteEngine存在并包含指令,因此使用它们代替根目录中的指令。

此外,请注意,指令的 none 是自动继承的,因此执行类似下面的操作会生成404,因为指令存在,但/.htaccess从未打开过第二个文件:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ /index.php?here

/test/.htaccess

RewriteRule ^ /index.php?there

http://example.com/test/non-existent-path

请求 - &gt; .htaccess

事实证明,您可以使用RewriteOptions指令从父RewriteEngine On RewriteOptions inherit 个文件导入规则:

.../fb_calendar/app/webroot/events/view/...

请注意,父规则集是在现有规则集之后导入的,因此在您的情况下,路径将被重写为.htaccess,然后由根.../fb_calendar/.htaccess文件中的规则处理(并且因此不会匹配)。

在这种情况下,您的选择是修改规则以适应这种情况,或者更简单地说,您可以将规则移动到{{1}}文件并调整它以便在该目录中。< / p>

答案 1 :(得分:0)

RewriteRule ^home/templates/cnb/fb_calendar/events/view/([^/\.]+)?$ /home/index.php?option=com_content&task=view&id=574&Itemid=85&direct=view:$1 [R=302,L]

我刚从重定向到的网址中删除了http://。

像魅力一样工作。在这里测试一下:

http://manson.revora.net/home/templates/cnb/fb_calendar/events/view/1234

转到

http://manson.revora.net/home/index.php?option=com_content&task=view&id=574&Itemid=85&direct=view:1234

此外,这也会重写地址栏中的网址,但是由于你添加了R = 302,我认为这就是你想要的?如评论中所指出的那样,您不需要第一条规则。