Page 1 of 1
[solved] mod_rewrite: rewriting subdirs, redirecting root
Posted: Wed Mar 06, 2013 5:49 pm
by pickle
I'm setting up a url shortening service for my organization. I want to rewrite shortened urls to my index.php file, but redirect requests to the root to my main website, ie:
Request: short.domain.tld/abc
User sees: short.domain.tld/abc
Page served up: short.domain.tld/index.php?code=abc
Request: short.domain.tld/
User sees: website.domain.tld
Page served up: website.domain.tld (on my website server)
My .htaccess file is currently:
[syntax]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?code=$1 [L,NS]
RewriteRule /
http://website.mydomain.tld/ [NS,R]
[/syntax]
I'm unsure how to differentiate between requests for subdirectories and requests for the root of the domain.
Re: mod_rewrite: rewriting subdirectories, redirecting root
Posted: Wed Mar 06, 2013 6:50 pm
by mecha_godzilla
Hi,
While I'm Gxxgling for some inspiration, your rule seems to be that if there's no page or directory name specified at the end of the URL, redirect to the main site. Can you just do an exact match on the address as your first rewrite rule? I think you can use %{REQUEST_URI} or $0 to get this information:
Code: Select all
RewriteCond %{REQUEST_URI} ^$
RewriteCond $0 ^$
Not sure if that will work, but it might be somewhere to start.
HTH,
Mecha Godzilla
Re: mod_rewrite: rewriting subdirectories, redirecting root
Posted: Wed Mar 06, 2013 7:06 pm
by requinix
FYI the REQUEST_URI always starts with a slash - it'll never be empty. It's not quite the same thing that RewriteRule operates on.
So there's two things going on: short.domain.tld/ redirects to the normal website, but short.domain.tld/* is a shortcode?
Code: Select all
# shortcodes
RewriteCond %{HTTP_HOST} =short.domain.tld
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)$ index.php?code=$1 [L,NS]
# allow index.php
RewriteCond %{REQUEST_FILENAME} =%{DOCUMENT_ROOT}/index.php
RewriteRule ^ - [L]
# send everything else away
RewriteRule ^ http://website.domain.tld%{REQUEST_URI} [L,R]
Re: mod_rewrite: rewriting subdirectories, redirecting root
Posted: Wed Mar 06, 2013 8:06 pm
by mecha_godzilla
Thanks for that requinix
Using
does seem to work at least if you need to match an empty request string, but you're right that %{REQUEST_URI} will always return a "/".
M_G
Re: mod_rewrite: rewriting subdirectories, redirecting root
Posted: Thu Mar 07, 2013 9:49 am
by pickle
Thanks, but that doesn't work.
Requests to short.domain.tld are being properly forwarded to website.domain.tld, but short.domain.tld/abcd/ gets forwarded to website.domain.tld/index.php?code=abcd
I only have 2 routing conditions:
If a shortcode is specified, redirect to index.php
Otherwise, redirect to website.domain.tld
Re: mod_rewrite: rewriting subdirectories, redirecting root
Posted: Thu Mar 07, 2013 10:18 am
by pickle
I posted this on StackOverflow as well, and the answer there worked. Here's the final working file:
[syntax]Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/? /index.php?code=$1 [NC,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .*
http://website.domain.tld [R=301,L][/syntax]