[solved] mod_rewrite: rewriting subdirs, redirecting root

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

[solved] mod_rewrite: rewriting subdirs, redirecting root

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: mod_rewrite: rewriting subdirectories, redirecting root

Post 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
Last edited by mecha_godzilla on Wed Mar 06, 2013 8:07 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: mod_rewrite: rewriting subdirectories, redirecting root

Post 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]
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: mod_rewrite: rewriting subdirectories, redirecting root

Post by mecha_godzilla »

Thanks for that requinix :mrgreen:

Using

Code: Select all

ReWriteCond $0 ^$
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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: mod_rewrite: rewriting subdirectories, redirecting root

Post 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
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: mod_rewrite: rewriting subdirectories, redirecting root

Post 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]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply