Chop out index.php for /

Discuss practical ways rearrange URLs using mod_rewrite.

Chop out index.php for /

Postby TeckniX » Fri Aug 15, 2008 9:29 am

I know I'm going to hate myself for it, but I can't seem to figure out what I'm doing wrong.

Code: Select all
RewriteCond %{REQUEST_URI} ^.*index\.php$
RewriteRule ^index\.php$ / [R=301,L]


I want all domain.com/index.php to be domain.com/

But if I have domain.com/index.php?myid=3 then keep it like that,
right now, it's doing domain.com/?myid=3

What am I doing wrong?
TeckniX
 
Posts: 38
Joined: Tue Mar 27, 2007 12:18 pm

Postby richardk » Sat Aug 16, 2008 3:22 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Make sure the query string is empty.
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^(.+/)?index\.php$ /$1 [NC,R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby TeckniX » Mon Aug 18, 2008 8:25 am

haha

Thanks again richardk.

it's funny how a weekend will help clear your mind - I actually implemented the exact same fix - An empty querystring will make sure that the index.php doesn't show up when not needed ;)
TeckniX
 
Posts: 38
Joined: Tue Mar 27, 2007 12:18 pm

Postby noob » Sat Sep 13, 2008 3:24 pm

Can someone help me please? I'm not good in regex and even worst in english.

I have put this in .htaccess
Code: Select all
Options +FollowSymlinks

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \?\ HTTP [NC]
RewriteRule .? http://www.mydomain.com%{REQUEST_URI}? [R=301,L]


Because that was only way to get rid of "?" in url, like this:
http://www.mydomain.com/index.php?

but after I added your code:

richardk wrote:Try
Code: Select all

# Make sure the query string is empty.
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^(.+/)?index\.php$ /$1 [NC,R=301,L]


all I get is loop:
Code: Select all
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

The browser has stopped trying to retrieve the requested item. The site is redirecting the request in a way that will never complete.

    * Have you disabled or blocked cookies required by this site?
    * NOTE: If accepting the site's cookies does not resolve the problem, it is likely a server configuration issue and not your computer.


Can someone tell me where is error, I'm not getting it?

With second code, I still get url like this:
http://www.mydomain.com/?

Thanks
noob
 
Posts: 1
Joined: Sat Sep 13, 2008 3:09 pm

Postby richardk » Mon Sep 15, 2008 10:53 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{THE_REQUEST} \?\ HTTP [NC]
RewriteRule .* http://www.example.com%{REQUEST_URI}? [R=301,L]

RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^(.+/)?index\.php$ http://www.example.com/$1 [NC,R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 23 guests

cron