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.
[solved] mod_rewrite: rewriting subdirs, redirecting root
Moderator: General Moderators
[solved] mod_rewrite: rewriting subdirs, redirecting root
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
Re: mod_rewrite: rewriting subdirectories, redirecting root
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:
Not sure if that will work, but it might be somewhere to start.
HTH,
Mecha Godzilla
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 ^$HTH,
Mecha Godzilla
Last edited by mecha_godzilla on Wed Mar 06, 2013 8:07 pm, edited 1 time in total.
Re: mod_rewrite: rewriting subdirectories, redirecting root
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?
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]- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
Re: mod_rewrite: rewriting subdirectories, redirecting root
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
Using
Code: Select all
ReWriteCond $0 ^$
M_G
Re: mod_rewrite: rewriting subdirectories, redirecting root
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
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
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: mod_rewrite: rewriting subdirectories, redirecting root
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]
[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]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.