0

I'm looking for an ".htaccess" file solution to rewrite requested addresses ending in both "./index.html" and "./index" down to only the site root or subfolders, such as "http://www.mydomain.com/index[.html]" down to only "http://www.mydomain.com/". I've found some help with the "index.html" issue, but I'm having a hard time making the solution work for the "./index"-only request. I'm using the following code with my attempt at the "./index"-only piece commented out:

# MOD_REWRITE IN USE
Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# Redirect "index" page requests to either root or directory

# These first two lines work for "./index.html"...
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://%{HTTP_HOST}/ [R=301,L]

## These three lines don't work, last two are alternates of one another...
##  RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index*\ HTTP/
##  RewriteRule ^index*$ http://%{HTTP_HOST}/ [R=302,L]
##  RewriteRule ^index*$ $1 [R=302,L]

# Hide .html extension - additional information

# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]

# To internally forward /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L]

(The latter portion is for hiding the ".html" endings, as noted, and may superfluous, but I included the whole script, as is.) I don't know the version of Apache server, but any assistance would be greatly appreciated. Thanks.

cmc.tech
  • 21
  • 1
  • 3
  • the first `RewriteRule` along with the condition works fine for me. What exactly is the problem you're running into? – Jon Lin Jul 09 '13 at 08:19
  • Thanks for checking. I'm trying to get the commented code behind the double #'s, right below the first rewrite, to match both the "/index", and, possibly, "/index.html", conditions and rewrite the URL the same as the first rewrite does. Otherwise, if I could just get the "/index" condition to match and rewrite to "/", I wouldn't mind having two conditions with rewrites, but I can't get anything I've tried to handle the "/index" condition, at all. – cmc.tech Jul 09 '13 at 21:40

2 Answers2

2

I've managed to answer my own question with the help of a link that appeared on this page, after posting, combined with an off-center web search for a specific definition. Here are the two links: StackOverflow - Apache .htaccess to redirect index.html to root,... and Media Temple Support/Knowledgebase - Using .htaccess rewrite rules - .htaccess. The new code looks like this:

## MOD_REWRITE in use ##

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

## Redirect index page requests to either root or directory ##

RewriteCond %{THE_REQUEST} ^.*\/index.*\ HTTP/
RewriteRule ^(.*)index.*$ "/$1" [R=301,L]

## Hide .html extension ##

## To externally redirect /dir/foo.html to /dir/foo...

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]

## To internally forward /dir/foo to /dir/foo.html...

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L]

The new first rewrite/condition could be modified to look more like the original first one, by substituting

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

in place of the

RewriteCond %{THE_REQUEST} ^.*\/index.*\ HTTP/
RewriteRule ^(.*)index.*$ "/$1" [R=301,L]

but I don't know why you might do that, since the latter works just fine, with less text. Perhaps I miss something - but if it ain't broke, don't fix it.

I haven't been able, as yet to nail down where the original first rewrite/condition came from, but it does what it is designed for, though I was looking for something a little different. I hope all this gibberish makes sense to those looking for this type of solution. Thank you to "Jon Lin" and "Bip" for responding in an effort to help, but it's, also, nice when hours of search and re-search pay off, unexpectedly. Now my site behaves as it should, as will others. Thanks guys!

Community
  • 1
  • 1
cmc.tech
  • 21
  • 1
  • 3
  • I should add that the final version of the "index" redirect part will, also, work for "index.php", for example, or any other "index[.whatever]" request, if this is not obvious at first glance. – cmc.tech Jul 11 '13 at 02:50
0

Check it here i think, its your problem. Also you can check this

Community
  • 1
  • 1
Bip
  • 697
  • 2
  • 10
  • 29
  • 1
    This answer is not clear to me, since both links offered refer to IIS configuration on a Windows Server OS, not htaccess configuration on a Linux server. Not sure what you intended here. – cmc.tech Jul 09 '13 at 21:31