我已经准备好了一个项目。。现在客户端想要重写整个url。。。ie(www.mydomain/page.php)到www.mydomain/page等等。。。
文件夹中有多页,比如说大约10-15页。。我们有没有可能在给定的时间使用.htaccess重写这些页面的整个url?
此外,这些页面的链接带有".php"answers".html"扩展名,以便导航到其他页面。。现在我应该手动删除所有这些扩展吗?或者我可以通过其他方式更改它吗(例如;.htaccess)感谢
您需要添加一些内容来了解它是PHP还是HTML,比如
all .php files becomes www.mydomain.com/p/page
and .html files become www.mydomain.com/h/page
(这需要另一套htaccess规则,而不是下面的规则)
或者,您可以将所有.html文件更改为.php,这样会更容易。。
您现在可以让所有文件像一样隐藏其.php扩展名
www.mydomain.com/page.php becomes www.mydomain.com/page
要做到这一点,请在.htaccess上输入:
RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}' /([^' ]+)'.php
RewriteRule ^/?(.*)'.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}'.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
经过一些研究,我编辑了我的答案,因为你需要添加301个从旧链接到新链接的重定向,所以上面的代码现在应该可以工作了。这里的答案值得称赞:将.php URL重定向到不带扩展名的URL
您可以在.htaccess
文件中使用类似的东西
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
通过此操作,您可以使用以下URL:
mydomain.com/page
或
mydomain.com/page/test
但请注意,所有请求都将移动到index.php
,因此index.php
将类似于路由器。
在根目录中尝试Apache提供的功能。htaccess:
Options +MultiViews