Solved

htaccess rules for URL routing

Posted on 2012-03-28
5
459 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

831 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