Forum Moderators: phranque
ErrorDocument 400 /error/
ErrorDocument 401 /error/
ErrorDocument 403 /error/
ErrorDocument 404 /error/
ErrorDocument 500 /error/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{QUERY_STRING} letter=([a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z])$
RewriteRule ^ http://www.example.com/letter-%1/? [R=301,L]
RewriteCond %{QUERY_STRING} lang=nl&page=about$
RewriteRule ^(.*)$ http://www.example.com/aboutus/? [L,R=301]
RewriteCond %{QUERY_STRING} lang=nl&page=beer$
RewriteRule ^(.*)$ http://www.example.com/wine/? [L,R=301]
RewriteCond %{QUERY_STRING} lang=nl&page=drinks$
RewriteRule ^(.*)$ http://www.example.com/food/? [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
AddType application/x-httpd-php .php .html
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType text/html "access plus 1 days"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 years"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
</IfModule>
It works, but is this the correct way? and doesn't this make multiple redirects?
RewriteCond %{QUERY_STRING} letter=([a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z])$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L] ^([^.]+[^./])$ RewriteRule ^([^.]+[^./])$ http://www.example.com/$1/ [R=301,L] RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/$ /$1.html [L] RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/$ /index.php?pagename=$1 [L] 1) Requesting this url: http://example.com/pagename redirects to: www.example.com/pagename.html/
2) Page loads the same content:
www.example.com/pagename.html
www.example.com/pagename/
www.example.com/pagename
These 3 all load the same page
But i would like only to be www.example.com/pagename/
3) I am not sure wether the redirects and my htaccess is good because sometimes (ones every 20 minutes) i see in my data that a page or file is requested repeatedly
That is, oops: OK except that where's the pattern? Are you redirecting everything to the root, or is this rule only meant to apply to root requests?
Yikes. Is it possible you've misunderstood the use of a hyphen? All you need there is [a-z]. Just one letter, right?
You never need <IfModule> envelopes. Either you've got a mod or you haven't. Find out, and write the rules accordingly.
First of all: You're not doing anything with images and stylesheets, are you?
Here, it looks as if you're working with extensionless URLs.
Where does your rewriting take place? Did you leave it out from the quoted material?
How often is "repeatedly"?
One time it requested over 500 times, and another time it was requested 60 times. I think it keeps requesting untill the visitor leaves the page.
But the files do exist.
Should they perhaps be in full path: http://www.example.com/error/
Generally speaking, this is most likely a timeout by the server caused by a 'loop' (endless search for a file that does not exist. Most common is Error Documents).
<Files "forbidden.html">
Order Allow,Deny
Allow from all
</Files>
If you are on shared hosting and they tell you to use a particular name for error documents, these lines are already present in the config file. RewriteRule forbidden\.html - [L] This rule is not mean to go to redirect to the root and mean to apply for &page=pagename requests
That will redirect to www.example.com/pagename/
or whatever name that page has. (with a few exceptions of pagename change that i did manual)
So I am guessing it's not correct after all?
RewriteCond %{QUERY_STRING} lang=(fr|uk|us|nl)&page=([^&]+)
RewriteRule ^$ http://www.example.com/%2/? [R=301,L] I have a dictionary with words from a to z on the website, so i thought i need to put them all in for all 26 pages. If I change that to [a-z] it works the same?
You never need <IfModule> envelopes. Either you've got a mod or you haven't. Find out, and write the rules accordingly.
Didn't know that, I will try and find out.
You're not doing anything with images and stylesheets, are you?
No I am not, should I?
Here, it looks as if you're working with extensionless URLs.
I used to, but now it's .html files.
Where does your rewriting take place? Did you leave it out from the quoted material?
I didn't left it out because it doesn't exist. The htaccess i posted is my complete file. I have got multiple headache of this and because i was not sure, i posted this question. I am very happy you answered my question, but it also makes me more confused then before :-)
Should all redirects, or all individuel redirects have:RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/$ /$1.html [L]
Here is a screenshot of my log:
Um, not sure. I got lost in the "not"s. If your ordinary URL looks like
example.com/pagename=blahblah
and you want to redirect to
example.com/blahblah/
then the rule should say
RewriteCond %{QUERY_STRING} lang=(fr|uk|us|nl)&page=([^&]+)
RewriteRule ^$ http://www.example.com/%2/? [R=301,L]
That way, the server only looks at conditions when the request is for the index page-- and the rule only executes if the condition is met. mod_rewrite works on a "two steps forward, one step back" system, where conditions are only evaluated if the pattern of the rule potentially fits.
Uh-oh, now wait. Do your final URLs -- the ones seen by the user -- end in / or in .html? My impression from all other rules is that everything is supposed to end in a / slash. Again, that's the visible URL. The physical file is a different matter.
You are not the first person to have this complaint :( But if you are redirecting to URLs that end in / and there's no rewriting, that means that every single one of your pages is called "index.html" and each one lives in its own directory. Somehow I don't think that's what is happening.
Oh, dear. That's amazingly unhelpful. Did you edit parts of the log before taking the screenshot? If not, it looks as if everything-- including non-page files-- is being redirected to /pagename/
The only problem here is that if a user request example.com/blabla/ That page does not excist and should be redirect to example.com/error/
But instead, the error page gets loaded and the URL remains the same. The response header is: HTTP/1.1 404 Not Found.
This is giving some 404s in webmastertools, which i then have to manual redirect again. There should be a better way for this.
RewriteRule ^ etcetera RewriteRule ^$ etcetera RewriteRule ^(index\.html)?$ etcetera How do we make this so that rewriting does make place?
RewriteCond %{REQUEST_URI} !^/(images|stylesheets|mystuff)/ Do you think this would prevent the multiple redirects?
That's what is supposed to happen when a page doesn't exist. What are you complaining about?
But in order to hammer out this rule, we need to know what the real, physical filename is for each URL. In an earlier post I tossed out a couple of possibilities, but those were just guesses.
every single request receives a 200, except one place where there are four consecutive identical requests each receiving the same 301. So the problem is not with infinite redirects. In fact: Are you sure the quoted snippet isn't an artifact of selecting-and-pasting, or a hiccup in the logs themselves? The one thing I'm definitely not seeing is anything that would suggest an infinite loop.
So the rule should be:
RewriteCond %{REQUEST_URI} !^/(img|includes|downloads)/
RewriteRule ^([^./]+)/$ /$1.html [L] /pagename.html RewriteRule ^(img|includes|downloads)/index\.html http://www.example.com/$1/ [R=301,L]
RewriteRule ^([^./]+/)index.html http://www.example.com/$1 [R=301,L]