从网址中删除文件扩展名

时间:2013-05-19 09:04:32

标签: php .htaccess

我的网站上有一些像这样的网址..

http://www.mydomain.com/about.php
http://www.mydomain.com/contact.php
http://www.mydomain.com/signup.php
http://www.mydomain.com/testimonials.php ...... and much more 

现在我正在尝试将上面的网址转换为用户友好的网址

http://www.mydomain.com/about
http://www.mydomain.com/contact
http://www.mydomain.com/signup
http://www.mydomain.com/testimonials

这是我目前为止尝试过的.htaccess文件中的代码。但它不起作用。

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php 

任何人都可以帮我从我的网址中删除.php etension吗? 谢谢。

2 个答案:

答案 0 :(得分:1)

摆脱您正在使用的条件,而不是在htaccess文件中使用以下2行

RewriteEngine On # Turn on the rewriting engine
RewriteRule   ^whatever/?$   whatever.php  [NC]

即使用户在名称后面键入正斜杠

,这也会有效

如果你想让它们变得动态,here's是一个很好的教程

答案 1 :(得分:0)

正确实现,以便在您无法访问服务器的htaccess文件或覆盖时不会在部署期间造成混淆。您可以考虑使用像toro-php http://toroweb.org/这样的库来轻松路由。

相关问题