htaccess重定向工作在文件上但不适用于其他文件

时间:2015-05-25 10:14:30

标签: php .htaccess mod-rewrite redirect

以下是我的htaccess文件。

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f

RewriteBase /

ErrorDocument 404 /404.php
RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]

RewriteCond %{THE_REQUEST} \s/view_campaign\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ view_campaign/%1? [R=301,L]
RewriteRule ^view_campaign/([0-9]+)$ /view_campaign.php?var1=$1 [L]
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ view_campaign.php?var1=$2&var2=$3 [NC,L]

RewriteCond %{THE_REQUEST} \s/profile_view\.php\?id1=([0-9]+)\s [NC]
RewriteRule ^ profile_view/%1? [R=301,L]
RewriteRule ^profile_view/([0-9]+)$ /profile_view.php?id1=$1 [L]
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ profile_view.php?id1=$2&name2=$3 [NC,L]

这是我的网址/

http://localhost/profile_view/1/test
http://localhost/view_campaing/1/test-campaign-name

使用一些php代码,我的view_campaign链接正确重定向。我可以说我的view_campaign链接工作正常。如果它看起来如下。

http://localhost/view_campaign/1/test-campaign-name-some-junk
http://localhost/view_campaign/1
http://localhost/view_campaign/1/

我想为profile_view做同样的事情。我复制粘贴在htaccess / php文件中的相同代码并更改相关的东西。 问题是,如果我尝试打开http://localhost/profile_view/1/testhttp://localhost/profile_view/1http://localhost/profile_view/1/ 它正在重定向到

http://localhost/view_campaign/1/test-campaign-name-some-junk

我对htaccess了解不多。

请告知。

2 个答案:

答案 0 :(得分:1)

这样做:

ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]

RewriteCond %{THE_REQUEST} \s/view_campaign\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ view_campaign/%1? [R=301,L]

RewriteCond %{THE_REQUEST} \s/profile_view\.php\?id1=([0-9]+)\s [NC]
RewriteRule ^ profile_view/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^(view_campaign|profile_view)/(\d+)$ $1.php?var1=$2 [L,NC,QSA]
RewriteRule ^(view_campaign|profile_view)/(\d+)/([^/]+)$ $1.php?var1=$2&var2=$3 [NC,L,QSA]

答案 1 :(得分:0)

我在anubhava的答案中做了一些改变。现在,它工作正常。 以下是工作代码。

ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]

RewriteCond %{THE_REQUEST} \s/view_campaign\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ view_campaign/%1? [R=301,L]

RewriteCond %{THE_REQUEST} \s/profile_view\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ profile_view/%1? [R=301,L]

RewriteRule ^view_campaign/([0-9]+)$ /view_campaign.php?var1=$1 [L]
RewriteRule ^(view_campaign)/(\d+)/([^/]+)$ view_campaign.php?var1=$2&var2=$3 [NC,L]

RewriteRule ^profile_view/([0-9]+)$ /profile_view.php?var1=$1 [L]
RewriteRule ^(profile_view)/(\d+)/([^/]+)$ profile_view.php?var1=$2&var2=$3 [NC,L]