homepage Welcome to WebmasterWorld Guest from 54.145.209.80
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

    
htaccess variable redirect
Zacharias

5+ Year Member



 
Msg#: 3340020 posted 1:33 pm on May 15, 2007 (gmt 0)

Hi

Am a little confused about a htaccess redirect.

I am wondering if it is possible to do this?

Redirect this

http://example.com/index.php?s=films&c=dvds

to

http://example.com/films/dvds/

I am not trying to rewrite the URL, I have already managed that. I am trying to stop access to these variables in the URL and keep it so that anyone who tries to access those variables in the URL will be redirected the rewritten equivalents.

I can find loads of info on rewrites like this

RewriteRule index/(.*)/(.*)/(.*)/(.*)/$ /index.php?$1=$2&$3=$4

But nothing for redirects.

Do i need to use RewriteCond directive?

Any help is much appreciated.

Z

[edited by: Zacharias at 1:34 pm (utc) on May 15, 2007]

[edited by: jdMorgan at 7:12 pm (utc) on May 15, 2007]
[edit reason] example.com [/edit]

 

Discovery

5+ Year Member



 
Msg#: 3340020 posted 3:12 pm on May 15, 2007 (gmt 0)

I am using this abc.com to redirect to www.abc.com with all variable.

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) http://www\.example\.com/$1 [L]

might be this can help you and you can do change according to your need.
[other member please correct me if i was wrong.]

[edited by: Discovery at 3:14 pm (utc) on May 15, 2007]

[edited by: jdMorgan at 7:13 pm (utc) on May 15, 2007]
[edit reason] example.com [/edit]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3340020 posted 6:06 pm on May 15, 2007 (gmt 0)

1) Publish this HTML link on your Web page(s):
<a href="http://example.com/films/dvds">Order DVDs!</a>

2) Internally rewrite that URL, when requested from your server, to your script:

RewriteRule ^films/dvds/?$ /index.php?s=films&c=dvds [L]

3) Catch direct client requests (only) for the dynamic URL and externally redirect them to the SE-friendly, static-looking URL:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=films&c=dvds[^\ ]*\ HTTP/
RewriteRule ^index\.php$ http://example.com/films/dvds? [R=301,L]

Note that I have used "/films/dvds" as the URL-path; This is the correct way to reference an extensionless file, rather than a directory index. Change to suit.

I assume that all rules are in example.com/.htaccess.

THE_REQUEST is the entire request header received from the client (e.g. browser), and in the case you are trying to address, looks something like this:
GET /films/dvds?s=films&c=dvds HTTP/1.1

By testing THE_REQUEST using a RewriteCond, you prevent the redirect from being invoked as a result of a previously-rewritten request for "/films/dvds". If this test is omitted, then the two rules would each countermand the other, and the result would be an 'infinite' rewrite-redirect loop. This would continue until either the client or the server reached its maximum redirection limit.

The trailing "?" on the external redirect URL is required to clear the existing query string; It *will not* appear in the redirected URL.

[added] For more information, see the "Rewriting static to dynamic URLs" tutorial in the Apache forum section of the WebmasterWorld library [webmasterworld.com]. [/added]

Jim

[edited by: jdMorgan at 6:09 pm (utc) on May 15, 2007]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3340020 posted 7:16 pm on May 15, 2007 (gmt 0)

The domain redirection code posted above is not relevant to this problem. It's also incorrect in at least three ways. While I do not want this thread to be diverted off-topic, here is the corrected domain canonicalization code:

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

Jim

Zacharias

5+ Year Member



 
Msg#: 3340020 posted 3:00 am on May 16, 2007 (gmt 0)

Thanks for the replies they really helped

I am trying now to add variables to the url like this:

I should also mention that I am trying to redirect the link generated by a form submission, so I end up with + signs instead of spaces that i would like to replace with a -

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder/index\.php\?s=([-A-Za-z+]+)[^\ ]*\ HTTP/
RewriteRule ^index\.php$ [127.0.0.1...] [R=301,L]

This is not working as expected and is giving me:

/cheap+action+dvds/

insted of

/cheap-action-dvds/

I cannot seem to replace the + with a -

I have seen some posts using several of the same redirects like this

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
rewriterule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ [mysiteurl.com...] [R=301,L]
rewriterule ^([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ [mysiteurl.com...] [R=301,L]
rewriterule ^([^_]*)_([^_]*)_(.*)\.html$ [mysiteurl.com...] [R=301,L]
rewriterule ^([^_]*)_(.*)\.html$ [mysiteurl.com...] [R=301,L]</code>

is this the solution to my problems?

Would i really have to specify a rewrite for each different combination of words found in the url?

any help is much appreciated

[edited by: Zacharias at 3:04 am (utc) on May 16, 2007]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3340020 posted 3:34 am on May 16, 2007 (gmt 0)

You will probably be a lot happier if you put the hyphens in the original URL (change them within the script you are POSTing to, if possible) so that you do not have to rewrite or redirect to change them.

mod_rewrite is not a full-fledged programming language, and there is no flexibility to handle cases with one, or two, three, or more "+" characters, hyphens, etc., all with one rule. So, an explicit solution for each case will be required:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder/index\.php\?s=([a-z]+)\+([a-z]+)\+([a-z]+)\ HTTP/ [NC]
RewriteRule ^index\.php$ http://example.com/folder/%1-%2-%3/? [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder/index\.php\?s=([a-z]+)\+([a-z]+)\ HTTP/ [NC]
RewriteRule ^index\.php$ http://example.com/folder/%1-%2/? [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder/index\.php\?s=([a-z]+)\ HTTP/ [NC]
RewriteRule ^index\.php$ http://example.com/folder/%1/? [R=301,L]

Obviously, if you add code for three, four, five, and six hyphens, things start to get a bit inefficient.

mod_rewrite also imposes a limit of nine back-references, so you won't be able to handle more than eight hyphens using this method.

Using the [NC] flag makes character matches case-insensitive and eliminates the need for "[A-Za-z]". It does introduce an ambiguity on the "HTTP/" at the end of the request header, which must always be uppercase in a valid HTTP request, but I wouldn't call that a big hacker target...

Jim

Zacharias

5+ Year Member



 
Msg#: 3340020 posted 9:17 am on May 16, 2007 (gmt 0)

Thanks so much for your help on this JD

You are so knowledgeable about a subject that I have to be honest, baffles me sometimes.

Both solutions (fixing in the script and URL redirect) will work nicely for me, Cheers mate :)

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