Forum Moderators: phranque

Message Too Old, No Replies

Variable folder redirect

         

mrvega

2:30 pm on Sep 27, 2007 (gmt 0)

10+ Year Member



Hi All, I'm new on the forum, and after spending a couple of hours reading through the posts I just can't find a solution for my problem.

Im tryin to set up a redirect from a folder to a php script.
example:
[mysite.com...]
will be redirected to [mysite.com...]

The folder, in this case 22, isn´t actually on the server.
Also how will this affect other folders that do physically exist on the server like [mysite.com...] do I need to set up seperate redirect rules for those, and what's the correct order of rules to do this correctly.

I hope you guys can give me an answer to this problem!

Thanks in advance!

MrVega

akameng

4:50 pm on Sep 27, 2007 (gmt 0)

10+ Year Member



This will be help you

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

#Case1: for file/folder if not exist
RewriteCond /your/docroot/%{REQUEST_FILENAME}!-f
RewriteRule ^(.+) [exmaple.com...] [L,QSA]

#or
RewriteCond %{REQUEST_URI}!-U
RewriteRule ^(.+) [exmaple.com...] [L,QSA]

"This uses the URL look-ahead feature of mod_rewrite. The result is that this will work for all types of URLs and is a safe way. But it does a performance impact on the webserver, because for every request there is one more internal subrequest. So, if your webserver runs on a powerful CPU, use this one. If it is a slow machine, use the first approach or better a ErrorDocument CGI-script.
from: [httpd.apache.org...]

and for file/folder that exist, if you wish to redirect them, then:

RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ goto.php?id=$1 [L,QSA]

#you can exclude some files/folders,

RewriteCond %{REQUEST_URI}!(^/login/(.*)/¦^/yourfolder/)
RewriteRule ^(.*)$ goto.php?id=$1 [L,QSA]

I am sorry for any error, Just I wanna to help, because more people helped me...

mrvega

12:11 am on Sep 28, 2007 (gmt 0)

10+ Year Member



Hey akameng!

Thank you very much for your input on this matter!

I've tried your solutions, the first two I can't get to work.

your 3rd rewrite rule:
RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ goto.php?id=$1 [L,QSA]

exactly did what I wanted!
it's redirecting me from www.site.com/22
to www.site.com/goto.php?id=22

However under this current setting, the actual root will redirect, so will all the folders that already exist. (my image folder for example goes through goto.php?id=images)

What line do I need to add to exclude the root folder (so www.site.com/) and my existing folders (images, login, etc.) from redirection?

Thanks!

MrVega

jdMorgan

3:57 am on Sep 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If these non-existent URLs can be classified -- that is, say, if they are always numeric, then something like:

RewriteRule ^([0-9]+)$ goto.php?id=$1 [L,QSA]

This will not rewrite your root folder because it is non-numeric, nor will it create a loop, because goto.php is non-numeric as well.

If the non-existent URLs are not always numeric and you cannot describe a concise pattern that will fit them, then something like:


RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^([^.]+)$ goto.php?id=$1 [L,QSA]

might work better. But this method invokes a call to the filesystem for every request matching the rule pattern, to check to see if the file exists. Therefore, it is much less efficient than a purely pattern-based rule.

Jim

g1smd

8:03 am on Sep 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your question asked for help with the code for a redirect, but your code is for a rewrite.

You do need a redirect too. If a user asks for the dynamic URL, you'll want to redirect them to the "static" URL.

mrvega

8:17 pm on Sep 28, 2007 (gmt 0)

10+ Year Member



Hi guys!

Thanks for your input on this one, I appreciate it. JD thanks for your line on numeric only, since this is the case so my problem is solved!

MrVega

mrvega

8:52 am on Oct 3, 2007 (gmt 0)

10+ Year Member



JD, just to get back to your second remark, if the url would have got a Pattern, let's say: numeric + the letter A. www.example.com/12A or /654987A can this be described as a pattern, and what modification would the numeric only line need?

Thanks,

MrVega

g1smd

9:52 am on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is it always the same number of characters?

Are the letters and numbers always in the same position?

[a-z]{3,6}[0-9]{1,2} describes a string that contains 3 to 6 letters followed by 1 or 2 digits, for example.

Can you test for the presence (or absence) of a / or . or - in the URL as a means to differentiate the URL or a part of the URL?