0

I read similar questions on stackoverflow, but haven't found solution to my problem.

In my .htaccess, I have:

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] 

I need to add one more redirect. I have a real site page - domain.com/some_real_page, and I need a shorter link for it - domain.com/link. How is this possible?

Tried to do the following, but it is not working:

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] 

RewriteCond %{REQUEST_URI} ^/some_real_page$
RewriteRule ^(.*)$ http://www.domain.com/link [R=302,L]
4

1 回答 1

1

你这里有两个问题。首先,更具体的重写规则必须先于一般规则。其次,您的特定重写规则是倒退的。

RewriteEngine从上到下处理规则,很像编程if/else if/else。这意味着您希望将包罗万象的案例(又名^(.*)$)放在列表​​的底部。在此之前的所有规则都需要[L]属性在匹配后停止进一步处理。

对于第二部分,您想让浏览器链接http://www.domain.com/link指向实际文件http://www.domain.com/some_real_page.php。在您的示例代码中,您正在做相反的事情。

最终结果应如下所示:

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/link$
RewriteRule ^(.*)$ some_real_page.php [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] 
于 2013-09-05T05:46:40.333 回答