1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

Discussion in 'Apache' started by Nintendo, Jul 30, 2005.

  1. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #461
    Nope, his rule worked.
     
    crazyryan, Dec 20, 2006 IP
  2. TechEvangelist

    TechEvangelist Guest

    Messages:
    919
    Likes Received:
    140
    Best Answers:
    0
    Trophy Points:
    133
    #462
    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. :eek: What am I missing?
     
    TechEvangelist, Dec 27, 2006 IP
  3. TechEvangelist

    TechEvangelist Guest

    Messages:
    919
    Likes Received:
    140
    Best Answers:
    0
    Trophy Points:
    133
    #463
    Nintendo, oh mod_rewrite guru of gurus, where art thou?
     
    TechEvangelist, Dec 27, 2006 IP
  4. expat

    expat Stranger from a far land

    Messages:
    873
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #464
    hi

    RewriteEngine On

    # if the query string isn't empty
    RewriteCond %{QUERY_STRING} !^$
    # redirect without the query string
    RewriteRule .* %{REQUEST_URI} [R,L]



    Expat
     
    expat, Dec 27, 2006 IP
    vishwaa likes this.
  5. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #465
    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]
     
    vishwaa, Dec 27, 2006 IP
  6. TechEvangelist

    TechEvangelist Guest

    Messages:
    919
    Likes Received:
    140
    Best Answers:
    0
    Trophy Points:
    133
    #466
    Thanks for the help. :D

    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.
     
    TechEvangelist, Dec 27, 2006 IP
  7. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #467
    Nintendo, Dec 27, 2006 IP
  8. TechEvangelist

    TechEvangelist Guest

    Messages:
    919
    Likes Received:
    140
    Best Answers:
    0
    Trophy Points:
    133
    #468
    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.
     
    TechEvangelist, Dec 27, 2006 IP
  9. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #469
    Does that code work with any directories?
     
    Nintendo, Dec 27, 2006 IP
  10. TechEvangelist

    TechEvangelist Guest

    Messages:
    919
    Likes Received:
    140
    Best Answers:
    0
    Trophy Points:
    133
    #470
    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.
     
    TechEvangelist, Dec 28, 2006 IP
  11. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #471
    ? 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!
     
    Nintendo, Dec 28, 2006 IP
  12. TechEvangelist

    TechEvangelist Guest

    Messages:
    919
    Likes Received:
    140
    Best Answers:
    0
    Trophy Points:
    133
    #472
    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.
     
    TechEvangelist, Dec 28, 2006 IP
  13. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #473
    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
     
    apachehtaccess, Jan 17, 2007 IP
    Nintendo likes this.
  14. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #474
    Nintendo, Jan 17, 2007 IP
  15. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #475

    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?
     
    apachehtaccess, Jan 18, 2007 IP
  16. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #476
    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
     
    apachehtaccess, Jan 18, 2007 IP
  17. perdrix

    perdrix Peon

    Messages:
    187
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #477
    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
     
    perdrix, Jan 19, 2007 IP
  18. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #478
    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
     
    apachehtaccess, Jan 22, 2007 IP
  19. clover

    clover Peon

    Messages:
    472
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #479
    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!
     
    clover, Jan 24, 2007 IP
  20. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #480
    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.
     
    Nintendo, Jan 25, 2007 IP