views:

596

answers:

2

Hi,

I have a tricky issue redirecting some urls internally for my site.

The situation is this, I currently have a url like example.com/check/youtube.com which internally redirects to check.php?domain=youtube.com using the following mod_rewrite code:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

RewriteRule ^offline offline.php [NC,L]
RewriteRule ^error error.php [NC,L]

RewriteRule ^check/(.*)$ check.php?domain=$1 [NC,L]

However I would also like to be able to redirect to check.php using a url like example.com/youtube.com. Unfortunatly it is just beyond me to figure it out.

I have a directory /assets/ with all the css, js, etc. which shouldn't be effected.

Thanks :P

+2  A: 

Try this rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.]+\.[^/]+$ check.php?domain=$0 [L]

This rule rewrites any request with a URL path of the form [^/.]+\.[^/]+ (a string that contains at least one dot but no slashes at all) that cannot be mapped to an existing file to your check.php.

Gumbo
Thanks a ton, intreasting that you use $0 as I haven't seen that used before, I'll have to look that up :P
Sam
`$0` is just the matched string of the whole regular expression.
Gumbo
Ah, yea that makes a lot of sense. Thanks again.
Sam
A: 

As you want to redirect "example.com/youtube.com" does that mean you wish to redirect pretty much anything? What is specifically allowed to be passed, e.g. would I be allowed to pass "example.com/youtube.com/foobar.php" for a redirect to check.php?

Philipp Lenssen
Well, the script is cleaned via js on submission (there is a note as well for non js users), so if you were to submit *example.com/youtube.com/foobar.php* then it would refresh to *example.com/youtube.com*.If someone does send that link direct to the script though then it returns a 404. But I will work on that later and it's not a problem.though if you could clean it with .htaccess instead that would be cool...
Sam