I want to remove question mark & .php extension from the url using .htaccess

前端 未结 2 575
抹茶落季
抹茶落季 2021-01-14 07:26

I have a problem with redirect URL in .htaccess. I want to remove .php & question marks from the URL.

For Example: www.example.com/test.php?id=12

相关标签:
2条回答
  • 2021-01-14 07:53

    How about this? It requires mod_rewrite.

    RewriteEngine On
    RewriteRule ^test\/([0-9]+)$ test.php?id=$1
    
    0 讨论(0)
  • 2021-01-14 08:00

    That code appears to be from one of my answers :)

    Replace your code with this:

    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?id=123 to /dir/foo
    RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&\s]+) [NC]
    RewriteRule ^ %1/%2? [R,L]
    
    # To internally forward /dir/foo/12 to /dir/foo.php?id=12
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/([^/]+)/?$ $1.php?id=$2 [L,QSA]
    
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
    RewriteRule ^ %1 [R,L]
    
    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^(.*?)/?$ $1.php [L]
    
    0 讨论(0)
提交回复
热议问题