I’m trying to create my first .htaccess to make an URL rewrite and other things, but I’ve wasted two days searching for information and trying to make it work with no success.
What I would like to achieve is the following:
1) redirect all the non-www URLs to the www URL (it seems a best practice for seo?)
domain.com --> www.domain.com
2) redirect all the https requests to http ones:
https://www.domain.com --> http://www.domain.com
https://domain.com --> http://www.domain.com
3) rewrite my URLs to be SEO friendly, and eventually strip any trailing slash at the end of the address:
www.domain.com/abc --> www.domain.com/index.php?page=abc
www.domain.com/abc/ --> www.domain.com/abc
What I have so far, are three snippets which taken individually work:
# 1) This should be correct, I hope!
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# 2) This seems to work 100% right
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R,L]
#3) Major issues here
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
This works partially: it does what I want, but I get all relative paths broken when I put a trailing slash after the SEO friendly URL:
www.domain.com/abc OK
www.domain.com/abc/ page gets displayed but all relative URL for css and images are broken
I don’t even know how to combine the different rules in a single htaccess file to make them interact correctly.
Sorry for the long explaination, this problem is becoming quite frustrating for me.
The problem with relative path to css files or images of course is located in the trailing slash. The browser thinks he is in a subfolder an appends the relative pathes to the css files to your current location, which would result in
instead of
There are two ways, to work around this:
Redirect to same path without the trailing slash. since you seem to ignore the trailing slash, this should have no side effects. A matching rule would look like this
Your complete htaccess-file would be this one: