PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Aug 20, 2018 2:15 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Mar 06, 2013 6:49 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6445
Location: 53.01N x 112.48W
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: [ Download ] [ Hide ]
  1. Options +FollowSymLinks  
  2. RewriteEngine on 
  3.  
  4. RewriteCond %{REQUEST_FILENAME} !-s 
  5. RewriteCond %{REQUEST_FILENAME} !-f 
  6. RewriteCond %{REQUEST_FILENAME} !-d 
  7. RewriteRule ^(.+)$ index.php?code=$1 [L,NS] 
  8. RewriteRule / http://website.mydomain.tld/ [NS,R] 

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.


Top
 Profile  
 
PostPosted: Wed Mar 06, 2013 7:50 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Apr 14, 2010 4:45 pm
Posts: 375
Location: UK
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:

Syntax: [ Download ] [ Hide ]
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 9:07 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Wed Mar 06, 2013 8:06 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
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:
# 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]


Top
 Profile  
 
PostPosted: Wed Mar 06, 2013 9:06 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Apr 14, 2010 4:45 pm
Posts: 375
Location: UK
Thanks for that requinix :mrgreen:

Using

Syntax: [ Download ] [ Hide ]
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


Top
 Profile  
 
PostPosted: Thu Mar 07, 2013 10:49 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6445
Location: 53.01N x 112.48W
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.


Top
 Profile  
 
PostPosted: Thu Mar 07, 2013 11:18 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6445
Location: 53.01N x 112.48W
I posted this on StackOverflow as well, and the answer there worked. Here's the final working file:

Syntax: [ Download ] [ Hide ]
  1. Options +FollowSymlinks -MultiViews 
  2. RewriteEngine On 
  3.  
  4. RewriteCond %{REQUEST_URI} !index\.php    [NC] 
  5. RewriteRule ^([^/]+)/?  /index.php?code=$1 [NC,L] 
  6.  
  7. RewriteCond %{REQUEST_URI}  ^/$    
  8. RewriteRule .*  http://website.domain.tld [R=301,L] 

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group