All folder paths to 1 file

New to mod_rewrite? This is a good place to start.

All folder paths to 1 file

Postby SmEdD » Wed Sep 30, 2009 7:09 pm

I have a program where it looks at the URI and picks the right files to load.

example:
sub.site.com/accounts/user

would really go to:
sub.site.com/index.php

but it would stay as the original URL since index.php is requesting the URI so it can pull up the right files.

This is the code I have atm but it's not working and I'm a noobie :(
Code: Select all
RewriteEngine On
RewriteRule ^/.*(\.css)|(\.php)|(\.htc)|(\.jpg)|(\.png)|(\.html)|(\.gif)|(\.js)|(\.txt)|(\.ico)$ - [L]
RewriteRule ^/css/(.*) - [L]
RewriteRule ^/([a-zA-Z0-9\s_/+.!*',()~-]+)$ /index.php [QSA]


Also this is in a sub domain just incase that makes any difference.
SmEdD
 
Posts: 2
Joined: Wed Sep 30, 2009 6:51 pm

Postby richardk » Thu Oct 01, 2009 8:40 am

Are you using a .htaccess file? If you are you should not put a / after the ^ because the paths matched by the RewriteRule are relative to the current directory.

You could either use
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# If it's not (!) a request to an existing file (-f)
RewriteCond %{SCRIPT_FILENAME} !-f
# and if it's not (!) a request to an existing directory (-d)
RewriteCond %{SCRIPT_FILENAME} !-d
# send the request to index.php.
RewriteRule . /index.php [QSA,L]


Or (like your mod_rewrite)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# File extensions to ignore.
RewriteRule ^.+\.(css|php|htc|jpg|png|html|gif|js|txt|ico)$ - [L]
# Directories to ignore.
RewriteRule ^css(/.*)?$ - [L]
# All other requests to index.php.
RewriteRule ^(.+)$ /index.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby SmEdD » Thu Oct 01, 2009 6:44 pm

Much thanks, I used the 2nd one you posted and it works.

Very much appreciate the time you use to help people out here, went though a bunch of others.

Thanks again!
SmEdD
 
Posts: 2
Joined: Wed Sep 30, 2009 6:51 pm


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 27 guests

cron