How to remove webpage extension from URL? [closed]

2020-05-09 00:53发布

I want to remove webpage extension from URL,somebody gave me the code for .htaccess. But it works on localhost when I turn on rewrite module.But when I use it on Godaddy it doen't work.and also when I visit example.com/cool.php it says 404 not found But I want it to be redirect if some visit example.com/cool.php to example.com/cool.... and also help about godaddy rewrite module I contacted them they haven't replied me yet. Here below is the code the guy gave to me..Thanks

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条回答
贪生不怕死
2楼-- · 2020-05-09 01:23

Assuming you want to show the "pretty" URL (example.com/cool) but still get the data from the original request (example.com/cool.php), you may try this in the .htaccess file at root directory:

Updated to work with php files at any directory:

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
查看更多
登录 后发表回答