Friendly URLs: Redirecting everything to index.php

Discuss practical ways rearrange URLs using mod_rewrite.

Friendly URLs: Redirecting everything to index.php

Postby Seguer » Mon Jan 19, 2009 6:47 pm

Hi, I've been trying to use mod_rewrite to rewrite urls such as /folder1/folder2/folder3 (ad infinitum) all to index.php so that php can handle the delivering of content.

My current rules look like this:

Code: Select all
RewriteCond %{HTTP_HOST} ^127.0.0.1/(.*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !(\.(gif|jpg|jpeg|png|css|js)$|index.php$) wbcms/ [L]


The first condition is just for use in my development area (I need to rewrite differently on different hosts), the next two were trying to stop the resulting 404s.

Currently if I go to 127.0.0.1/wbcms/ it works, but if I go to 127.0.0.1/wbcms/test, Apache will complain that /test doesn't exist (404) instead of rewriting it.

Basically I want everything to be rewritten and sent to wbcms/ or / (on a different host) so that my index.php can handle everything.

Any ideas? Thanks.
Seguer
 
Posts: 4
Joined: Mon Jan 19, 2009 6:42 pm

Postby richardk » Tue Jan 20, 2009 3:49 am

HTTP_HOST is the domain/IP address only.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^127\.0\.0\.1$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !^(.+\.(gif|jpe?g|png|css|js)|index\.php)$ /wbcms/ [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Seguer » Tue Jan 20, 2009 3:57 am

That gave me error 500's again, and attempts to modify it resulted in more 500s or Apache telling me it couldn't find /wbCMS/wbcms

I managed to get something working myself today;

Code: Select all
#---- dev
RewriteBase /wbcms
RewriteRule !\.(gif|jpe?g|png|css|js) index.php [L,NC]


But this means I'd need to change the RewriteBase on a live server. Currently the above does what I need (on my dev platform)
Seguer
 
Posts: 4
Joined: Mon Jan 19, 2009 6:42 pm

Postby richardk » Tue Jan 20, 2009 12:50 pm

What does you error log say the 500 error is caused by?
Try removing the Options line.
Are you rewriting to /wbcms/index.php?

Also try (with and without the Options line)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^127\.0\.0\.1$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !^.+\.(gif|jpe?g|png|css|js)$ /wbcms/index.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Seguer » Tue Jan 20, 2009 3:16 pm

Too many internal redirects, but that coded you posted works! Cheers. In order to get it working for both my dev and live I'm assuming I just need to change the HTTP_HOST part and rewrite to /index.php (for my own example). Can't test this right now but that should work having both sets of rules, correct?
Seguer
 
Posts: 4
Joined: Mon Jan 19, 2009 6:42 pm

Postby richardk » Tue Jan 20, 2009 4:03 pm

That should work.

Or you can try this in one rule
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
# The path for localhost.
RewriteCond %{HTTP_HOST}/wbcms/index.php ^(1)27\.0\.0\.1(/wbcms/index\.php)$ [NC,OR]
# The path for example.com and www.example.com
RewriteCond %{HTTP_HOST}/index.php ^(www\.)?example\.com(/index\.php)$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !^.+\.(gif|jpe?g|png|css|js)$ %2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Seguer » Tue Jan 20, 2009 4:32 pm

Cool, thanks. I'll give that a shot when I test on my live server!
Seguer
 
Posts: 4
Joined: Mon Jan 19, 2009 6:42 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 24 guests

cron