Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite problem

mod rewrite problem

         

milkman_joe

1:58 pm on Sep 14, 2005 (gmt 0)

10+ Year Member



Hi,

I have been trying to mod rewrite my site for some time now and have yet to work it out.

I have something like:
http://www.example.com.au/displayitemstype.php?cat1=Electricals&cat2=Hairdryers

And i would like it to become:
http://www.example.com.au/Electricals/Hairdryers.html

I have tried something like:
RewriteEngine on
RewriteRule (.*)/(.*).html$ /displayitemstype.php?cat1=$1&cat2=$2 [L]

However the url remains the same.

Any help would be greatly appreciated!

[edited by: jatar_k at 3:24 pm (utc) on Sep. 14, 2005]
[edit reason] examplified [/edit]

js1063

3:24 pm on Sep 14, 2005 (gmt 0)

10+ Year Member



Try something like

RewriteRule /displayitemstype.php?cat1=(.*?)&cat2=(.*?) /$1/$2.html

and see if that works

jarom

js1063

3:30 pm on Sep 14, 2005 (gmt 0)

10+ Year Member



Sorry.. modification to my previous message. Leave off the question marks in the regular expression -- Apache's regexps are slightly different than Perl's. I was trying to do non-greedy matching but in this case it probably doesn't matter.

Try

RewriteRule /displayitemstype.php?cat1=(.*)&cat2=(.*) /$1/$2.html

jd01

5:44 pm on Sep 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



**Added: didn't notice**

Welcome to WebmasterWorld.

**End Addition**

It appears you are going the wrong way.

Your links should point to the static pages, then you can use mod_rewrite to serve the information to those locations.

See the Library -- links top left of the pages.

The correct way to do what you have asked above is:

http://www.example.com.au/displayitemstype.php?cat1=Electricals&cat2=Hairdryers

RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat1=(.+)cat2=(.+)
RewriteRule ^displayitemstype\.php$ http://yoursite.com.au/%1/%2.html [R=301,L]

But, my guess is you do not want every link on your site to point to a page that is redirected.

Justin

milkman_joe

1:11 am on Sep 15, 2005 (gmt 0)

10+ Year Member



Thanks for the replies guys.

When i tried the following:

RewriteCond %{QUERY_STRING} ^cat1=(.+)cat2=(.+)
RewriteRule ^displayitemstype\.php$ http://example.com/%1/%2.html [R=301,L]

Im getting a 404 error.

The other problem that might exist in the future is that sometimes we have more string requests for that same page. Here are two examples:
Example 1: http://www.example.com/displayitemstype.php?cat1=Electricals&cat2=Straighteners
Example 2: http://www.example.com/displayitemstype.php?cat1=Electricals&cat2=Hairdryers&limit=8&find=2

Example two is because we have a limit for mysql results for each page.

[edited by: jdMorgan at 4:24 am (utc) on Sep. 15, 2005]
[edit reason] No URLs, please. See TOS. [/edit]

jd01

4:15 am on Sep 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, do you really want to redirect from the dynamic URLs to the static, or do you want users to access the static URL and get the information from the dynamic?

If you want users to use the static URL with the dynamic information, you are going the wrong way, and will need to change your links so they link to the static URL, then use mod_rewrite to serve the information from the dynamic location when the static URL is requested.

Static URL -- Dynamic Content:

RewriteEngine on
RewriteRule ^([^/]+)/([^.]+)\.html$ /displayitemstype.php?cat1=$1&cat2=$2 [L]

() = creates a back-reference.

[^/]+ = Anything that is not a /

[^.]+ = Anything that is not a .(dot)

\ = escaping character, so \. = a litteral .(dot)

Please, be specific about what you are trying to accomplish.

Justin

milkman_joe

12:34 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



Thank for the reply.

Is there some way i can avoid changing the links in my php files and just using .htaccess file?

Because if i use the static URL with the dynamic information, it will cause me to change alot of links in my php files.

I tried it and it worked fine except for the part that it did not include the css file.

milkman_joe

1:22 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



Ok, after a bit of research on the apache help tutorials ive realised that 'You cannot use mod_rewrite to change the address displayed in the browser... including to a more 'friendly' URL -'

So i must change every link in my php files.

Also the reason why my css files dont show up is because i was using the '/' to seperate the variables. Which confuses the location of the css file.

All fixed.
Thanks for the help.

milkman_joe

11:26 am on Sep 19, 2005 (gmt 0)

10+ Year Member



I have come accross a problem i am not sure how to fix. One of my products name has a forward slash.

http://www.example.com.au/178-CurlingIron11/2.html

So the problem is when the page redirects, its trying to look in another folder. (/2.html)

How can i get around this? Apart from renaming the file.

[edited by: jdMorgan at 2:24 pm (utc) on Sep. 19, 2005]
[edit reason] No URLs please. See TOS. [/edit]

jdMorgan

1:58 am on Sep 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a couple of things you could do: You could either change the URL, or write a rule specifically for that URL and place it above the general rule.

Jim

milkman_joe

5:24 am on Sep 21, 2005 (gmt 0)

10+ Year Member



how would i write a rule specifically for the one page?

jd01

6:14 am on Sep 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That depends on what information you need where -- when looking at the example above, I cannot figure out what information should be passed to get the result you are looking for.

The easiest way will be to create the appropriate back references using the full URL. EG if 178 was the cat1 and curling iron was the cat2, you could use this:

RewriteRule ^(178)-(CurlingIron)11/2\.html$ /displayitemstype.php?cat1=$1&cat2=$2 [L]

You will just need to put the information you need for each cat inside of the appropriate back-reference () -- count left ( to find the correct reference number. Then add the single rule to the file prior to the other 'catch all' rule.

Justin

milkman_joe

2:22 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



Thanks, uve hit the nail on the head :)