Gregor
Gregor

Reputation: 81

redirect users from dynamic to static url (with two parameters)

I have a URL on my website with the structure:
- mywebsite.com/directory/object.php?id=[id]&title=[title]
Using the .htaccess file i have changed the above dynamic URL to the following:
- mywebsite.com/directory/[id]/[title] similar to stackoverflow's URL for this question..

I would like to know how to redirect any user that types in the old dynamic URL to the new static one. I know you use RewriteCond but i can't figure out the code.

My .htaccess file code that changes the dynamic URL into the static URL contains:

    RewriteEngine On
    RewriteRule ^directory/([^/]*)/([^/]*)$ /directory/object.php?id=$1&title=$2 [L]

Thanks in advance!

Upvotes: 2

Views: 259

Answers (1)

anubhava
anubhava

Reputation: 785531

Have it this way:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+(directory)/object\.php\?id=([^&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

RewriteRule ^directory/([^/]+)/([^/]+)/?$ /directory/object.php?id=$1&title=$2 [L,QSA]

Upvotes: 2

Related Questions