Forum Moderators: phranque

Message Too Old, No Replies

Mod-rewrite to remove: index, www & .html combined

         

Gral

11:11 pm on May 11, 2011 (gmt 0)

10+ Year Member



Apologies I know there are loads of posts about mod-rewrite but I have been searching this forum and others for days to no avail. I have come back here as this forum has many helpful people and existing posts have helped with some answers. But everyone seems to have a slightly different scenario and I have exhausted my search.

Basically I would like to:
1. 301 redirect my .com domain to my .com.au domain. I own both domains and I want both to point to .com.au
2. remove www so the site would be [domain.com.au...]
3. remove index.html from the end of my homepage URL
4. set up extensionless URLs - I only have .html at the moment and don't envisage changing that as its a fairly basic site.

The code I have so far is:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\.au\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^www.domain.com.au$
RewriteRule ^/?$ "http\:\/\/domain\.com\.au" [R=301,L]

1. The rewrite .com to .com.au seems to be working fine.
2. Removing www works for the homepage, but if someone keys in www.domain.com.au/page1 the www remains. Can I get rid of all occurences of www and is it worthwhile?
3. I have looked around and I'm not sure how to remove index.html especially when combined with my other requirements
4. I have tried various bits of code from this site and can't get it to work. I'm not sure if its because of my .com.au domain?

My site is quite small and simple. I am willing to change the file organisation slightly if this is necessary. The structure at present is
homepage at domain.com.au
Then I have Page1, Page2 etc in the root folder appearing as domain.com.au/page1.html
Then I have folders for each page, containing subsidiary info for example
domain.com.au/page1/pageinfo3.html
domain.com.au/page3/pageinfo2.html

I thought this would be one of the most popular combinations of mod rewrites - essentially, I just want to tidy up the URLs for what is a simple html site. But I have had only partial success searching the forums.

Is anyone able to help?

g1smd

1:10 am on May 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Have you tried recent threads? I remember posting much of the code you will need at least several times in the past week.

Your proposed rule order will create a multiple step redirection chain for some requests. The non-www to www redirect should be the last redirect. The rewrite should appear after the last redirect.

lucy24

4:19 am on May 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$


can always be written as

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$


Oh, and you don't need to \ escape anything in the output, just in the input. So you've got that bit backward. The : and / don't need to be escaped at all.

Gral

12:11 pm on May 12, 2011 (gmt 0)

10+ Year Member



OK thanks I am going to try to build up to this gradually so I now have the following:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\.au\/" [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /tagword/([^/]+/)*index\.html
RewriteRule ^tagword/(([^/]+/)*)index\.html$ [domain.com.au...] [R=301,L]

RewriteCond %{HTTP_HOST} ^www.domain.com.au$
RewriteRule ^/?$ "http\:\/\/domain\.com\.au" [R=301,L]

That should get rid of www, index.html and divert .com to .com.au right?

Lucy thanks for the tip, I would love to simplify those 2 http and www lines into a single line of code but I'm not sure how with a .com.au domain.

g1smd, I assume you meant www to non-www ? Thanks I have now moved that to the last rewrite rule, and placed the index.html rule immediately before that.

Does anyone know if this code looks correct for a .com.au domain? and rules are in the optimal order?

Once I get this locked in I will move onto the broader remove .html issue.

Cheers

Gral

12:17 pm on May 12, 2011 (gmt 0)

10+ Year Member



oh and this still doesn't solve the problem for removing www for all pages. It only removes www for the homepage.

Any ideas on that?

For a brand-new site with no third party links pointing to my site (apart from links I can control) is that even necessary? I thought it might help for futureproofing to reduce the possibility of third parties setting up deep links to my sub-pages using a www.

g1smd

7:08 pm on May 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's still several errors in the code, especially do not escape anything in the target URL as mentioned above.

The rule order is still incorrect, and several of the patterns can be combined to be much more efficient as noted by lucy.

RegEx patterns should escape all literal periods.