如何从URL中删除网页扩展?

时间:2013-04-14 06:39:38

标签: php .htaccess url

我想从网址中删除网页扩展程序,有人给了我.htaccess的代码。 但是当我打开重写模块时它适用于localhost。但是当我在Godaddy上使用它时它不起作用。而且当我访问example.com/cool.php它说404找不到但是我希望它重定向一些访问example.com/cool.php到example.com/cool .... 并帮助godaddy重写模块我联系他们他们还没有回复我。 下面是这个家伙给我的代码。谢谢

RewriteEngine On
RewriteBase /

# remove enter code here.php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]


# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

1 个答案:

答案 0 :(得分:1)

假设您要显示“漂亮”网址(example.com/cool)但仍然从原始请求(example.com/cool.php)获取数据,您可以在根目录的.htaccess文件中尝试此操作:

更新为在任何目录中使用php文件:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

# Get the URI-path directly from THE_REQUEST variable
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/(.*)\.php [NC]
# Strip the extension and redirect permanently
RewriteRule  .*   /%2   [R=301,L,NC]

# Now the browser's address bar shows: http://example.com/cool

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php  [NC]
# Map internally to the original resource
RewriteRule  ^(.*)/?   /$1.php  [L,NC]

# Maps silently to: http://example.com/cool.php