Forum Moderators: phranque
ErrorDocument 401 /errorpages/error-401.php
ErrorDocument 404 /errorpages/error-404.php
ErrorDocument 500 /errorpages/error-500.php
RewriteEngine On
RewriteBase /
Redirect 301 /galleries/index.php?cat=0 http://www.example.com/galleries/index.php
RewriteCond %{HTTP_HOST} \.us$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} \.org$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} \.net$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^index\.html?$ / [NC,R,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^$ http://www.example.com/index.php [L,R=301]
RewriteCond %{REQUEST_URI} /galleries/$
RewriteRule ^(.*) http://www.example.com/galleries/index.php [L,R=301] [edited by: eelixduppy at 1:33 am (utc) on Dec 1, 2011]
[edit reason] exemplified [/edit]
Note that the rules in htaccess cannot change your URLs. You must alter the links on the page to point to the correct URL. That is where the change occurs.
Also note that if your main concern is SEO, you can start by telling g### and the rest of them to disregard the named parameters. If you actually don't use them, you can then simply let them die a natural death.
o all of your domains pass through this same .htaccess? You can collapse the "host" group of rules into
%{HTTP_HOST} \.(us|org|net)$
or simply
%{HTTP_HOST} !^(www\.example\.com)?$
Put this rule after all the specific redirects. It only needs to pick up the remaining requests that haven't already been redirected by other means.
If I instruct htaccess to rewrite an url, the page with the new address will be sent to the browser. It is the same page as the old one, simply without the parameters I do not want in the title. Am I getting the meaning of htaccess completely wrong?
Make sure you've got a solid grip on the difference between redirect and rewrite.
if the browser's address bar is different from what they intially typed or clicked, it's a redirect. If the address stays the same, it's a rewrite.
Are you saying here that you want to use certain parameters, but not have them show up in the address bar? If they're not anywhere in the URL, they don't exist as far as htaccess is concerned
You can delete selected parameters
tell g### to ignore only those specific ones
Only use this feature if you feel confident about how parameters work for your site. Telling Googlebot to exclude URLs with certain parameters could result in large numbers of your pages disappearing from our index.
RewriteCond %{THE_REQUEST} /galleries/displayimage.php(.*)&pid=([0-9]+)
RewriteRule ^(.*) http://www.example.com/galleries/displayimage.php?pid=%2? [L,R=301]
http://www.example.com/galleries/displayimage.php?album=45&pid=917#top_display_media http://www.example.com/galleries/displayimage.php?pid=917#top_display_media RewriteCond %{THE_REQUEST} /galleries/thumbnails.php?album=([0-9]+)&page=1$
RewriteRule ^(.*) http://www.example.com/galleries/thumbnails.php?album=%1? [L,R=301] http://www.example.com/galleries/thumbnails.php?album=45&page=1 /galleries/thumbnails.php?
^[A-Z]{3,9}\ / to match the literal GET or POST at the start. ^galleries/thumbnails\.php$ for the RewriteRule pattern instead. This ensures the condition is evaluated only if the rule pattern is a match. The bit I've quoted contains two mistakes.Found them! I have to escape the . AND the ? :-) Done and now it works!
Can you really search for ? alone?I searched for QSA and I found what I needed!
Since THE_REQUEST is the literal GET request sent by the browser, you should begin your RewriteCond pattern with ^[A-Z]{3,9}\ / to match the literal GET or POST at the start.
It's detailed in a diagram in the Apache manual, and I did not understand the significance of it for at least a year, but the Rule pattern is evaluated first. The RewriteCond lines are evaluated only if the rule pattern was a match.
I can't get rid of "#top_display_media"
I still don't get when you're supposed to put it at the end of the rule
I actually thought that I could get away without the [A-Z]{3,9} if I just did not put the ^ before the pattern and apparently it seems to work... I mean, the pattern is matched and the rule is executed...
You will need RewriteCond whenever you look at anything other than the straight PATH part of the request.
You need it for looking at http_host, server_port, query_string, the_request and others.
report msg
joined:Apr 9, 2011
posts:1844
#:439318210:04 pm on Dec 1, 2011 (gmt 0)
I can't get rid of "#top_display_media"
You won't be able to. Anything in # is a fragment, not technically part of the URL at all. It is handled by the browser, not by the server. If the page no longer has an anchor for this fragment, it is not a problem, because the browser will simply send the user to the top of the page. This is a basic HTML requirement and you can trust all browsers to do it, no matter how capricious they are about other "compliant user-agents" rules ;)
? by itself at the end of the target means "delete the entire existing query string, if any". If there was no query, it has no effect. You will sometimes see a pattern ending in html? if you want to catch both "htm" and "html" extensions. For ? after parentheses, study Regular Expressions.
RewriteCond %{THE_REQUEST} \.php
RewriteRule \.php$ - [F]
[edited by: g1smd at 11:17 pm (utc) on Dec 1, 2011]