htaccess重定向文件扩展名的规则

时间:2017-07-12 08:46:54

标签: php apache .htaccess

我搜索了SO并尝试了不同的东西但似乎没有工作:

我希望所有这些网址只能转到一个页面:

http://myurl.com/apply
http://myurl.com/apply/
http://myurl.com/apply.html
http://myurl.com/apply.php

所有人都应该转到此页面(http://myurl.com/apply.php),但网址应如下所示:

http://myurl.com/apply

现在我只有重定向到.php页面的部分。我无法将URL掩码用于隐藏.php的地方

Redirect /apply http://prosaverapp.com/apply.php
Redirect /apply/ http://prosaverapp.com/apply.php
Redirect /apply.html http://prosaverapp.com/apply.php
Redirect /apply.php/ http://prosaverapp.com/apply.php

2 个答案:

答案 0 :(得分:1)

以下是一个例子。

如果您只想删除扩展程序:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Apache的一些有用的方法:https://httpd.apache.org/docs/current/howto/htaccess.html

答案 1 :(得分:1)

您可以在站点根目录中使用这些规则.htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d    
RewriteCond %{THE_REQUEST} \s/+(\S+?)(?:/|\.(?:html|php))[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]