Forum Moderators: phranque
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]
Try
RewriteRule /displayitemstype.php?cat1=(.*)&cat2=(.*) /$1/$2.html
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
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]
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
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.
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.
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]
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