Lahey
Lahey

Reputation: 221

.htaccess to rewrite URL's with parameter

I wish to change my URL's from example this:

www.website.com/?page=about
www.website.com/?page=info
www.website.com/?page=contact

to this:

www.website.com/about
www.website.com/info
www.website.com/contact

This should be doable with one rule I hope. I've been looking around but I see people doing each occurrence separately. So the only URL parameter is "page" and the website has only one depth. Should be easy right? :)

Upvotes: 0

Views: 62

Answers (2)

anubhava
anubhava

Reputation: 784918

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ ?page=$1 [L,QSA]

Upvotes: 2

Panama Jack
Panama Jack

Reputation: 24448

This is a very basic common question. This goes in the .htaccess in your root.

RewriteEngine On
RewriteBase /

#prevent anyone from using the old urls
RewriteCond %{THE_REQUEST} [A-Z]{3,}\ /+\?page=([^&\ ]+) [NC]
RewriteRule ^ /%1? [R=301,L]

#provide pretty URL's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /?page=$1 [L]

Upvotes: 2

Related Questions