Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Closed Thread
 
Thread Tools Display Modes
Share |
  #1  
Old 01-19-2014, 09:41 AM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
htaccess redirect ONLY domain/index.php to domain root

I want to redirect www.example.com/index.php to www.domain.com/ without affecting any other files or directories.

I already have a rule for redirecting non-www to www.

I have searched and read until my brain is clogged. I found this, which seems to be overkill :
Code:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]
This seems better, but I still think it could be improved :
Code:
RewriteCond %{THE_REQUEST} ^.*/index.php RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]
The problem I'm having with the last one is that I don't know exactly what ^.* does, and if it does something I don't need, how would I get rid of it? I'm trying to learn more about htaccess and keep my file as clean, short, and concise as possible.

Last edited by HTMLBasicTutor; 01-19-2014 at 11:44 AM.
 

Advertisement

Advertisement

  #2  
Old 01-19-2014, 10:28 AM
Lahori_Munda's Avatar
Lahori_Munda Lahori_Munda is offline
Contributing Member
 
Join Date: 07-02-10
Location: Pakistan
Posts: 169
iTrader: 0 / 0%
How's that

Quote:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com/index.php$
RewriteRule ^$ http://www.example.com/ [L,R=301]
 
  #3  
Old 01-19-2014, 11:21 AM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
Not sure why, but that didn't work. I went back to using the 2nd example I gave earlier, for now, but I'm just curious if there's a better, more precise way to do this.

(and still wondering exactly what ^.* does)
 
  #4  
Old 01-19-2014, 11:35 AM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
Argh. I had to take it completely out - the 2nd example was also redirecting index.php when it was in a sub-directory (which is what I was worried it might do), and I cannot have that. So I'm still stuck.
 
  #5  
Old 01-19-2014, 02:07 PM
ScriptMan's Avatar
ScriptMan ScriptMan is online now
Super Moderator
 
Join Date: 02-10-07
Location: Central Kentucky
Posts: 13,300
iTrader: 4 / 100%
Quote:
Originally Posted by Katt View Post
Argh. I had to take it completely out - the 2nd example was also redirecting index.php when it was in a sub-directory (which is what I was worried it might do), and I cannot have that. So I'm still stuck.
I don't know what it does in this voodoo stuff but I know it is required. Any changes you make in the root but be countermanded in child directories if you don't want them to be affected.

I'm not sure you need a conditional statement to perform this operation; however my first 3 attempts to write the correct redirect did not work. I will fight with it a bit more later.
 
  #6  
Old 01-19-2014, 03:26 PM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
I appreciate you trying!

I initially thought a straight-forward redirect would work. Maybe I did something wrong but it didn't do anything. Of course every example I found used only the pagename (instead of the full url) being redirected so I made several attempts along this line before I gave up and decided I needed a rewrite instead :

Code:
Redirect 301 ^example.com/index.php http://www.example.com/
I might take another stab at redirecting instead.
 
  #7  
Old 01-19-2014, 03:54 PM
ScriptMan's Avatar
ScriptMan ScriptMan is online now
Super Moderator
 
Join Date: 02-10-07
Location: Central Kentucky
Posts: 13,300
iTrader: 4 / 100%
I just figured out that part of my problem was FF. After breaking a site on localhost it did not come back with a correction to the .htaccess file. I had to clear recent everything in rencent history to get back to the site. Because of this I suggest testing with a 302 until you get it working.

I may not do much tonight but I will work on this some more.
 
  #8  
Old 01-19-2014, 04:03 PM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
I don't have a local apache box to test with, only my live sites on a shared host, so I really appreciate your help.

Even if you don't find a solution, you rock just for trying! Thank you!

This situation has frustrated me to the point where I may just buckle down and finally learn all this htaccess voodoo lol - hey maybe we'll learn something together here.
 
  #9  
Old 01-20-2014, 07:11 PM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
Wow - much googling and hair-pulling later, I finally got this to work with this (almost the same as my first example but not quite) :

Code:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ RewriteRule ^index\.php$ http://www.example.com/$1 [R=301,L]
It works, and doesn't affect sub-directories, so I guess I will let it be. Thank you for your kind attempts to help, I really appreciate it!
 
  #10  
Old 01-20-2014, 08:00 PM
ScriptMan's Avatar
ScriptMan ScriptMan is online now
Super Moderator
 
Join Date: 02-10-07
Location: Central Kentucky
Posts: 13,300
iTrader: 4 / 100%
Nice to know you got it working. I got blind sided today on several other things.
 
  #11  
Old 01-21-2014, 06:39 AM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
So you had a typical Monday then, right? LOL Mondays should be outlawed.

Thanks again for your help!
 
  #12  
Old 01-21-2014, 07:23 AM
ScriptMan's Avatar
ScriptMan ScriptMan is online now
Super Moderator
 
Join Date: 02-10-07
Location: Central Kentucky
Posts: 13,300
iTrader: 4 / 100%
Quote:
Originally Posted by Katt View Post
So you had a typical Monday then, right? LOL Mondays should be outlawed.
Yep and I'm retired. It never gets better.

I once tried taking Mondays off for and entire month learned about terrible Tuesdays .
 
  #13  
Old 01-21-2014, 07:25 AM
jumpen jumpen is offline
Junior Member
 
Join Date: 12-30-13
Location: KCMO
Posts: 29
iTrader: 0 / 0%
I can't yet give you the site that I'm quoting this from, but:
Quote:
The caret, ^, signifies the start of an URL, under the current directory. This directory is whatever directory the .htaccess file is in. You’ll start almost all matches with a caret.
Quote:
.* together yields "any character, 0 or more times"
I saw that you figured it all out, but figured you may still be wondering what the ^.* does.
 
  #14  
Old 01-21-2014, 07:46 AM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
Hey thanks jumpen! Then maybe I should try this instead of the rewrite :

Code:
Redirect 301 ^.*example.com/index.php http://www.example.com/
I know I tried it with just the caret, and it did nothing, but I'm not sure I ever added the .* - thank you very much for the explanation!
 
  #15  
Old 01-21-2014, 08:43 AM
jumpen jumpen is offline
Junior Member
 
Join Date: 12-30-13
Location: KCMO
Posts: 29
iTrader: 0 / 0%
Because the ^.* implies the start of your URL, you could get rid of example.com in the first part of your redirect. I think it'll work both ways though.

Redirect 301 ^.*index\.php$ (variable)

I think you'll have to escape the period(s) inside the first part of the expression because it is regex, it'll be read as a special character.

Because I can't type URL's yet, the (variable) should be replaced with the URL that you are redirecting to.
 
  #16  
Old 01-21-2014, 07:40 PM
Katt's Avatar
Katt Katt is offline
Moderator
 
Join Date: 12-14-13
Location: Texas
Posts: 1,563
iTrader: 0 / 0%
so you think

Code:
Redirect 301 ^.*index\.php$ http://www.example.com/
will redirect the index.php in the root only, without affecting subdirectories? I thought I would need the full url there. I'm willing to try it, next time I'm working on the site during off-hours. I'll update here when I do. Thanks!
 
  #17  
Old 01-21-2014, 08:39 PM
jumpen jumpen is offline
Junior Member
 
Join Date: 12-30-13
Location: KCMO
Posts: 29
iTrader: 0 / 0%
To only affect the directory the .htaccess file is in, get rid of the .*

Code:
RewriteEngine on RewriteCond %{THE_REQUEST} ^.*/index.html RewriteRule ^/index.html$ h++p://yoursitedotcom/ [R=301,L]
 
  #18  
Old 01-22-2014, 01:38 AM
yogadull yogadull is offline
Contributing Member
 
Join Date: 01-22-14
Posts: 54
iTrader: 0 / 0%
hi, the easy way is using cpanel, if you have it. choose redirection option in cpanel and select the target url. that's it
 
  #19  
Old 01-22-2014, 02:33 AM
jumpen jumpen is offline
Junior Member
 
Join Date: 12-30-13
Location: KCMO
Posts: 29
iTrader: 0 / 0%
Sorry mods, I need to edit my above post It should read:

Code:
RewriteEngine on RewriteCond %{THE_REQUEST} ^.*/index\.html RewriteRule ^index\.html$ h++p:yoursitedotcom/ [R=301, L]
Don't forget to escape the .'s and there is no leading backslash before index in the RewriteRule directive.
If you omit the R=301 flag it will default to R=302.
 
Go Back   Webmaster Forum > Web Development > Coding Forum

Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need .htaccess code to redirect SSL root to SSL sub-domain vectro Coding Forum 7 02-10-2013 05:08 PM
Redirect all pages of old site to new domain root *without parameters* Bernard Coding Forum 12 11-02-2011 06:58 AM
domain.com vs. domain.com/index.php/html redirect juliensimon SEO Forum 3 09-27-2010 08:15 PM
redirect from index.php to root gullsinn Coding Forum 6 03-09-2010 04:43 PM
htaccess/mod rewrite help to redirect subdomain to a domain Thanol Coding Forum 0 07-19-2004 09:36 AM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 07:59 PM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC