rockstardev
rockstardev

Reputation: 13527

How do I rewrite a single parameter using htaccess?

I have URLs in the following format:

http://www.mysite.com/index.php?l=accommodation/lodge/some-place

I would like to use htaccess to rewrite it to this format:

http://www.mysite.com/accommodation/lodge/some-place

How would I do that? I started trying to do it with drupal's htaccess file and got something like this:

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?l=$1 [L,QSA]

Problem is, if I enter the original index.php url, it still shows. It must redirect to the short version. In other words, this:

http://www.mysite.com/index.php?l=accommodation/lodge/some-place

Must forward to this:

http://www.mysite.com/accommodation/lodge/some-place

Thoughts?

Upvotes: 1

Views: 34

Answers (1)

anubhava
anubhava

Reputation: 784928

Insert this additional rule above your rule:

RewriteCond %{THE_REQUEST} \s/+index\.php\?l=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]

This will externally redirect http://www.mysite.com/index.php?l=accommodation/lodge/some-place to http://www.mysite.com/accommodation/lodge/some-place

Upvotes: 1

Related Questions