Eddie
Eddie

Reputation: 337

htaccess seo friendly urls and redirect

I'm currently trying to make urls more SEO friendly, the problem is they are only half working.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?name=$1 
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?name=$1

That's my code. It works to an extent, but I can load the page from both mysite.com/index.php?name=name and mysite.com/name/ ; where as I only want mysite.com/name/ to load and I want mysite.com/index.php?name=name to redirect to mysite.com/name/

Any suggestions? Thanks.

Upvotes: 1

Views: 43

Answers (1)

Sreedev S B
Sreedev S B

Reputation: 279

Try this one


RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?name=([^&\s]*)\s [NC]
RewriteRule ^ %1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?name=$1 [L]

Upvotes: 1

Related Questions