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

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.

图片转代码服务由CSDN问答提供 功能建议

我正在使用Slim构建一个站点,它建议使用以下 .htaccess 代码进行创建 漂亮的URL:

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

这意味着 http://example.com/account/login 之类的网址 http://example.com/index的别名。 php / account / login ,然后由我的前端控制器处理。 到目前为止一直很好。

但是,如果有人明确导航到

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

我希望首先重定向到:

http:

然后别名

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

这样,用户将在其导航栏中看到 http://example.com/account/login < p>如何在 .htaccess 中同时处理这两个规则,最重要的是,无需在< http://example.com 中对主机和域( http://example.com )进行硬编码 代码> .htaccess

这也应该在一个子目录中工作,例如:

http:// example .com / site1 / index.php / account / login - &gt; http://example.com/site1/account/login < p>使用位于 site1 目录中的 .htaccess 文件相对于文档根目录,而无需在中明确地放置 site1 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]
    
    点赞 打赏 评论
  • duanqiangwu9332 2015-06-26 00:23

    Try this:

    RewriteRule ^index\.php/(.*)$ /$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
    
    点赞 打赏 评论

相关推荐 更多相似问题