我一直在尝试在我正在构建的网站上实现以下重写/重定向,但我很挣扎,无法在线找到正确的答案。
我想重写
http://www.example.com/php/someFile.php
至:
example.com/someFile
我已经设法在我的 .htaccess 根文件中使用以下代码实现了大部分所需的结果,但我需要帮助来删除 php 子目录:
RewriteEngine on
# remove www from url
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
# redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
# internally map /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [L]
任何帮助将不胜感激。
在您的站点根目录 .htaccess 中使用这种方式:
RewriteEngine on
# remove www from url
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# redirect /php/file.php to /file
RewriteCond %{THE_REQUEST} \s/+(?:php/)?([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R=301]
# internally map /file to /php/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/php/$1.php -f
RewriteRule ^(.+?)/?$ php/$1.php [L]
请务必在清除浏览器缓存后进行测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句