mod_rewrite -> php variables

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

mod_rewrite -> php variables

Postby sven » Sat Jun 20, 2009 6:38 pm

Hopefully this doesn't get deleted. This is a simple one for someone who knows how this works.

All I need to do is pass a variable from the url using an internal rewrite (ie its not seen).

Code: Select all
example.com/lorem

to
Code: Select all
example.com/index.php?file=lorem


Can anyone help me?

EDIT:
Code: Select all
Options +FollowSymLinks

RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /index.php?file=$1 [QSA,L]


Stole this from another place, its not helping me much though.

EDIT: I GOOOOTTTT IT. These forums own.
Even though they seem to love pron.

EDIT: Can I make it not apply for one condition? Say not apply for the index page.
sven
 
Posts: 2
Joined: Sat Jun 20, 2009 6:30 pm

Postby richardk » Sun Jun 21, 2009 10:13 am

There are a few conditions you can use
Code: Select all
# Don't match sub requests (eg. the request to /index.php?file=lorem).
RewriteCond %{ENV:REDIRECT_STATUS} ^$

# Don't match requests to existing files.
RewriteCond %{SCRIPT_FILENAME} !-f

# Don't match requests to existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d

# Don't match requests to specific files.
RewriteCond %{REQUEST_URI} !^/(file1|file2)$ [NC]

# Don't match requests to specific directories.
RewriteCond %{REQUEST_URI} !^/(dir1|dir2)(/.*)?$ [NC]


I'd probably go for
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?file=$1 [QSA,L]

but it depends on your situation.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby sven » Tue Jun 23, 2009 4:41 am

Excellent!
Thank you [:
sven
 
Posts: 2
Joined: Sat Jun 20, 2009 6:30 pm


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 99 guests

cron