Question

So I'm adjusting the URLs on a site I'm working on and I'm having some trouble with a couple of variables being passed in the URL.

mylighting.com/bath-fixture-c-13.html?osCsid=u2qj8o9rvjn0p5pa7p8npuhs54
RewriteRule ^bath-fixture bath-fixture-c-13.html?id=$1

So this Rewrite works perfect as the page that comes up is mylighting.com/bath-fixture

Now unfortunately on that page there are several other items to view and I'm having some trouble with the page variable. I think I have the code correct but every time I try to go to the correct page it doesn't seem to work.

http://mylighting.com/bath-fixture-c-13.html?page=2&id=u0hnumfus6gjhjc45av36663m3
RewriteRule ^bath-fixture/([a-zA-Z0-9]+)$ bath-fixture-c-13.html?page=$1&id=$2

So I thought I had this correct but apparently not. I would like the output to be mylighting.com/bath-fixture/2 for the second page.

Unfortunately with that code in the .htaccess, every time I input that URL it takes me to the first page of the category and not the second like it should.

Was it helpful?

Solution

It appears that you have misunderstood the format of the RewriteRule. The first one is working by accident.

RewriteCond %{QUERY_STRING}  page=([0-9]+)
RewriteRule ^bath-fixture-c-13.html bath-fixture/%1 [R=301,QSA,L]
RewriteRule ^bath-fixture-c-13.html bath-fixture/ [R=301,QSA,L]

The first argument to RewriteRule is the regex to match against the requested URL. The second argument is the URL to send as a Redirect to the user, so they end up at your desired URL instead. Because you want to parse the QUERY_STRING I believe you need to use a RewriteCond. If you are doing this for SEO purposes do not forget to add the [R=301].

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

If you are trying to go the other direction then you need the following.

RewriteRule ^bath-fixture/$ bath-fixture-c-13.html [QSA,L]
RewriteRule ^bath-fixture/([0-9]+) bath-fixture-c-13.html?page=$1 [QSA,L]

If you can clarify your desired inputs and outputs you will get correct answers much more quickly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top