mod_rewrite

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

mod_rewrite

Post by malcolmboston »

Ive just been reading up on HTaccess and im baffled tbh.

i have URL's that look like the following

Code: Select all

http://localhost/RedSquare/MainSite/cms/index.php?s=products&a=edit&productStockCode=71304
i would like it to look like

Code: Select all

http://localhost/RedSquare/MainSite/cms/index.php/products/edit/71304
How would i go about this?

Any help would be appreciated

Thanks, Mal
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You can do that with PHP itself. There's an argument separator directive in php.ini I think, and either way you can read it from $_SERVER["REQUEST_URI"].

With mod_rewrite it would be:

Code: Select all

RewriteBase /RedSquare/MainSite/cms/
RewriteRule    ^index\.php/([A-Za-z0-9]+)/([a-zA-Z0-9]+)/([0-9]+)$    index.php?s=$1&a=$2&productStockCode=$3
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

for whatever reason that doesnt work on the URL

Code: Select all

http://localhost/RedSquare/MainSite/cms/index.php?s=products&a=edit&productStockCode=71304
using

Code: Select all

RewriteEngine On
RewriteBase /RedSquare/MainSite/cms/
RewriteRule    ^index\.php/([A-Za-z0-9]+)/([a-zA-Z0-9]+)/([0-9]+)$    index.php?s=$1&a=$2&productStockCode=$3
i woulod be very interested in hearing how it can be done with PHP (literally rewrite the URL from an end uer perspective, surely would reqwuire Javascript?)

Cheers
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

i managed to build this using some simple str_replace

Code: Select all

urlrewrite Object
(
    [originalAddress] => http://localhost/RedSquare/MainSite/cms/index.php?s=products&a=edit&productStockCode=70059
    [NewAddress] => http://localhost/RedSquare/MainSite/cms/products/edit/70059
)
i cannot see anyway though i can transparently use this so that when a visitor goes to the NewAddress they are actually going to the OldAddress, isnt that what the rewrite engine does (in an overcomplicated way)
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

I think your problem is that you're still linking to

Code: Select all

http://localhost/RedSquare/MainSite/cms/index.php?s=products&a=edit&productStockCode=71304
and wanting it to show up as

Code: Select all

http://localhost/RedSquare/MainSite/cms/index.php/products/edit/71304
in the address bar. mod_rewrite and htaccess don't actually rewrite the URL that shows up in the address bar. What mod_rewrite is doing is secretly pointing anything that matches the clean look to instead point to the index.php file with the arguments.

If this is the case, change all your links in your site to point directly to

Code: Select all

http://localhost/RedSquare/MainSite/cms/index.php/products/edit/71304
add the htaccess lines that d11wtq posted, and put the htaccess file in the same directory as your index.php file. This will make it so that anytime you link directly to

Code: Select all

http://localhost/RedSquare/MainSite/cms/index.php/products/edit/71304
it gets pointed to

Code: Select all

http://localhost/RedSquare/MainSite/cms/index.php?s=products&a=edit&productStockCode=71304
Post Reply