用.htaccess url重写重写不同的页面

时间:2015-05-18 08:39:43

标签: .htaccess url-rewriting

我想使用.htaccess进行网址重写。 在我之前的post中,您已经向我展示了如何对index.php页面进行操作,但现在也想对其他页面进行操作,例如browser.error.php等。 为此,我尝试在.htaccess中添加最后两行,但不幸的是,如果我尝试链接www.mysite / browser-error /它不起作用 - 请将我链接到索引页面。

Options +FollowSymLinks
RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^(.*)/(.*)/(.*)\/?$ index.php?op=$1&idric=$2&sp=$3 [L,QSA,NC]
RewriteRule ^(.*)/(.*)\/?$ index.php?op=$1&idric=$2 [L,QSA,NC]
RewriteRule ^(.*)\/?$ index.php?op=$1 [L,QSA]

#NEW ADDED ROWS
RewriteRule ^browser-error/(.*)\/?$ browser.error.php?r=$1 [L]
RewriteRule ^browser-error\/?$ browser.error.php [L]

我做错了什么?我怎么去?谢谢

2 个答案:

答案 0 :(得分:2)

我建议避免使用.*并使用[^/]+代替重新排序您的规则:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

#NEW ADDED ROWS
RewriteRule ^browser-error/([^/]+)/?$ browser.error.php?r=$1 [L,QSA,NC]
RewriteRule ^browser-error/?$ browser.error.php [L,NC]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?op=$1&idric=$2&sp=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?op=$1&idric=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?op=$1 [L,QSA]

答案 1 :(得分:0)

您在此规则之前有规则。这会使您的请求在达到规则之前更改为index.php。 (它们都有L标志,使它们成为最后处理的规则)。由于它们不限于在URL是浏览器错误时不应用,因此它们将在每个/ / / *或simialr请求上运行。

然后,你应该添加RewriteCond来否定你的其他RewriteRules中的浏览器错误网址:

请尝试并评论,如果不是您需要它不能按预期工作。

希望有所帮助

Options +FollowSymLinks
RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^(.*)/(.*)/(.*)\/?$ index.php?op=$1&idric=$2&sp=$3 [L,QSA,NC]

RewriteCond %{REQUEST_URI} !^browser-error\/?(.*)?\/?%
RewriteRule ^(.*)/(.*)\/?$ index.php?op=$1&idric=$2 [L,QSA,NC]

RewriteCond %{REQUEST_URI} !^browser-error\/?(.*)?\/?%
RewriteRule ^(.*)\/?$ index.php?op=$1 [L,QSA]

#NEW ADDED ROWS
RewriteRule ^browser-error/(.*)\/?$ browser.error.php?r=$1 [L]
RewriteRule ^browser-error\/?$ browser.error.php [L]