.htaccess - 如何通过 htaccess 仅重命名特定的 url?
问题描述
我已经设法使用下面的 htaccess 代码删除了 .php 扩展名,但我现在面临的问题是 index.php 显示像www.site.com/index
有人可以帮我把它重命名为www.site.com/home而不是 /index 吗?
这是我完整的 htaccess 文件
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ pages.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
<IfModule mod_deflate.c>
<FilesMatch "\.(css|x?html?|php)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
<IfModule mod_expires.c>
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/html "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F,L]
</IfModule>
非常感谢您的帮助。
解决方案
您能否使用一次在您的 .htaccess 文件中设置的以下规则。请确保在测试您的 URL 之前清除您的浏览器缓存。
RewriteEngine On
RewriteBase /
##new redirection rules here for index.php to home.
RewriteRule ^index\.php/?$ /home? [R=301,NC,L]
RewriteRule ^home/?$ site/index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ pages.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
<IfModule mod_deflate.c>
<FilesMatch "\.(css|x?html?|php)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
<IfModule mod_expires.c>
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/html "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [NC,OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2}) [NC]
RewriteRule .* index.php [F,L]
</IfModule>
推荐阅读
- java - 创建或复制动态数量的 Shape3DObjects
- vb.net - 将 DataGrid 中一行的颜色更改为红色
- amazon-web-services - 由于以下原因之一,远程桌面无法在亚马逊 Web 服务 Windows 实例上连接到远程计算机
- python - 如何在python中向用户显示模块和子类的文档
- mysql - Sequelize多表OR语句未知列错误
- reactjs - JS 上的无限循环 for
- raku - 双结参数交换边
- python - Pandas Dataframe:计数和 Bin 整数和日期时间到产生两个输出数据帧的范围内
- css - 带边框的 CSS 自定义形状
- stata - 编码后Stata中的奇怪排序