Forum Moderators: phranque

Message Too Old, No Replies

htaccess help - remove index.php from URL

htaccess help - remove index.php from URL

         

webdrv

12:02 am on Mar 4, 2012 (gmt 0)

10+ Year Member



I am not able to remove index.php from URL with the htaccess code given below. Could anyone please help with this. Thanks.


#######################################
# 1. Remove index.php
# 2. Redirect all example.com to www.example.com/
# 3. Enforce trailing SLASH on HTTP
# 4. Enforce trailing SLASH on HTTPS and force to www.example.com/secure/
# 5. Enfore HTTP for non /secure/ folder and redirect to www.example.com
# 6. Enfore HTTPS for /secure/ folder and redirect to www.example.com/secure/
#######################################
Options -Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /

#--------------------------------------
# 1. Remove index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [NC]
#tried the following also
#RewriteRule ^(.*)$ /index.php/$1 [NC]
#--------------------------------------


#--------------------------------------
# 2. Redirect all example.com to www.example.com
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/ [NC]
#--------------------------------------

#--------------------------------------
# 3. Enforce trailing SLASH on HTTP
RewriteCond %{HTTPS} off
#if you want to exclude index.php files, otherwise comment the following line
#RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/ [NC]
#--------------------------------------

#--------------------------------------
# 4. Enforce trailing SLASH on HTTPS
RewriteCond %{HTTPS} on
#The RewriteCond %{HTTPS} on portion may not work for all web servers.
##RewriteCond %{HTTP:X-Forwarded-SSL} on
#if you want to exclude index.php files, otherwise comment the following line
#RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_URI} !/secure/favicon\.ico$
RewriteRule ^(.*)$ [example.com...] [NC]
#--------------------------------------

#--------------------------------------
# 5. Enfore HTTP for non /secure/ folder and redirect to www.example.com
RewriteCond %{HTTPS} on
#The RewriteCond %{HTTPS} on portion may not work for all web servers.
##RewriteCond %{HTTP:X-Forwarded-SSL} on
RewriteCond %{REQUEST_URI} !^/secure/?.*$
RewriteRule ^(.*)$ http://www.example.com/ [NC]
#--------------------------------------

#--------------------------------------
# 6. redirect all http traffic to https, if it is pointed at /secure
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/secure/?.*$
RewriteCond %{REQUEST_URI} !/secure/favicon\.ico$
RewriteRule ^(.*)$ [example.com...] [NC]
#--------------------------------------

g1smd

12:42 am on Mar 4, 2012 (gmt 0)

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



There are a large numbers of issues with the code with some of the rules in the wrong order, incorrect or missing syntax, and other things. I'll go into more detail tomorrow, but suffice to say for the moment all of those issues have already been covered in a several recent threads.

webdrv

1:28 am on Mar 4, 2012 (gmt 0)

10+ Year Member



I am not sure where to start. I really appreciate your help.

lucy24

5:20 am on Mar 4, 2012 (gmt 0)

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



RewriteRule ^(.*)$ /index.php?/$1 [NC]

Immediate response: If you wanted to add "index.php" whether or not it's there already, this would be the right way to do it. But I don't perfectly understand why you want your query string to start with a slash / Or, in the alternative version, why you want "index.php" to be followed by more stuff. I think g1 explained this once. But he hasn't explained it several hundred times in the past month, so I'll allowed to forget.

Are you trying to adapt a boilerplate htaccess that came with your system? Make sure your #comments always say what you are doing, not what the original rule did before you changed it.

3. Enforce trailing SLASH on HTTP


Uhm... Why? Directories have slashes. Files don't. Protocol has nothing to do with it. Does your site consist entirely of directories? Or-- if you're rewriting everything-- do you want your users to think that the site et cetera?

Meanwhile, start reading earlier threads in this forum. When you start getting a sense of "I think I've read this identical question seventeen times before", you can do some more work on your htaccess.

Start by paying attention to the [L] flag. I don't see a single one in the originally posted code, which could lead to fairly calamitous results.

webdrv

10:23 am on Mar 4, 2012 (gmt 0)

10+ Year Member



Hi,
I am trying to do the following:
1. URL has index.php with parameters then no change
2. URL has index.php with no parameters, then remove it

Example:
1.
INPUT: http://www.example.com/index.php?a=2
OUTPUT: http://www.example.com/index.php?a=2

2.
INPUT: http://www.example.com/index.php
OUTPUT: http://www.example.com/

Thanks.

g1smd

11:00 am on Mar 4, 2012 (gmt 0)

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



Are you aware that http://www.example.com/?a=2 is also a perfectly valid URL and as it is shorter it is the preferred option?

Do the links on your page point to the URLs that you want people to "see" and "use"? Be aware that mod_rewrite cannot change the URLs in your links.

lucy24

8:55 pm on Mar 4, 2012 (gmt 0)

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



1. URL has index.php with parameters then no change
2. URL has index.php with no parameters, then remove it

Aha. Then you need a RewriteCond looking at the Query String. I just (re)posted the Query String boilerplate within the last day or two, so a quick search should turn it up. All you need is a minimalist

.

meaning "It exists".

Your examples make it look as if you also want to remove the query, leaving you with across-the-board Duplicate Content. It may help to backtrack a little bit and explain-- in English-- what your final goal is.

webdrv

9:06 pm on Mar 4, 2012 (gmt 0)

10+ Year Member



hi g1smd, lucy24, thank you for your comments. It makes sense. Is the following a recommended solution? This completely removes the index.php from the URL. I am not sure why the last line was provided (http://www.programmingfacts.com/how-to-remove-index-php-from-url-using-htaccess-mod_rewrite/). Thanks.


RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*)index\.php(.*) /$1$2 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET

webdrv

9:11 pm on Mar 4, 2012 (gmt 0)

10+ Year Member



This works, but can you tell me whether it is recommended to use this solution? what does the last command line do?

Revised:
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET

g1smd

9:15 pm on Mar 4, 2012 (gmt 0)

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



The last line is a separate condition for a following rule that hasn't been included.

The (.*) before index.php is the wrong pattern.

The (.*) after index.php is redundant because RewriteRule cannot see parameters.

The GET.* pattern is very inefficient. A better pattern has been posted here many times before.

The trailing .* after system/ is redundant.

The redirect target should include the protocol and domain.

webdrv

9:27 pm on Mar 4, 2012 (gmt 0)

10+ Year Member



Thank you. could you please provide me a link for the better pattern?

g1smd

9:39 pm on Mar 4, 2012 (gmt 0)

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



[google.com...] [site:webmasterworld.com/apache THE_REQUEST [A-Z] 3 9]

Look for /([^/]+/)* or similar.

[edited by: g1smd at 9:40 pm (utc) on Mar 4, 2012]

webdrv

9:40 pm on Mar 4, 2012 (gmt 0)

10+ Year Member



so far, I found in this forum, but it does not work yet. [webmasterworld.com...]

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

g1smd

9:42 pm on Mar 4, 2012 (gmt 0)

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



After {3,9}\ you need /( as in {3,9}\ /(

webdrv

9:59 pm on Mar 4, 2012 (gmt 0)

10+ Year Member



thank you so much. I have changed it as below. It only removes when there is no parameters. Is it possible to change this to remove "index.php" when the parameters present to get a short url?

"/index.php" becomes "/"
"/index.php?a=2" stays as "/index.php?a=2" instead of "/?a=2"


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

g1smd

10:06 pm on Mar 4, 2012 (gmt 0)

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



Yeah, you'll need to add extra code before the \ HTTP bit.

There's quite a few examples of that in this forum.

Make sure you use the right pipe characters in your code. There are two on the keyboard, and this forum shows the wrong one on some old posts. Manually edit ¦ to | here.

webdrv

10:28 pm on Mar 4, 2012 (gmt 0)

10+ Year Member



thank you. the following is working. Is it safe to use this way?


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

webdrv

10:30 pm on Mar 4, 2012 (gmt 0)

10+ Year Member



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

webdrv

10:39 pm on Mar 4, 2012 (gmt 0)

10+ Year Member



sorry, the above did not work. The following syntax works so far. Is it safe this way?

thank you. the following is working. Is it safe to use this way?


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

g1smd

10:40 pm on Mar 4, 2012 (gmt 0)

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



Guesswork on the code will lead to you opening your site for hackers.

RewriteRule cannot see query strings, so the new bit after "html" and before "$" is not needed.

The RewiteCond can see the query string, but (([^/]+/)*) is not the right pattern to match a question mark followed by characters, equals sign and optional ampersand.

You need to get comfortable with how Regular Expressions work. They will come up again and again in many programming and coding tasks.

webdrv

10:50 pm on Mar 4, 2012 (gmt 0)

10+ Year Member



I am new to this. Is there a pay service I could use to submit my full htaccess file and to get it revised and hardened? Thanks.

lucy24

4:24 am on Mar 5, 2012 (gmt 0)

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



Do you even need the full Request? Seems like an unanchored
index\.php
by itself would do. Matter of fact, that's how I deal with evil robots without throwing out auto-indexing.

If you don't spell out the remainder of the Request, then it doesn't matter what-- if anything-- comes after the index.php.