chris
chris

Reputation: 36937

htaccess canonical url with slugs

Trying to figure out how to write my httaccess to let me have it so my urls are 'www' free and allow me to have a slug style setup for 'friendly' urls. I seem to keep writing myself into an internal 500 error though. That or it doesn't seem to carry over the extra stuff. extra stuf being anything from .com/ over ie mydomain.com/hello/world

RewriteEngine On
Options +FollowSymlinks -MultiViews
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.mydomain.com [NC]
RewriteRule ^(.*)/?$ http://mydomain.com/index.php?r=$1

Overall goal take URL in either of these 2 fashions

http://mydomain.com/hello/world

http://www.mydomain.com/hello/world

and have it translate to

http://mydomain.com/hello/world to the front end but on the backend be the equivalent to http://mydomain.com/index.php?r=hello/world

also I would like to apply conditions where if a file exists, or folder exists, or whatever exists stop the rewrite cold. I know this is possible well without the removal of the www part, not sure with that part, as I used to once have an htaccess file that I could do this with, but I have lost that file and its been far to long since ive played with htaccess to remember how i did it in the first place.

Upvotes: 0

Views: 464

Answers (1)

iDev247
iDev247

Reputation: 1901

This will forward www to non-www site. Also take care of the index.php.

RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
RewriteRule ^(.*)$ index.php?r=$1 [QSA,L]

You can also add the following two lines. Like this if you have any css or images it will not rewrite them to index.php:

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

This is not tested but a general guidance.

Upvotes: 1

Related Questions