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条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了