Jump to content

Rewrite Help - How to ignore first query string parameter


drjnet

Recommended Posts

Hi,

Im struggling with this, any help would be much appreciated.

 

I have a site with two types of pages like this

 

Categories - http://www.domain.com/listing.php?brandid=14

Products - http://www.domain.com/details.php?prodid=297

 

I need to move to this:

 

Categories - http://www.domain.com/category-alias

Products - http://www.domain.com/product-alias

 

For various reasons it will be easier for me to add another parameter to the urls rather than simple replace the id with the aliases, so i will be able to get to this

 

Categories - http://www.domain.com/listing.php?brandid=14&alias=category-alias

Products - http://www.domain.com/details.php?prodid=297&alias=product-alias

 

My questions are :

 

a) Can i setup a rewrite rule that ignores everything upto the alias (as the id will vary) and just picks up the alias so my urls look like this

Categories - http://www.domain.com/category-alias

Products - http://www.domain.com/product-alias

 

I've got this so far in my .htaccess:

 

RewriteEngine on

RewriteRule ^([A-Za-z0-9-]+)/?$ listing.php?brandid=$1&alias=$2 [L]

RewriteRule ^([A-Za-z0-9-]+)/?$ details.php?prodid=$1&alias=$2 [L]

 

But if i stick this in the browser for example :

http://www.domain.com/listing.php?brandid=15&alias=test-alias

 

the url does not rewrite/redirect to:

http://www.domain.com/test-alias

 

Am I missing something really simple? htaccess is definately work ok just not my rewrite rules.

 

Can anyone help ?

thanks

 

 

 

 

 

Link to comment
Share on other sites

In my opinion you seem to be looking at the problem entirely backwards (which seems very common with mod_rewrite). You don't really want to type that URL (the one with the query string) into the address bar and have the other (the 'neat' URL) appear, you want people/sites to use the 'neat' URL and have the one with a query string be the one that the user sees (but without the address bar changing). If you wish to use the id, there is no escaping it will have to be in the URL, generally speaking people would use domain.com/id-number/title. That way the number is there for the background work, but the title is still visible.

Link to comment
Share on other sites

You are dead right, i was informed about this today and fell fairly stupid!!! (no actually very stupid...)

 

I guess what i need to know then is how to send my short urls through some kind of script using htaccess to use mod_rewrite effectively.

 

Now its been pointed out, it clear i was looking at this arse backwards (no change there).

 

Anyone got any pointers? Im guessing i need to tell htaccess to push all  urls matching a certain regex through some kind of helper script that points the request at the full url?

 

So it looks like i just need to modify the ugly url so it will work and carry the appropriate info (e.g. nice title) then setup some rewrite rules??

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.