【发布时间】:2019-03-31 22:30:47
【问题描述】:
我正在尝试重写 URL 地址,但没有取得太大进展。 我读了很多书并尝试了,但我没有设法做我想做的事。如果有人给出一个想法,我会很高兴。 我想做的是:
- 删除“.php”扩展名,例如。 “site.com/about”
- 如果我输入“site.com/index.php”或“site.com/index”到 重定向到“site.com”
- 如果我键入“site.com/profile.php?profile=alex”以将 URL 重写为“site.com/profile/alex”
还要保留我包含的文件的路径,例如 css、js、图像或 php。
我分别尝试过的:
1。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
2。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
3。
RewriteEngine On
RewriteRule ^profile/(.+)[/]?$ profile.php?profile=$1
【问题讨论】:
-
为什么不重写所有对 index.php 的请求并使用router。
-
嗨@Jaquarh 感谢您的建议,但代码大部分是内联的。
-
@anubhava 是的,对于 1.,我尝试
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L]2.RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?/$1 [L]和 3.RewriteRule ^profile/(.+)[/]?$ profile.php?profile=$1 -
请编辑您的问题显示您尝试的 .htaccess
标签: php .htaccess url url-rewriting