Mod rewrite from folder

Discuss practical ways rearrange URLs using mod_rewrite.

Mod rewrite from folder

Postby SystemLord » Thu Jan 22, 2009 12:17 am

Wow am i glad i found this forum, i have been looking over tons of pages trying to find a way to accomplish my mod_rewrite, but i just cant do it....

Here is the problem...
I am trying to redirect a url like this [BMW is just an example, might be VW or also Lamborghini]
Code: Select all
www.domain.com/BMW/somename.html


to this

Code: Select all
www.domain.com/phpfile.php?variable=somename&variable2=BMW


There might be an occurrence however where there is no /BMW/ just
Code: Select all
www.domain.com/somename.html


and they should then be redirected to
Code: Select all
www.domain.com/phpfile.php?variable=somename




Now i have gotten the first to somehow work by just using a RewriteRule as following:
Code: Select all
RewriteRule ^/?(BMW|VW)/([^/]+)\.html$ http://www.domain.com/phpfile.php3?variable=$2&variable2=$1 [R=301,L]


however, all my images don't show up because they are now being looked after in the domain.com/BMW/image/ folder not the domain.com/image/ folder. Furthermore the url is actualy displayed as
Code: Select all
http://www.domain.com/audi_details.php3?car_name=$2&layout=$1

not as
Code: Select all
http://www.domain.com/BMW/somefile.html


Seems confusing? Tell me about it.
I truly hope someone can help me out with this, i kind of need it asap.

Thank you in advance.
SystemLord
 
Posts: 3
Joined: Wed Jan 21, 2009 11:52 pm

Postby richardk » Thu Jan 22, 2009 11:23 am

however, all my images don't show up because they are now being looked after in the domain.com/BMW/image/ folder not the domain.com/image/ folder.

FAQ: Relative paths to images, JavaScript, CSS and other external/linked files are broken.

Furthermore the url is actualy displayed as
Code: Select all
http://www.domain.com/audi_details.php3?car_name=$2&layout=$1

You have made it a redirect (vs. a "rewrite") by using R=301 (permanent redirect) and including "http://www.example.com". Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^(BMW|VW)/([^/]+)\.html$ /phpfile.php3?variable2=$1&variable=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby SystemLord » Thu Jan 22, 2009 11:53 am

Thx for the relative path link...

Am i correct in assuming that changing my current links from this
<img src="images/image-one.jpg" alt="An image of a one">

to this

<img src="/images/image-one.jpg" alt="An image of a one">

will fix the problem? AKA adding in the / before the image folder?

Regarding the rewrite,

Code: Select all
RewriteRule ^(BMW|VW)/([^/]+)\.html$ /phpfile.php3?variable2=$1&variable=$2 [QSA,L]


it still displays the actual RewriteRule not the /BMW/somefile.html

here is my rewrite file:

Code: Select all
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^audi4life.com
RewriteRule (.*) http://www.audi4life.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.audi4life.com/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.audi4life.com/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php3\ HTTP/
RewriteRule ^index\.php3$ http://www.audi4life.com/ [R=301,L]
RewriteRule ^([^/\.]+)\.html$ /audi_details.php3?car_name=$1 [L]
ErrorDocument 404 /404_error.php


I real hope someone can help me out with this because i am completely lost :-(...
SystemLord
 
Posts: 3
Joined: Wed Jan 21, 2009 11:52 pm

Postby richardk » Thu Jan 22, 2009 12:06 pm

urthermore the url is actualy displayed as
Code: Select all
http://www.domain.com/audi_details.php3?car_name=$2&layout=$1

Wait, where? Mod_rewrite does not change the links in your files, you must change them manually. It works on incoming requests only. Is that what you mean?

Am i correct in assuming that changing my current links from this
<img src="images/image-one.jpg" alt="An image of a one">

to this

<img src="/images/image-one.jpg" alt="An image of a one">

will fix the problem? AKA adding in the / before the image folder?

Yes, it should.

here is my rewrite file:

This is simpler
Code: Select all
Options +FollowSymLinks
ErrorDocument 404 /404_error.php

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /index\.(html|php3?)\  [NC]
RewriteRule ^index\.(html|php3?)$ http://www.audi4life.com/ [R=301,L]

RewriteCond %{HTTP_HOST} ^audi4life\.com$ [NC]
RewriteRule ^(.*)$ http://www.audi4life.com/$1 [R=301,L]

RewriteRule ^([^/]+)\.html$ /audi_details.php3?car_name=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby SystemLord » Thu Jan 22, 2009 12:28 pm

Well i need to do this

IF:
Code: Select all
http://www.audi4life.com/sometext.html

OR
Code: Select all
http://www.audi4life.com/BMW/sometext.html


they have to be redirected to

Code: Select all
http://www.audi4life.com/audi_details.php3?car_name=$2&layout=$1


where $1=BMW and $2=sometext

$1 might be empty like in the first url i provided.


----
with the other text i meant that if i use this rule:
Code: Select all
RewriteRule ^(BMW|VW)/([^/]+)\.html$ /phpfile.php3?variable2=$1&variable=$2 [QSA,L]


and enter this URL:
Code: Select all
http://www.audi4life.com/BMW/sometext.html

it dosnt just link to the RewriteRule destination
Code: Select all
/phpfile.php3?variable2=$1&variable=$2


but it actually rewrites the browser URL to
Code: Select all
http://www.audi4life.com/phpfile.php3?variable2=$1&variable=$2


Furthermore the rule dosnt work if there is no /BMW/ and it breaks down.

-----

Used your new simplified code, works, thx for cleaning it up. Truly appreciate the help!
SystemLord
 
Posts: 3
Joined: Wed Jan 21, 2009 11:52 pm

Postby richardk » Fri Jan 23, 2009 11:20 am

Try
Code: Select all
Options +FollowSymLinks
ErrorDocument 404 /404_error.php

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /index\.(html|php3?)\  [NC]
RewriteRule ^index\.(html|php3?)$ http://www.audi4life.com/ [R=301,L]

RewriteCond %{HTTP_HOST} ^audi4life\.com$ [NC]
RewriteRule ^(.*)$ http://www.audi4life.com/$1 [R=301,L]

RewriteRule ^(BMW|VW)/([^/]+)\.html$ /audi_details.php3?layout=$1&car_name=$2 [QSA,L]
RewriteRule ^([^/]+)\.html$          /audi_details.php3?car_name=$1           [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 19 guests

cron