I've got a rewrite issue that is somewhat similar to isildur's problem. Normally, I don't have a problem getting rewrites to work, but his one is kicking my butt a bit. I want to eliminate some URLs in the SE indexes that have obsolete querystrings. In Google, they are sitting in supplemental, which is annoying because the URLs are actually still valid, but all the hyperlinks in the site that use querystrings were removed four months ago. The URLs are therefore orphaned URLs. I want to rewrite http://www.domainname.com/food-drink/restaurants/?s=A Code (markup): to http://www.domainname.com/food-drink/restaurants/ Code (markup): The possible values for s are A, H and P Here is the rule I'm currently working with, but it does not work: RewriteRule ^(.*)/?s=(A|H|P)$ /$1/ [R=301,L] The problem is related to the question mark. If I remove the question mark in the rule and in a test URL, it works. I've tried escaping the question mark in the rule, but it still doesn't work. Something related to the question mark is preventing it from working. RewriteRule ^(.*)/\?s=(A|H|P)$ /$1/ [R=301,L] This one should be simple, but it is just not cooperating. What am I missing?
hi RewriteEngine On # if the query string isn't empty RewriteCond %{QUERY_STRING} !^$ # redirect without the query string RewriteRule .* %{REQUEST_URI} [R,L] Expat
RewriteCond %{QUERY_STRING} ^s=A$ [OR] RewriteCond %{QUERY_STRING} ^s=H$ [OR] RewriteCond %{QUERY_STRING} ^s=P$ RewriteCond %{THE_REQUEST} ^.*\ HTTP/ RewriteRule .* http://www.domainname.com%{REQUEST_URI}? [R=301,L] or RewriteCond %{QUERY_STRING} !^$ RewriteCond %{THE_REQUEST} ^.*\ HTTP/ RewriteRule .* http://www.domainname.com%{REQUEST_URI}? [R=301,L]
Thanks for the help. Unfortunately, I tried all three ideas and none of them work. They each look like they should work. There is something that I'm missing here, because my simple one-liner should also work. It does if I remove the question mark from both the RewriteRule and a test URL, but the URLs I'm trying to remove from the search engine indexes each have the question mark. Figuring out how to get it to work with the question mark seems to be the key to this.
domain.com/.htaccess Options +Indexes Options +FollowSymlinks RewriteEngine on RewriteBase / RewriteRule ^food\-drink/restaurants/(.*)$ http://www.domainname.com/food-drink/restaurants/ [R=301,L] might do something.
Unfortuantely, the site at issue is a directory. The URL was just an example. I need to rewrite about 300 different URLs with categories and subcategories. The common factor is the three possible query string name-value pairs at the end. I tried a variation of your idea, but it messed with other deep URLs: RewriteRule ^(.*)/(.*)/(.*)$ /$1/$2/ [R=301,NC] So this one doesn't work either. I need to be able to detect any of the three query strings and rewrite those. This isn't a critical problem; it's just a nuisance issue. I have cleared out all of the other obsolete query strings from Google's index using 301 redirects. These few are the only ones that remain.
I don't know. I only have one directory. I used several other rewrites that have worked efectively to get rid of the URLs that essentially represent duplicate content. The ones that do not work contain a question mark. The "match" part of the rewrite rule is not being recognized when the question mark is present. Theoretically, I should be able to escape the question mark and it should work, but it doesn't. What am I missing with respect to using a match that contains a question mark? The search engines should eventually drop the URLs because the hyperlinks no longer exist in the site. However, they are still valid (i.e. they do not generate a 404), so when a search engine tests the URL, it works. Another approach might be to force the site to generate a 404 when those query strings are used.
? is not mod_rewrite friendly!!! I think RewriteCond %{QUERY_STRING} stuff is needed for that. Though I've used RewriteRule ^(.*)$ http://www.domain.com/mb/$1 [R=301,L] for redirecting a while vBulletin directory which of course has the ? in URLs!
I think you are right Mr. Wacko. According to all the documentation I've read, I should just be able to escape the ? just like a dot (.). However, it does not seem to work. Thanks for all the help guys. I will continue to experiment and will post a solution if I find one.
Great tips! Here are some SSL examples with Apache: SSL Redirect Method (doesn't require mod_rewrite!) SSLOptions +StrictRequire forces forbidden access (403) when SSLRequireSSL or SSLRequire decide access should be forbidden. Usually where a [WWW] Satisfy Any directive is used, this denial of access is overridden. For strict access restriction you can use SSLRequireSSL and/or SSLRequire in combination with an SSLOptions +StrictRequire Then an additional Satisfy Any has no chance once [WWW] mod_ssl has decided to deny access. SSLRequireSSL forbids access unless HTTP over SSL (i.e. HTTPS) is enabled for the current connection. SSLRequire forbids access unless HTTP_HOST matches your SSL certificate (in this case, the certificate is for askapache.com not www.askapache.com). If either of those 2 checks fail (403), then the [WWW] ErrorDocument directive uses a 302 to redirect the browser to https://askapache.com. SSLOptions +StrictRequire SSLRequireSSL SSLRequire %{HTTP_HOST} eq "askapache.com" ErrorDocument 403 https://askapache.com Code (markup): Note: Checking for the correct HTTP_HOST fixes the problem with Basic Authentication asking for the username/password twice, and also fixes security errors about your SSL certificate. Alternative to above method (doesn't require mod_ssl!) RewriteCond %{HTTPS} !=on RewriteRule .* - [F] ErrorDocument 403 https://askapache.com Code (markup): or RewriteCond %{HTTPS} !=on RewriteRule .*$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L] Code (markup): NOTE: The HTTPS variable is always present, even if mod_ssl isn’t loaded! This is useful if a non-SSL server is redirecting to a different SSL-enabled server. Redirect everything served on port 80 to SSL RewriteCond %{SERVER_PORT} ^80$ RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L] Code (markup): Redirecting to SSL or non-SSL using relative URIs RewriteRule ^/(.*):SSL$ https://%{SERVER_NAME}/$1 [QSA,R=302,L] RewriteRule ^/(.*):NOSSL$ http://%{SERVER_NAME}/$1 [QSA,R=302,L] Code (markup): There is a TON of best-practice htaccess and httpd.conf code snippets at the askapache blog. http://www.askapache.com/2006/htaccess/htaccesselite-ultimate-htaccess-article.htmlsnippets
Nice info. It looks like I got some mod_rewrite competition!! *gulp* That URLs wrong, it's.... http://www.askapache.com/2006/htaccess/htaccesselite-ultimate-htaccess-article.html (With 'snippets' taken out of the end.)
Hey thanks, I didn't notice it was wrong earlier, but when I just went to edit it the forum wouldn't let me due to live link restrictions or something. What do you mean mod_rewrite competitions?
BTW, there is another great article on the askApache blog about implementing caching using .htaccess or httpd.conf the url is *drum roll* http://www.askapache.com/2006/htaccess/speed-up-sites-with-htaccess-caching.htmlsnippets
I have a cgi script that I've included in a large group (100,000+) of pages on my website, and would find it much easier if i could redirect that cgi script using mod-rewrite to the existing hardcoded urls. That catch is I need to use the existing hard coded urls since they're widely linked to, and indexed by Google. The issue I have is due to the large number of pages, they're listed in a directory structure similar to this: directory/w/o/r/word.shtml Where the w represents the first letter of the word, the o the second, the r the third... and then you have the actual word.shtml. In cgi I can use something like this to parse the word "word" @ParseResult = split (//,'<!--LowerWord-->'); return "/registry/$ParseResult[0]/$ParseResult[1]/$ParseResult[2]/\L<!--LowerWord-->\E.shtml"; Is there a way to parse a word like this using mod-rewrite??? Thanks for any help! Dennis
Yea dennis this can be done quite easily with mod_rewrite, as this is the incorrect place to ask mod_Rewrite questions i would suggest posting your question at htaccesselite.com
hi nintendo and mod_rewrite gurus! I need to rewrite example.com/search.php?q=yyyy:xxxx: to xxxx.example.com or xxxx-yyyy.example.com if the above example is not possible given the yyyy variable. I have this code It works only on category (first line) How can i modify the code to work with the brand and merchant subdomains conditions? I am also getting an error on browsing the main domain example.com and www.example.com it shows the results of page http://example.com/search.php?q=category:: thank you in advance!
Does the domain have Wildcard DNS, where ANYTHING.example.com/search.php?q=WHATEVER:WHATEVER: would show the script? That's the only way I can think of that it might work, though RewriteBase / isn't RewriteSubDomain / so I don't know how it could be done. webmasterworld.com/apache/ *gags* might be a better place to ask.