Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Quick htaccess questions

    I got the following htaccess rewrite code from the canonical url tool on jackborn.com as a way of removing duplicate references to the same url. I think I know what this is doing in general terms but not what it does in detail. The aim is to have example.com, www.example.com, www.example.com/index.php and example.com/index.php all pointing to one place - www.example.com/

    My questions are:-

    a) Is it enough to test this by entering the various permutations in a web browser and checking that the url changes?
    b) Will this work for relative internal links too - i.e. links which just say index.php?

    # index.php to /
    RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
    RewriteRule (.*)index\.(php|html)$ /$1 [R=301,L]

    # force www.
    rewritecond %{HTTP_HOST} ^example.com [nc]
    rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

    Thanks

    Janice

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by K45ss View Post
    a) Is it enough to test this by entering the various permutations in a web browser and checking that the url changes?
    b) Will this work for relative internal links too - i.e. links which just say index.php?
    Yes, to both.

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you for your response.

    However, when I typed in http://www.example.com/index.php into the browser the site continued to show that whole url in the browser bar - does this mean that the redirect is not working as I want it to?

    Thanks

    Janice

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by K45ss View Post
    Thank you for your response.

    However, when I typed in http://www.example.com/index.php into the browser the site continued to show that whole url in the browser bar - does this mean that the redirect is not working as I want it to?

    Thanks

    Janice
    Make sure you turn the rewrite engine on:
    Code:
    RewriteEngine On
    
    # index.php to /
    RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
    RewriteRule .* / [R=301,L]
    
    # force www.
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteRule .* http://www.example.com%{REQUEST_URI} [R=301,L]
    I changed the code a little bit, to keep the URI when rewriting to www. That first rule will match index.php in subdirectories, too. Are you looking to have -all- index.php's redirect to "/"?

  • Users who have thanked Inigoesdr for this post:

    K45ss (01-05-2009)

  • #5
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks!

    I did have the rewrite engine on in my .htaccess.

    For the site I'm working on at the moment I don't have subdirectories but I can see that would be useful for sites in general (and probably future sites for me).

    I'm not sure what you mean by keep URI in your changes - does the version I have remove everything after .com even if people type in a longer string?

    thanks

    Janice

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by K45ss View Post
    I'm not sure what you mean by keep URI in your changes - does the version I have remove everything after .com even if people type in a longer string?
    Yeah, the second rule was stripping the folder when I tested it.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •