Solved

htaccess rules for URL routing

Posted on 2012-03-28
5
451 Views
Last Modified: 2012-03-28
Hi!

I am building a URL shortener app and am having trouble getting my .htaccess file correct.  I need to funnel all requests into route.php, except for asset files (.js, .css, .png, .jpg), and two folders (lib/ and admin/).

Whats the best way to achieve this?  Here's what I have currently ( I got this from Google - I don't really understand the htaccess syntax):

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On

# Redirect requests to route.php, excluding lib + admin folders and resource assets
RewriteCond %{REQUEST_URI} !=route.php
RewriteCond %{REQUEST_URI} !^/(admin|lib)/
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
RewriteRule .* route.php

Open in new window


The folder exclusion rule for lib and admin still redirect to route.php.. any ideas why?

Also, how do I remove the .php from the URL file extension, as usually done with a typical MVC environment.

Thanks..

TheFoot
0
Comment
Question by:Barry Jones
  • 2
  • 2
5 Comments
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 50 total points
ID: 37775945
0
 
LVL 12

Author Comment

by:Barry Jones
ID: 37775966
Great article - thanks, however it doesn't help me to exclude folders from my routing.. Any ideas?
0
 
LVL 7

Accepted Solution

by:
designatedinitializer earned 450 total points
ID: 37775976
	# Send request via route.php but only if its not a real file or folder
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ route.php/$1 [L]

Open in new window

it works for me
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37775981
0
 
LVL 12

Author Closing Comment

by:Barry Jones
ID: 37776009
Excellent solution - works great, nice and simple.  Thanks!

@BuggyCoder - thanks for the links - useful infos..
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now