homepage Welcome to WebmasterWorld Guest from 54.211.219.178
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod rewrite confusion
breham




msg:4433810
 7:18 am on Mar 27, 2012 (gmt 0)

Hi,

I'm trying to redirect:

www.example.com/new_cars/cars.php?make=Vauxhall to

www.example.com/new_cars/Vauxhall.html

but can't seem to to do it - I have got it to redirect to

www.example.com/Vauxhall.html but can't seem to include the new_cars directory in the redirect

Any advice please?

[edited by: incrediBILL at 8:16 am (utc) on Mar 27, 2012]
[edit reason] exemplified URLs [/edit]

 

g1smd




msg:4433812
 7:30 am on Mar 27, 2012 (gmt 0)

Without seeing your code, can't offer much as to where you went wrong.

Do use a RewriteRule rather than Redirect or Redirectmatch.

Use example.com in this forum to suppress URL auto-linking.

lucy24




msg:4433815
 7:35 am on Mar 27, 2012 (gmt 0)

:: peering into crystal ball ::

He's applying mod_alias syntax to mod_rewrite, so he expects the whole preceding part of the URL to remain unchanged.

breham




msg:4433819
 8:01 am on Mar 27, 2012 (gmt 0)

Oops, my code didn't make it in!

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /new_cars/cars.php?make=$1 [L]


is what I'm using currently but that redirects to the root directory I want to keep it all in /new_cars/ directory.

phranque




msg:4433902
 1:22 pm on Mar 27, 2012 (gmt 0)

you're doing a rewrite there.
if you want a redirect you must specify the R flag, probably with a 301 value.
you can also get a redirect to work if you specify the hostname in the substitution string.
i would do both - to canonicalize the hostname and make it clear that it is a redirect.

breham




msg:4433912
 2:06 pm on Mar 27, 2012 (gmt 0)

you're doing a rewrite there.
if you want a redirect you must specify the R flag, probably with a 301 value.
you can also get a redirect to work if you specify the hostname in the substitution string.
i would do both - to canonicalize the hostname and make it clear that it is a redirect.


Thanks for that - are you able to give an example of what you mean please?

Basically what I want to achieve is

/new_cars/cars.php?make=brandname

to have a friendlier url of

/new_cars/brandname.html

phranque




msg:4433926
 3:04 pm on Mar 27, 2012 (gmt 0)

I have got it to redirect to www.example.com/Vauxhall.html but can't seem to include the new_cars directory in the redirect


what mod_rewrite directives did you use for this?

g1smd




msg:4434077
 7:32 pm on Mar 27, 2012 (gmt 0)

to have a friendlier url of

/new_cars/brandname.html

mod_rewrite cannot "make" new URLs.

The very first step is that the links on your page should point to the friendly URL that you desire your users to "see" and "use".

URLs are defined in links.

mod_rewite kicks in only after that link is clicked.


You might want to replace
[^/]* with [^/]+ here. Your current rule allows example.com/.html to be a valid URL.
lucy24




msg:4434148
 10:09 pm on Mar 27, 2012 (gmt 0)

you're doing a rewrite there.
if you want a redirect you must specify the R flag, probably with a 301 value.

The code is right; it's the name that is wrong.
:: shuffling papers ::
###. I thought I'd written up some boilerplate on the redirect-to-rewrite two-step, but apparently not.
:: put on To Do list ::

Is this what you're trying to do overall?

IF user requests long ugly URL (via type-in or outdated bookmark) with query string, send them over to short pretty URL.

IF there is a request (either from human or from previous redirect) for short pretty URL, serve content from long ugly URL with query string.

If yes, then everyone is on the same page and we can continue :)

cwright2012




msg:4435363
 6:42 pm on Mar 30, 2012 (gmt 0)

Breham,

Have you found a solution yet? I am in the same situation. I emailed you my contact info for offline discussion.

Thanks,

g1smd




msg:4435390
 7:41 pm on Mar 30, 2012 (gmt 0)

As this is a question that comes up fairly regularly (like at least several times each month) there's a whole bunch of previous threads with example code that can be raided for ideas. Post your code here for discussion.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved