0

Here is my current htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.website\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.website\.com [NC]
RewriteRule ^(.*)$ http://www.website.com/gotourl.php?urlid=?%1 [L]
RewriteRule    ^(\w+)$ ./gotourl.php?urlid=$1    [NC,L]    
RewriteRule    ^(\w+)/$ ./gotourl.php?urlid=$1    [NC,L]   

The website in question is a URL shortener I'm working on for testing/developing and personal use. The above code may not be ideal as I'm not that familiar with .htaccess and made this via googling and testing. This does what I want it to for the URL shortening aspect:

http://website.com/1234 -> http://website.com/gotourl.php?urlid=1234
http://website.com/1234/ -> http://website.com/gotourl.php?urlid=1234

The problem is I cannot figure out how to get it to ignore certain directories. I want specific directories to go to specific pages:

http://website.com/img (or) http://website.com/img/ -> http://website.com/img.php
http://website.com/img/1234 (or) http://website.com/img/1234/ -> http://website.com/viewimage.php?imgid=1234

The main problem is I don't know how to properly do this and the ways I've tried to do it would always rewrite to:

http://website.com/gotourl.php?urlid=XXXX

or end up in an infinite loop of redirects instead of going to the specific page I'm wanting. Any help is appreciated.

In essence I'm looking for help writing my htaccess to do the following:

URL user visits -> URL user sees
http://website.com/img -> http://website.com/img.php
http://website.com/img/XXXX -> http://website.com/viewimage.php?imgid=XXXX
http://website.com/XXXX -> http://website.com/gotourl.php?urlid=XXXX

All paths should work with or without the trailing slash.

Looking for help to get it to do that with maybe a bit of explanation as to what each line actually means so I can learn from it.

Jetteh22
  • 141
  • 3
  • 11
  • just an FYI, `http://website.com/img` is not a subdomain. An example of a subdomain url: `http://test.website.com/img`. `test` is the sub domain. `img` is the path. Even `www` is technically a subdomain of a website. – Jonathan Kuhn Jun 18 '14 at 18:12
  • @JonathanKuhn - Duh. I know that too :P Sorry. Editing now to get rid of all of those subdomains. I meant directory :P – Jetteh22 Jun 18 '14 at 18:13
  • You could probably just use a condition like: `RewriteCond %{REQUEST_FILENAME}.php -f` to check if the requested path + .php exists and then rewrite to that file. – Jonathan Kuhn Jun 18 '14 at 18:15
  • Something like: http://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess – Jonathan Kuhn Jun 18 '14 at 18:17

1 Answers1

0

The main thing you want to change, is making your rule more specific. Your shortened url can probably not contain a slash or a dot, so you could change both the url shortening url's to: RewriteRule ^([^/\.]+)/?$ gotourl.php?urlid=$1 [NC,L]. You'll need to prevent 'gotourl.php' from matching that though. The rule for img alone is more specific and should be done first. The rule for img/something is a little less specific and can be anywhere.

RewriteEngine On

RewriteRule ^img/?$ img.php [L]
RewriteRule ^img/([^/]+)/?$ viewimage.php?imgid=$1 [L]
RewriteCond %{HTTP_HOST} !^www\.website\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.website\.com [NC]
RewriteRule ^(.*)$ http://www.website.com/gotourl.php?urlid=%1 [L]
RewriteRule ^([^/\.]+)/?$ gotourl.php?urlid=$1 [NC,L]
Sumurai8
  • 20,333
  • 11
  • 66
  • 100
  • That worked for the URL shortening and also for the /img/XXXX but it didn't work for the regular /img/. That's still trying to pull from gotourl.php?urlid=img which gives an error because the short URL img doesn't exist. – Jetteh22 Jun 18 '14 at 18:32
  • Did you copy over the code above (that is: do you have the rules in the same order)? – Sumurai8 Jun 18 '14 at 18:43
  • Yep - Copied and pasted without changing anything except 'website' to the correct domain. – Jetteh22 Jun 18 '14 at 18:45
  • I have made a small change to the first rule: adding `/?` to the first argument. It is the only reason I can think of why it doesn't work correctly. It should match before the url shortening rule, and `img.php` should not match the url shortening rule. – Sumurai8 Jun 18 '14 at 18:50
  • Thanks! That worked a bit better. 2 Issues, though: 1) /img/ did take me to img.php but, for some reason, it didn't load my style sheet I don't think (page background, text formatting, etc were missing). 2) /img without the trailing slash literally took me to http://website.com/img/?urlid=img (that's the URL that showed in my browser after trying to go to /img – Jetteh22 Jun 18 '14 at 18:57
  • Okay, I fixed the CSS issue by adding a `` and that fixed it. Now the only issue I'm having is if I go to http://website.com/img it takes me to (and shows in the browser) http://website.com/img/?urlid=img - not sure what's causing that. EDIT**: I fixed that issue by changing [L] to [NC,L] - seems to be working fine for now! THank you so much for your help! – Jetteh22 Jun 18 '14 at 20:39