Htaccess help needed

What I’m trying to do is

  1. Avoid Duplicate Content
  2. I have reorganized my site so several pages have been moved to new locations.
  3. Those files have also been renamed and strutctured. For example - one page has been moved from http://www.example.com/example1.html to http://www.example.com/examples/index.html

That page i want to appear in the the URL bar as http://www.example.com/examples/ - with no .html extension. It just looks better to me.

Does this code look good? It does work. Is there anything I am missing? Is it in the correct order?

ErrorDocument 404 /404page.html
RewriteEngine on

Redirect pages

RewriteRule ^example/examples1\.html$ http://www.example.com/examples1/ [R=301,L]

Index Redirect back to bare folder URL

RewriteCond %{THE_REQUEST} [1]{3,9}\ /([^/]+/)index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)
)index\.(html?|php)$ http://www.example.com/$1 [R=301,L]

REDIRECT non-canonical domain to www

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

Thanks for any help you can give me.


  1. A-Z ↩︎

You just said the the new URL is http://www.example.com/examples, but your rule says http://www.example.com/examples[COLOR=“Red”]1[/COLOR]/, so that’s not correct. Rule itself is fine

The RewriteCond is superflous imho, you can replace these two lines with this one:


RewriteRule ^(([^/]+/)*)index\\.(html?|php)$ http://www.example.com/$1 [R=301,L]

I’m not really sure why you put [A-Z]{3,9} in the RewriteCond, so I just left it out.

No need to create an atom out of “www.example.com” in the RewriteCond, and you also don’t need the ? at the end:


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

The order of the rules seems fine to me :slight_smile:


  1. A-Z ↩︎

ScallioXTX WOW! That was a great help Thanks so much for your time.

If you dont mind I have 1 more question. If I have 15 pages i am relocating to 1 location how do I use wild card so that I only need 1 line.

Example

redirect 301 /products/red/red1.html http://www.example.com/products/red/
redirect 301 /products/red/red2.html http://www.example.com/products/red/
redirect 301 /products/red/red3.html http://www.example.com/products/red/
redirect 301 /products/red/red4.html http://www.example.com/products/red/
redirect 301 /products/red/red5.html http://www.example.com/products/red/

How do I turn this into 1 rewrite rule using a wildcard

RewriteRule ^products/“wildcard”\.html$ http://www.example.com/products/red/ [R=301,L]

That would be

RewriteRule ^products/red/red\d+\.html$ http://www.example.com/products/red/ [R=301,L]

\d+ matches 1 or more digit, so red1, red2, …, red20, red21, …, red9999999, you get the point :slight_smile:

If you want to use a restricted list of what it should match you can use

RewriteRule ^products/red/red(1|2|3)\.html$ http://www.example.com/products/red/ [R=301,L]

That would match red1, red2 and/or red3
You can to the list as you like.
That way you’re not doing a 301 redirect on pages that didn’t previously exist, but you would get a 404 on those.

Excellent!

Two things for me to comment on, though!

  1. DirectoryIndex selects the default script for Apache to serve when only the subdirectory/ is requested. Don’t forget to set that at least once (DocumentRoot) and reset for any subdirectory which uses a different prioritized list.

  2. Because mod_alias doesn’t use the regex engine, it’s preferable to use it if you don’t need the POWER of mod_rewrite so RewriteMatch 301 /products/red/red\d+\.html http://www.example.com/products/red/ should work like the “catch-all” ScallioXTX advised against (but even those should be redirected to red/ and it’s DirectoryIndex should select index.html for you there).

Regards,

DK