FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

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

  1. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #601
    kks_krishna, May 13, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #602
    Try this in a domain.com/dlinks/links/.htaccess file:
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /dlinks/links/
    RewriteRule ^([0-9]+)\.html$ linkit.php?id=$1 [L]
    Code (markup):
     
    rodney88, May 13, 2007 IP
  3. mikey1090

    mikey1090 Moderator Staff

    Messages:
    15,869
    Likes Received:
    1,055
    Best Answers:
    0
    Trophy Points:
    445
    Digital Goods:
    2
    #603
    good stuff, cheers
     
    mikey1090, May 15, 2007 IP
  4. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #604
    Hi Nintendo,

    First thanks for the lovely guide in the beginning - I tried but somehow messed up. I have both the pages mysite.com/ and mysite.com/index.php in diff PR.

    I wanted to redirect /index.php to .com/

    First I used the code :


    This sent the site on an infinite loop. My hosting ppl said it needs higher coding... like u said mod_rewrite is voodoo ... so i gave up.

    As of now I have this code below, which is working well - which redirects the http://mysite.com to http://www.mysite.com


    I need help in redirecting all /index.php to .com/ - The script of the site is too big to omit all reference to /index.php ...

    Thanks,

    JS
     
    jack_sparrow, May 15, 2007 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #605
    Put the

    RewriteRule ^index.php$ http://www.domain.com/ [R=301,L]

    before the

    RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

    stuff. If that doesn't work, just link to the URL without index.php.
     
    Nintendo, May 15, 2007 IP
  6. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #606
    Hi Nintendo,

    Done that, but again its not working. Also removing the ref of /index.php is diff as its a large script and the instructions goes thru smarty.

    Any other way you might think off... as this is a bugging issue for many...
    and maybe it is bcoz both the url share the same page...

    I have a silly idea... we have error pages like 400.shtml, 401,402 etc... which redirects ppl to the home page...

    What happens if we use mod re-write in redirecting /index.pages to some intermediate page ... will that work..if yes then what will be the code like.

    Thanks

    JS.

    PS. Dont laugh at my idea if it is funny :)
     
    jack_sparrow, May 16, 2007 IP
  7. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #607
    Hi Nintendo,

    Another help required. I have two long urls coming like:

    mysite.com/view_video.php?viewkey=f9c4f6877e1894a1faa0&page=1&viewtype=&category=
    mysite.com/view_video.php?viewkey=51c63a55cdb8cb047fad&page=1&viewtype=&category=mr

    which I would like to reduce tosomething like:

    mysite.com/view_video.php?viewkey=f9c4f6877e1894a1faa0
    &
    mysite.com/view_video.php?viewkey=51c63a55cdb8cb047fad

    Please help me with the rewrite code.

    Thanks,

    JS.
     
    jack_sparrow, May 16, 2007 IP
  8. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #608
    Hi Nintendo,

    I have read all your post and scrapped the net and found a solution to redirecting index page to homepage. Meaning any homepage/index.extn (php or html) to the homepage.

    Normally if we use "RewriteCond %{HTTP_HOST}" or "{REQUEST_URI} then a client request for / is internally re-written as /index.html by mod_dir which always executes before anything else. And only after which mod_rewrite is invoked which redirects it to / and the process repeats ( as the call is to the same page) until the redirection limit is hit on the server or your host suspends your account.

    The code:

    This code verifies the original request of the client which is not updated by mod_dir like it does for http_host and request_uri.

    The other variant is:

    In some palce i saw the advice of using " [A-Z]{3,9} " though i dont understand what good it is. The second code is supposed to work for all directories (?)
    (Pls replace html with php wherever needed)

    I am currently using th code below:

    The only problem is though it works for /index.php it does not redirects domain.com/index.php? and domain.com/? which comes occasionaly.

    Both of the codes will work and there will be no infinite loop. I would leave it to "Nintendo" to tell us if we can use this and if there is any downside to it in terms of SEO or indexing.

    I would also request you to let us know if we need any line of code in the robots.txt file to disallow SE's from indexing the index page. Also , whether we need to remove /index.php reference from the sitemap.xml file if any.

    Regards,

    JS.
     
    jack_sparrow, May 16, 2007 IP
  9. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #609
    If it works, you can use it! I'm a total n00bie when it comes to RewriteCond stuff.

    Is there anything that you don't have working right now?
     
    Nintendo, May 16, 2007 IP
  10. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #610
    Why not just use
    Options All -Indexes
    DirectoryIndex index.php index.html
    Code (markup):
    I recommend using Googles Webmaster Tools to see what you need to do for SEO, use the robots.txt analysis tool

    You can read about SEO with robots.txt, and Wordpress robots.txt optimized for SEO
     
    apachehtaccess, May 16, 2007 IP
  11. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #611
    Dear Nintendo,

    I appreciate your honesty and help. I just got the RewriteCond stuff from somewhere on the net - i am a gr8r noobie than you. But it's working in re-directing the index page without any loop!

    I need help with the re-write rule. The current code is:

    What changes are required so that index.php , index.php?, and /? all gets redirected to domain.com

    I tried the above also, but it does not work.

    Regards,

    Jack
     
    jack_sparrow, May 17, 2007 IP
  12. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #612
    Can you edit the script so the links don't have index.php , index.php?, and /?

    Options +FollowSymLinks +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]
    RewriteRule ^index\.php\?$ http://www.domain.com/ [R=301,L]
    RewriteRule ^/\?$ http://www.domain.com/ [R=301,L]

    But I'm guessing it might not work since it has ?. I think ? needs a RewriteCond.
     
    Nintendo, May 17, 2007 IP
  13. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #613
    Hi Nintendo!

    I have been able to solve(?) one of the problem (index.php/?) by playing around with the RewriteCond ... its real voodoo. You were correct in saying that "?" requires special treatment of RewriteCond.

    Here it is :

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php?\?\ HTTP/
    RewriteRule ^(.*)index\.php?$ http://www.domain.php/$1& [R=301,L]

    There is a funny thing happening - its redirecting as if in two steps: Please check the url and tell me if it is Ok or if it needs correction:

    http://www.hotclips.mobi/index.php?

    The other problem redirecting mydomain.com/? to mydomain.com is still there... let me know if you can help me from the above code.

    I require one more help also:

    To change the url:

    /view_video.php?viewkey=f1586614cf2c1ad240b6&page=1&viewtype=&category=

    to something like:

    /view_video.php?viewkey=f1586614cf2c1ad240b6

    and also advice me where to place it in the htaccess file.

    The current code is:

    Options +FollowSymLinks +Indexes

    Thanks,

    Jack.
     
    jack_sparrow, May 18, 2007 IP
  14. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #614
    For mydomain.com/? my only guess is

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\?\ HTTP/
    RewriteRule ^?$ http://www.domain.php/ [R=301,L]


    RewriteRule ^([^.]+)/([^.]+)\.html$ viewkey=$1&page=$2 [L]

    /view_video.php?viewkey=f1586614cf2c1ad240b6&page=1&viewtype=&category=

    to

    f1586614cf2c1ad240b6/1.html

    If you have more than one page, you'll have to have the page part in the URL.

    view_video.php?viewkey=f1586614cf2c1ad240b6 isn't search engine friendly at all.
     
    Nintendo, May 18, 2007 IP
  15. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #615
    Hi,

    The code for /? is giving a 500 error :(

    For the pages below, pls suggest me a proper Search engine friendly rewrite. Also, not one but there are approx 12000 + pages in this category.There are three diff url's which the script generates but all pointing to the same file. I have listed them below:

    /view_video.php?viewkey=31919438fcfc6bd5f199
    /view_video.php?viewkey=31919438fcfc6bd5f199&page=1&viewtype=&category=
    /view_video.php?viewkey=31919438fcfc6bd5f199&page=1&viewtype=&category=mr

    The viewkey=number_alphabet part is variable ( over 2500+) and also the category=alphabet part

    Also tell me how to put the code with reference to my current .htaccess codes.

    Thanks and much more,

    JS.
     
    jack_sparrow, May 19, 2007 IP
  16. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #616
    Options +FollowSymLinks +Indexes
    RewriteEngine On
    RewriteBase /
    RewriteRule ^view/([^.]+)/([^.]+)/([^.]+)\.html$ view_video.php?viewkey=$1&page=$2&viewtype=&category=$3 [L]
    RewriteRule ^view/([^.]+)/([^.]+)\.html$ view_video.php?viewkey=$1&page=$2&viewtype=&category= [L]
    RewriteRule ^view/([^.]+)\.html$ view_video.php?viewkey=$1 [L]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php?\ HTTP/
    RewriteRule ^(.*)index\.php?$ http://www.hotclips.mobi/$1 [R=301,L]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php?\?\ HTTP/
    RewriteRule ^(.*)index\.php?$ http://www.hotclips.mobi/$1& [R=301,L]
    RewriteCond %{HTTP_HOST} ^hotclips.mobi
    RewriteRule (.*) http://www.hotclips.mobi/$1 [R=301]
     
    Nintendo, May 19, 2007 IP
    Pammer likes this.
  17. davcheong

    davcheong Well-Known Member

    Messages:
    1,148
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    175
    #617
    davcheong, May 20, 2007 IP
  18. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #618
    For some unknown reason these codes are not working for any of the three url's.

    Any idea.. or changes required...

    Thanks,

    Jack.
     
    jack_sparrow, May 20, 2007 IP
  19. Xuxa

    Xuxa Banned

    Messages:
    248
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #619
    Very nice topic, this helped me a lot. I am currently using it on my site..
     
    Xuxa, May 20, 2007 IP
  20. mailryo

    mailryo Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #620
    Nintendo how do I do this ? Please help, I readed 20 pages of this forum and didn`t find the answer. Can`t read anymore, my eyes are dead :)

    I currently redirect http://subdomain.domain.com to http://12.12.12.12/dir/dir/dir and it works, it actually redirects to http://12.12.12.12

    but I want to keep:the http://subdomain.domain.com/dir/dir/dic in the address bar instead of going straight to http://12.12.12.12/dir/dir/dir



    The current .htaccess I got ( which makes the redirections of the subdomain to 12.12.12.12

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.domain.com$
    RewriteRule ^(.*)$ http://12.12.12.12 [R=301,L]


    Any help would be appreciated.. Thank you !
     
    mailryo, May 21, 2007 IP