duan3019 2015-06-25 23:29 采纳率: 100%
浏览 53
已采纳

301重定向也被重写的URL

I am building a site using Slim, which suggests the following .htaccess code for creating pretty URLs:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

This means that a URL like http://example.com/account/login will alias for http://example.com/index.php/account/login, which is then processed by my front controller. So far so good.

However, if someone explicitly navigates to

http://example.com/index.php/account/login,

I would like it to first redirect to:

http://example.com/account/login,

and then alias for

http://example.com/index.php/account/login.

That way, the user will see http://example.com/account/login in their navigation bar.

How can I handle both of these rules simultaneously in .htaccess, and most importantly, without hardcoding the host and domain (http://example.com) in .htaccess?

This should also work in a subdirectory, for example:

http://example.com/site1/index.php/account/login -> http://example.com/site1/account/login

With the .htaccess file located in the site1 directory relative to document root, and without having to explicitly put site1 in the .htaccess.

  • 写回答

2条回答 默认 最新

  • duansha7453 2015-06-26 04:08
    关注

    You need a new redirect rule before this rule:

    RewriteEngine On
    
    RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
    RewriteRule ^.*$ - [E=BASE:%2]
    
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^index\.php(?:/(.*))?$ %{ENV:BASE}$1 [L,R=302,NC,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?