Jens Jørgen
Jens Jørgen

Reputation: 31

Htaccess rewrite with id

i got a problem with rewriting my edit.php?id=1 to edit/id/1. Right now i have the following in my .htaccess file:

RewriteEngine On
RewriteBase /

RewriteRule ^edit/id/([^/.]+)$ edit.php?id=$1 [NC]

This doesn't change the url. Can something see what i do wrong?

Upvotes: 1

Views: 3034

Answers (2)

anubhava
anubhava

Reputation: 784898

You need one additional rule to change the URL externally. This should be placed in root .htaccess:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/([^/]+)/edit\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1/edit/id/%2? [R=302,L,NE]

RewriteRule ^([^/]+)/edit/id/([^/.]+)$ $1/edit.php?id=$2 [NC,L,QSA]

Upvotes: 1

Panama Jack
Panama Jack

Reputation: 24448

Give this a try and see how it works for you.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/edit/id/([^/.]+)$ $1/edit.php?id=$2 [NC,L]
   </IfModule>

OR you can do this if you only have a couple of directories the same. The will go in the root .htaccess file.

  <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(profiles|customers|test)/edit/id/([^/.]+)$ $1/edit.php?id=$2 [NC,L]
       </IfModule>

Upvotes: 0

Related Questions