This thread is a discussion for the wiki article: Remove index.php From URLs
   
1 of 2
1
Remove index.php From URLs
Posted: 04 June 2008 12:52 AM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  236
Joined  11-20-2007

I use the “include” method to remove index.php on my site. I was having serious difficulty linking to files in a real directory. To simplify somewhat, I was using code like this in .htaccess:

RewriteEngine on
RewriteCond
$1 ^(blog|search|site|P[0-9]{2,8}) [NC]
RewriteRule
^(.*)$ /index.php/$1 [L]


The problem was that the real directory is called “blog_files” so the above code was rewriting any request for a file from that directory.

It took me a long time (six months of occasional frustration), and eventually a nudge from Ingmar to look for the problem in .htaccess. I should have caught this right away, but sometimes the obvious is impossible to see.

So, here’s the lesson to be (re)learned: the ^ in the regex above means “begins with”. It is not an exact match. So, mod_rewrite was seeing the request to “blog_files”, matching it to “blog” and then rewriting it. The result was that I couldn’t link to files in that directory.

The solution was simple once I found the problem. I just added a slash to the end of all of the template group names in the RewriteCond that might match a real directory. So, the resulting code looks like this:

RewriteEngine on
RewriteCond
$1 ^(blog\/|search\/|site\/|P[0-9]{2,8}) [NC]
RewriteRule
^(.*)$ /index.php/$1 [L]


That way, it will only match if it sees “blog/” in the URL and it, therefore, won’t attempt to rewrite “blog_files/”.

I think that it is a good idea to do this just in case you might ever create a real directory that might match a template group name.

 Signature 

Tim


TVMCalcs.com - New and Improved! Now proudly powered by EE.

Profile
 
 
Posted: 07 July 2008 03:29 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  1
Joined  07-07-2008

I had my EE served from a sub directory, so the rules herein weren’t quite working.  I had to alter it ever so slightly and I thought other people might find it useful:

# BEGIN Expression Engine Rewrite       
RewriteEngine On
RewriteBase
/sub-directory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_FILENAME} !-d
RewriteRule
. /sub-directory/index.php [L]


# END Expression Engine Rewrite

This will check for existing directories and rewrite anything else.  I discuss it on my website [url=http://www.wil-linssen.com]http://www.wil-linssen.com[/url]

Profile
 
 
Posted: 08 July 2008 04:30 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  647
Joined  05-16-2004

Search, Comments and index.html

If you’re using EE for part of your site, but not for your home page, certain EE functions won’t work properly. 

RewriteCond %{THE_REQUEST} ^POST
RewriteCond
$1 ^$
RewriteRule ^(.*) /index.php/$1   [L]

This allows EE to handle post requests even if your home page is index.html.  But it won’t allow GET requests, like the email activation link in an email, to work.

TTFN
Travis

 Signature 

Fight spam better with “Defensio for EE,” a free module


Hop Studios Internet Consulting
http://www.hopstudios.com/

Profile
 
 
Posted: 25 July 2008 12:56 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  43
Joined  09-09-2007

Here are my rules that enable the mailing list activation emails to work as well… my server is one fo the ones that doesn’t seem to support PATH_INFO..

RewriteEngine on
RewriteCond
%{QUERY_STRING} ^(ACT=.*)$ [NC]
RewriteRule
^(.*)$ index.php?%1 [L]
RewriteCond
$1 !^(images|system|themes|library|favicon\.ico|robots\.txt|index.php) [NC]
RewriteRule
^(.*) /index.php?/$1 [L]

It is a modified version of the one on the wiki page that fixes the CSS linking issue…

See this thread for more info…

 Signature 

• Web Design & Development • Perth, Western Australia •
@michaelropernotmuch.tumblr.com

Profile
 
 
Posted: 09 August 2008 08:45 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  39
Joined  08-06-2008

Using the default recommended rewrite rules using .htaccess on Apache didn’t work for me. I found a forum post with the following recommendations which did the trick.

Try changing this line

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

to:

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

(note the ? after index.php)

Profile
 
 
Posted: 16 August 2008 12:22 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  1
Joined  07-12-2008

None of these are working for me on localhost using MAMP, which is allegedly supposed to be ready to go out of the box.

Mod-rewrite is loaded. Still nothing but 404 errors.

Profile
 
 
Posted: 26 August 2008 05:11 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  155
Joined  11-19-2007

For me what works best is

RewriteCond %{THE_REQUEST} !^POST
RewriteRule
^site/?(.*)$ /$1 [R=301,L]

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

In this two lines:

RewriteCond %{THE_REQUEST} !^POST
RewriteRule
^site/?(.*)$ /$1 [R=301,L]


The main template group is removed from the url and the browser redirects to /


In the next three lines:

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


If REQUEST_FILENAME is not a file nor a directory, then passes all arguments to index.php

The reason why it works best for me is because I don’t have to worry about editing the .htaccess file every time I add a template group or a top level section.

If the url points to something existent on the server it serves it otherwise it will serve index.php


Now there were times I had EE in a subdirectory.

If the .htaccess is in the subdirectoy with EE, i.g.

/subdir/.htaccess
/subdir/index.php

it looks like this:

RewriteCond %{THE_REQUEST} !^POST
RewriteRule
^site/?(.*)$ /subdir/$1 [R=301,L]

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

But if the .htaccess is at the top level directoy and EE in a subdirectory, i.g

/.htaccess
/subdir/index.php

RewriteCond %{REQUEST_URI} !^subdir/
RewriteCond %{THE_REQUEST} !^POST
RewriteRule
^subdir/index.php/?(.*)$ /$1 [R=301,L]
RewriteRule
^site/?(.*)$ /$1 [R=301,L]

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


Here is a bonus, since I find it very useful.

The following lines forces a subdomain. Its mostly used to add or remove www from the url.

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

 Signature 

http://www.bbook.com

Profile
 
 
Posted: 26 August 2008 08:49 PM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  276
Joined  06-12-2002

Please make sure to add this to the actual wiki entry, too, bkuberek! This is way useful!

Profile
 
 
Posted: 26 August 2008 10:57 PM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  155
Joined  11-19-2007
alienne - 26 August 2008 08:49 PM

Please make sure to add this to the actual wiki entry, too, bkuberek! This is way useful!

Force www

[EDIT]: I just found the actual wiki page. I added it to the .htaccess category to make it easier to find.
http://expressionengine.com/wiki/Remove_index.php_From_URLs/

 Signature 

http://www.bbook.com

Profile
 
 
Posted: 28 August 2008 11:09 AM   [ Ignore ]   [ # 9 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  111
Joined  07-02-2008

If you are wondering why Expression Engine is injecting a question mark (?) into the url

e.g.

http://mysite.com/?/about-us

Make sure you are not Forcing URL query strings.

To check/change this make your way to:

Admin ›  System Preferences ›  Output and Debugging Preferences

and set “Force URL query strings” to no.  If you’re using the MSM be sure to set this on each individual site.

WARNING

This may break your site if your server environment does not support the PATH_INFO variable.

 Signature 

View Creative Agency | http://www.viewcreative.co.uk

Profile
 
 
Posted: 28 August 2008 11:12 AM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  111
Joined  07-02-2008

If your server environment does not support PATH_INFO variable you may still be able to remove “Forece URL query strings” with this Workaround for Forced Query Strings

Hope this saves somebody some stress!

 Signature 

View Creative Agency | http://www.viewcreative.co.uk

Profile
 
 
Posted: 28 August 2008 11:36 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  155
Joined  11-19-2007

Thanks viewcreative

I had to find this information a long time ago when I ran through this problem. This should be a wiki page as well I believe. It would saved me a lot of time if I didn’t have to search all forums.

 Signature 

http://www.bbook.com

Profile
 
 
Posted: 03 September 2008 11:56 AM   [ Ignore ]   [ # 12 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  155
Joined  11-19-2007

Actually I came over to a stop on BlueHost.com when setting up an addon domain.

Options +FollowSymLinks
RewriteEngine on

RewriteBase
/

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


If I I use redirect rather than a rewrite it will redirect to the right location:

RewriteRule ^(.*)$ /index.php/$1 [R,L]


The above code works fine for the main account domain (public_html) but not for addon domains (public_html/addon_domain.com/)

Has anybody experienced this?

 Signature 

http://www.bbook.com

Profile
 
 
Posted: 04 September 2008 03:31 PM   [ Ignore ]   [ # 13 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  155
Joined  11-19-2007

Problem:
The RewriteRule is not working!


In some shared hosts the configuration of Apache can affect the Rewrites.

Apache Core Features - AcceptPathInfo Directive

“The treatment of requests with trailing pathname information is determined by the handler responsible for the request. The core handler for normal files defaults to rejecting PATH_INFO requests. Handlers that serve scripts, such as cgi-script and isapi-handler, generally accept PATH_INFO by default.”

The solution:

add a question mark (?) right after index.php

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

 Signature 

http://www.bbook.com

Profile
 
 
Posted: 09 September 2008 07:50 AM   [ Ignore ]   [ # 14 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1077
Joined  01-24-2006

For those of you who want to use the include method AND automatically generate the template groups / page urls I have created LG .htaccess Generator! Check it out.

 Signature 

VOTE FOR LG Addons in the Mashable Open Web Awards!


Newism - Newcastle Web Design & Development


LG Better Meta now w/ Sitemap Meta & XML Generator | LG Polls | LG .htaccess Generator

Profile
 
 
Posted: 09 September 2008 08:49 AM   [ Ignore ]   [ # 15 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  155
Joined  11-19-2007

Great extensions you have made. Thanks. Let me ask you since you seem to know more about htaccess,

What is the advantage or disadvantage of using either the “Include” method or the “File and Directory Check” Method?

Thanks

 Signature 

http://www.bbook.com

Profile
 
 
Posted: 09 September 2008 06:34 PM   [ Ignore ]   [ # 16 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1077
Joined  01-24-2006

First I’ll start by saying that I’m no .htacess genius and follow that by saying I can’t provide .htaccess support as issues generally are site and server specific.

So the file and driectory check method. From the wiki:

Important note for SEO:  This method will serve all ExpressionEngine pages with a code 200 - which means that Error Code 404 - Not Found will never be delivered, even if EE is set to use it.  The other methods may be more SEO friendly, though fiddler to work with.

 Signature 

VOTE FOR LG Addons in the Mashable Open Web Awards!


Newism - Newcastle Web Design & Development


LG Better Meta now w/ Sitemap Meta & XML Generator | LG Polls | LG .htaccess Generator

Profile
 
 
Posted: 06 November 2008 01:58 PM   [ Ignore ]   [ # 17 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  425
Joined  02-28-2008

Hey Leevi,
I gave the extension a try…resulted in “500 Internal Server Error”.
Any hints to what may be causing it?

# -- LG .htaccess Generator Start --

# .htaccess generated by LG .htaccess Generator v1.0.0
# http://leevigraham.com/cms-customisation/expressionengine/addon/lg-htaccess-generator/

# secure .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

# Dont list files in index pages
IndexIgnore *

# EE 404 page for missing pages
ErrorDocument 404 /index.php?/

# Simple 404 for missing files
<FilesMatch "(\.jpe?g|gif|png|bmp)$">
  
ErrorDocument 404 "File Not Found"
</FilesMatch>

RewriteEngine On

RewriteBase
/

# remove the www
RewriteCond %{HTTP_HOST} ^(www\.$) [NC]
RewriteRule
^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]

# Remove index.php
# Uses the "include method"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_URI} ^/(faq|search|main|includes|tutorials|downloads|links|mycontent||members|P[0-9]{2,8}) [NC]
RewriteRule
^(.*)$ /index.php?/$1 [L]

# Remove IE image toolbar
<FilesMatch "\.(html|htm|php)$">
  
Header set imagetoolbar "no"
</FilesMatch>

# -- LG .htaccess Generator End --

On the other side manually adding template group names (include list method) and using:

RewriteEngine On
RewriteCond
$1 ^(member|faq|search|main|includes|tutorials|downloads|links|mycontent|P[0-9]{2,8}) [NC]
RewriteRule
^(.*)$ /index.php?/$1 [L]

works just fine.

Thank you for your time!

Profile
 
 
Posted: 06 November 2008 04:47 PM   [ Ignore ]   [ # 18 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1077
Joined  01-24-2006

It’s because there is a double pipe on line:

RewriteCond %{REQUEST_URI} ^/(faq|search|main|includes|tutorials|downloads|links|mycontent||members|P[0-9]{2,8}) [NC]

I’m guessing this is because you don’t have any pages in your site.

 Signature 

VOTE FOR LG Addons in the Mashable Open Web Awards!


Newism - Newcastle Web Design & Development


LG Better Meta now w/ Sitemap Meta & XML Generator | LG Polls | LG .htaccess Generator

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 65085 Total Logged-in Users: 38
Total Topics: 82223 Total Anonymous Users: 18
Total Replies: 441916 Total Guests: 219
Total Posts: 524139    
Members ( View Memberlist )
Newest Members:  BombermanhaimtuagocsadamVeNeaDoRHildegaardhrtrulzUNFORGIVEN IIIEmmanuelYanYanTomsB