Remove .php from urls with htaccess

前端 未结 5 1635
抹茶落季
抹茶落季 2020-11-27 04:20

EDIT: current .htaccess file:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension snippet

# To          


        
相关标签:
5条回答
  • 2020-11-27 04:38

    On apache 2.4 and later, You can use the following Rule with END flag to remove .php extension from urls.

    RewriteEngine on
    
    RewriteRule ^(.+)\.php$ /$1 [NC,L,R=301]
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+)/?$ /$1.php [END]
    
    0 讨论(0)
  • 2020-11-27 04:43

    try this.

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^(.*)$ $1.php [L,QSA]
    </IfModule>
    

    if this not work then your sever do not have mod_rewrite activated or support url rewriting.

    0 讨论(0)
  • 2020-11-27 04:48

    Try this code for hiding .php (will work both ways):

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    ## don't touch /forum URIs
    RewriteRule ^forums/ - [L,NC]
    
    ## hide .php extension snippet
    
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R,L]
    
    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ $1.php [L]
    
    0 讨论(0)
  • 2020-11-27 04:58

    This should work

    Options -Indexes +FollowSymlinks -MultiViews
    
    RewriteEngine on
    
    DirectoryIndex index.php
    
    # REDIRECT Force requests for named index files to drop the index file filename, and force non-www to avoid redirect loop
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?|php[45]?)(\?[^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]*/)*)index\.(html?|php[45]?)$ http://example.com/$1 [R=301,L]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)/([^\.]+)\.php\ HTTP/
    RewriteRule ^([a-zA-Z0-9_-]+)/([^.]+)\.php$ http://example.com/$1/$2 [R=301,L]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)/([^/]+)/([^\.]+)\.php\ HTTP/
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([^.]+)\.php$ http://example.com/$1/$2/$3 [R=301,L]
    
    # REDIRECT www to non-wwww
    RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
    RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L]
    
    # REWRITE url to filepath
    RewriteRule ^([a-zA-Z0-9_-]+)/([^/.]+)$ /$1/$2.php [L]
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([^/.]+)$ /$1/$2/$3.php [L]
    
    0 讨论(0)
  • 2020-11-27 05:03

    Here is the simplest syntax I have found, which would result as below:

    RewriteEngine on
    RewriteRule ^/?PrivacyPolicy$ legal.php?mode=privacy [NC,L]
    RewriteRule ^Report/([0-9]+)$ report.php?id=$1 [NC,L] 
    

    Results:

    1. PrivacyPolicy to legal.php?mode=privacy
    2. Report/55 to report.php?id=55
    0 讨论(0)
提交回复
热议问题