删除.PHP并通过htaccess重定向

时间:2013-08-27 06:51:18

标签: php .htaccess

是否可以隐藏.PHP扩展名并同时重定向该网址。

示例:http://example.com/test需要重定向到http://someothersite.com

 Options +FollowSymlinks
 RewriteEngine on
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME}\.php -f
 RewriteRule ^(.*)$ $1.php
 RewriteRule ^(.*)/test http://someothersite.com

但它不起作用。

有什么想法吗?

由于

2 个答案:

答案 0 :(得分:1)

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteRule ^test http://someothersite.com [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

上面的代码没有经过测试,但它应该让你对它有所了解。在每组规则之后使用L flag,因为它会停止处理其余规则。

答案 1 :(得分:1)

你需要这个:

RewriteEngine On
RewriteBase /

RewriteRule ^(.*)$ $1.php [R=301,L]

对于跨域重写:

RewriteRule ^test http://someothersite.com [R=301,L]

总是使用L = 301进行永久重定向。