skip to Main Content

I have made sure that rewrite engine is enabled and removing .php extensions is working so I know that isn’t the issue.

what I’m trying to do is simply remove the ?id=value aspect of the URL, so basically making the URL look like such:

folder/medias/value

Instead of

folder/medias?id=value

My current .htaccess looks like this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

RewriteRule ^404/?$ /404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^ 404.php [L,R]

2

Answers


  1. With your shown samples/attempts, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    ##Rules for external rewrite.
    RewriteCond %{THE_REQUEST} s/([^.]*).php?id=(S+)s [NC]
    RewriteRule ^ /%1/%2? [R=301,L]
    ##Rule for internal rewrite.
    RewriteRule ^([^/]*)/([^/]*)/?$ $1?id=$3 [L]
    
    Login or Signup to reply.
  2. You may try this code inside the /folder/.htaccess (create this file if it doesn’t exist):

    RewriteEngine On
    
    # External redirect from /folder/media?id=val to /folder/media/val
    RewriteCond %{THE_REQUEST} /(S+?).php?id=([^&s]+)s [NC]
    RewriteRule ^ /folder/%1/%2? [R=301,L,NE]
    
    # Internal rewrite from /folder/media/val to /folder/media?id=val
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([w-]+)/([w-]+)/?$ $1.php?id=$2 [L,QSA]
    
    • Trailing ? in first rule is to remove query string from original URL.
    • %{REQUEST_FILENAME} !-f and %{REQUEST_FILENAME} !-d is to skip existing files and directories from rewrite in 2nd rule.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search