Forum Moderators: phranque
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
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...
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
RewriteRule ^([0-9]+)$ goto.php?id=$1 [L,QSA]
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]
Jim
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?