FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

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

  1. clover

    clover Peon

    Messages:
    472
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #481
    hi Nintendo,

    yes, the wildcard DNS is already enabled for the specific test domain

    and i can even get a

    anything.example.com = example.com/search?q=anything

    my current code is almost working.. but might need some minimal adjustments.

    thank you still for looking at my concern.

    cheers! :)

     
    clover, Jan 25, 2007 IP
  2. apachehtaccess

    apachehtaccess Guest

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

    Please clarify what rewrites you want to make exactly..

    In the meantime you can this code to redirect
    http://example.com/search.php?q=yyyy:askapache:
    ==>
    http://askapache.example.com/
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^([a-z]+\.)?(.+)\.com [NC]
    RewriteCond %{REQUEST_URI} ^/search\.php [NC]
    RewriteCond %{QUERY_STRING} ^q=([a-z]+):([a-z]+): [NC]
    RewriteRule .* http://%4.%2.com/? [L,R=301]
    Code (markup):
    Or to redirect
    http://example.com/search.php?q=apachecategory:askapache:
    ==>
    http://askapache-apachecategory.example.com/
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^([a-z]+\.)?(.+)\.com [NC]
    RewriteCond %{REQUEST_URI} ^/search\.php [NC]
    RewriteCond %{QUERY_STRING} ^q=([a-z]+):([a-z]+): [NC]
    RewriteRule .* http://%4-%3.%2.com/? [L,R=301]
    Code (markup):
     
    apachehtaccess, Jan 25, 2007 IP
    vishwaa likes this.
  3. clover

    clover Peon

    Messages:
    472
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #483
    Hi apachehtaccess,

    it would be best if i can make the rewrite as

    xxxx.example.com

    but considering the variable yyyy on the search string
    example.com/search.php?q=yyyy:xxxx:

    i guess its not possible as it would collide with the other variables of yyyy

    so the xxxx-yyyy.example.com output is acceptable.

    --------------

    I tried your code below and it gives me a output of

    xxxx-yyyy.example.com
    for
    example.com/search.php?q=yyyy:xxxx:

    but the variable yyyy is not recognized properly.
    if for example.

    example.com/search.php?q=zzzz:xxxx:

    it still gives me a search result of

    example.com/search.php?q=yyyy:xxxx:



    i hope i explained it well :)
    thanks apachehtaccess for the help!
     
    clover, Jan 26, 2007 IP
  4. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #484
    Ooops!

    This code works.

    
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^/search\.php [NC]
    RewriteCond %{QUERY_STRING} ^q=([a-z]+):([a-z]+): [NC]
    RewriteRule .* http://%2-%1.example.com/? [L,R=301]
    
    Code (markup):
     
    apachehtaccess, Jan 26, 2007 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #485
    Except I don't think he wants a redirect, he want's

    xxxx-yyyy.example.com

    to look exactly like

    example.com/search.php?q=yyyy:xxxx:

    as if the sub domain is the real URL.
     
    Nintendo, Jan 26, 2007 IP
  6. clover

    clover Peon

    Messages:
    472
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #486
    yes nintendo you are correct.

    but on consultation with other SEO people, some adviced that this is an SEO overkill and might be redflagged.

    I am abandoning the idea for now.

    Thanks again to Nintendo and apachehtaccess!!!!!

    any help is very much appreciated.

    regards,
     
    clover, Jan 28, 2007 IP
  7. apachehtaccess

    apachehtaccess Guest

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

    I'm not sure what you mean..

    So he doesn't want example.com/search.php?q=yyyy:xxxx: to be redirected?
     
    apachehtaccess, Jan 29, 2007 IP
  8. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #488
    Nintendo, Jan 29, 2007 IP
  9. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #489
    Oh ok... man that would have been a lot simpler if I would have known that! lol

    So now you don't even want the answer?
     
    apachehtaccess, Jan 29, 2007 IP
  10. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #490
    You can post it for next time some one comes along wanting a sub-domain this way.
     
    Nintendo, Jan 29, 2007 IP
  11. apachehtaccess

    apachehtaccess Guest

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

    Internally (transparent to the user) redirects
    http://digital-point.example.com => /search.php?q=point:digital:
    Code (markup):
    RewriteEngine On
    RewriteBase /
    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteRule ^ - [L]
    RewriteCond %{HTTP_HOST} !^(www\.)?example\.com [NC]
    RewriteCond %{HTTP_HOST} ^([a-z]+)?-?([a-z]+)?\.example\.com$ [NC]
    RewriteRule .* /search.php?q=%2:%1: [L]
    Code (markup):
     
    apachehtaccess, Jan 30, 2007 IP
  12. NosferatusCoffin

    NosferatusCoffin Active Member

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #492
    Hello,

    While I have worked a fair amount with htaccess, I have not really worked a lot with mod_rewrite.

    What I am looking to do is pretty simple. Due to a site conversion to WordPress, I am looking to change the URL from the old layout, which uses a PHP script for an Ask The Experts section, to the Wordpress page.

    Basically, this is what I looking for:

    From: http://www.beautynewsnyc.com/asktheexperts/index.php?act=Question&id=1
    To: http://www.beautynewsnyc.com/ask-the-experts/index.php?act=Question&id=1

    and

    From: http://www.beautynewsnyc.com/asktheexperts/index.php?act=Answer&cid=5&id=405
    To: http://www.beautynewsnyc.com/ask-the-experts/index.php?act=Answer&cid=5&id=405

    Here is the code I am using for the redirects:

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^asktheexperts/index.php?act=Question&id=([0-9][0-9])$ ask-the-experts/index.php?act=Question&id=$1[nc]
    RewriteRule ^asktheexperts/index.php/act=Answer&cid=([0-9][0-9][0-9])&id=([0-9][0-9][0-9])$ ask-the-experts/index.php?act=Answer&cid=$1&id=$2[nc]

    All that would change would be the directory, from asktheexperts to ask-the-experts.

    Any help would be greatly appreciated.
     
    NosferatusCoffin, Jan 31, 2007 IP
  13. roark

    roark Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #493
    Hi guys,
    apache 2.2
    winxp pro
    php5

    I'm having trouble with getting mod_rewrite to work on my machine with the above configuration.
    I'm new to mod_rewrite.

    If I run phpinfo(); it tells me I have succesfully loaded mod_rewrite.
    however I cannot get any mod_rewrite rule to work.

    here are the parts of httpd.conf I have changed:


    LoadModule rewrite_module modules/mod_rewrite.so

    DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"

    <Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Deny from all
    Satisfy all
    </Directory>



    And I am trying to use a .htaccess file with the following in it:


    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^wacko\.html$ index.html [L]


    (so when I browse http://localhost/wacko.html it should redirect me to http://localhost/index.html)

    This is not working, nor any mod_rewrite I try.

    I'm sure it must be something with the httpd.conf file but I have tried for days with no success.

    My main questions are:
    1. Is my mod_rewrite module active? (does it need to be in apache2+?)
    2. Is my httpd.conf configuration ok
    3. WHERE AM I GOING WRONG?

    any help would be greatly appreciated.

    Thank you!
    Roark
     
    roark, Jan 31, 2007 IP
  14. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #494
    Nintendo, Jan 31, 2007 IP
  15. roark

    roark Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #495
    Nintendo, thanks for the advice, at the moment I am hosted on a iis server, at the moment I am trying out apache as I would like to upgrade to it. so i wont be able to test it out on a real domain for a while.

    Is it a common problem getting mod_rewrite working on localhost?

    If so, is there a solution for it? It would be handy to get it working on my machine locally for testing purposes.

    Thanks for the help
    Roark
     
    roark, Feb 1, 2007 IP
  16. apachehtaccess

    apachehtaccess Guest

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

    For running apache on windows :( there is a really good forum and resource at Apache Lounge...
     
    apachehtaccess, Feb 1, 2007 IP
  17. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #497
    I've never tried mod_rewrite on localhost. Only on actual domains.
     
    Nintendo, Feb 1, 2007 IP
  18. roark

    roark Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #498
    Hey Guys

    I just could not find anything or anyone that had the same problem and posted about it.

    But I found a solution...

    When you, nintendo, said that you've never tried it on localhost before, made me think that that may be the issue, http://localhost/. so I changed the url in my address bar to http://127.0.0.1/ and my rewrite rule worked. (Using the ip address of my machine locally)

    Maybe you guys know why that made it work all of a sudden, I don't really know enough to try guess. but it worked...
    (I hope it helps some other people who might sit for 4 evenings looking for the solution)

    P.S. Thanks for the resources (apachehtaccess) I bookmarked that one.

    Cheers
    Roark
     
    roark, Feb 2, 2007 IP
  19. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #499
    You could even setup your actual domain on your local XP machine running apache. All the mod rewrite stuffs would work fine in this environment.

    You would need to configure your domain name in the virtual host section of apache configuration(httpd.conf) and also need to edit the hosts file (windows/system32/drivers/etc/hosts) to add the following lines.

    127.0.0.1 www.example.com
    127.0.0.1 example.com

    So you can actually access the local files with http://www.example.com/ instead of localhost/127.0.0.1

    When you are out of testing mode, remove the lines only from hosts file. You will be served directly from the actual remote server.

    I hope you can understand this.
     
    vishwaa, Feb 2, 2007 IP
  20. roark

    roark Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #500
    Brilliant...

    Thanks vishwaa, thats a great idea. I know that I'd need to change my hosts file, can you elaborate as to what changes I'd make in my httpd.conf file?

    I think i might try that.

    Thanks
    Roark
     
    roark, Feb 3, 2007 IP