Converting "example.php/1/2/3/" to "example/1

Discuss practical ways rearrange URLs using mod_rewrite.

Converting "example.php/1/2/3/" to "example/1

Postby cosmo » Thu May 15, 2008 7:34 pm

Hi everyone,

I'm sure you've all seen sites with URLs like: http://www.mydomain.com/some_page.php/category1/page2

I was wondering if it was possible to have it but without the .php extension. I already have functions in my .htaccess file to hide .php extensions. It works perfectly everywhere, except when there is a trailing slash after it. How can I make it work? By the way, the 1/2/3/ is just a fancy Clean URLs-like way for me to pass in variables in a PHP file. I use the explode function to accomplis this in PHP, but anyway.

Here is the .htaccess code I've been using:

Code: Select all
## Enable mod_rewrite, start rewrite engine
RewriteEngine on
RewriteBase /

## Converts test.php to test
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]



Please help, I've tried searching on this forum and googling like mad for several days and I came up with nothing. Thanks again!
cosmo
 
Posts: 4
Joined: Thu May 15, 2008 6:17 pm

Postby cosmo » Fri May 16, 2008 7:02 am

By the way, I'm sorry about the title of this thread.

It's supposed to read "Converting "example.php/1/2/3/" to "example/1/2/3/" instead.
cosmo
 
Posts: 4
Joined: Thu May 15, 2008 6:17 pm

Postby richardk » Fri May 16, 2008 4:42 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)(/(.*))?$ /$1.php/$3 [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby cosmo » Fri May 16, 2008 4:55 pm

richardk,

Thank you so much!!! It worked like magic, but I just have one problem left with this rewrite rule.

Ok so now if I visit:
www.example.com/page/1/2/3/

It requests: www.example.com/page.php/1/2/3/, which is exactly what I was looking for.

The only problem I have now is that if I try to visit:
www.example.com/1/2/3/

... It doesn't work. Only www.example.com/index.php/1/2/3/ works. Is there a way to get rid of that "index.php"?

Many thanks once again! I've had this headache for days and you solved it within minutes. Thank you.
cosmo
 
Posts: 4
Joined: Thu May 15, 2008 6:17 pm

Postby richardk » Fri May 16, 2008 5:11 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)(/(.*))?$ /$1.php/$3 [L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby cosmo » Fri May 16, 2008 11:59 pm

It worked! :D Worked like dark magic!

Thanks a bunch, richardk!
cosmo
 
Posts: 4
Joined: Thu May 15, 2008 6:17 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 8 guests

cron