Can't get mod_rewrite to work within a directory...

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

Can't get mod_rewrite to work within a directory...

Postby jg_forum9 » Sun Oct 25, 2009 3:36 am

Hello folks,

Trying to do something i've not done before and i'm failing miserably!

website structure is as follows:

domain.com/collections/ (a directory which displays its index.php with a list of products)
domain.com/biography.php
domain.com/somethingelse.php etc

and in the collections directory i'm trying to get a url like:

domain.com/collections/item

to rewrite as domain.com/collections/index.php?col=item - so i can grab the col $_GET value and display the correct product


At the moment i'm using (in the root directory):

Code: Select all
Options +FollowSymlinks
RewriteEngine On

RewriteRule ^collections/([^/]+) collections/index.php?col=$1 [NC]


But it's not working - I just get a 404

Is there something wrong with my RewriteRule? Or am I missing something when dealing with a directory like this?

Do i even need to use a directory, or should i just go with a collections.php file, and adjust my Rule to remove the .php and add the /item on the end?

Just a bit confused, help appreciated!

Thanks

James
jg_forum9
 
Posts: 4
Joined: Sun Oct 25, 2009 3:19 am

Postby richardk » Sun Oct 25, 2009 11:05 am

Where are you putting the mod_rewrite?

You do not need a directory. Try the following in /.htaccess
Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^collections/([^/]+)/?$ /collections.php?col=$1 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jg_forum9 » Sun Oct 25, 2009 4:04 pm

Wow! Thanks, that works a treat!

What was I missing? I assume it's something to do with the ? bit at the end of

Code: Select all
^collections/([^/]+)/?$


Mind you, the only problem now is, it doesn't work when going to

domain.com/collections

Because there's no rule to rewrite collections to collections.php - can I add this without breaking the new rule you've given me? Something like:

Code: Select all
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1\.php [L]


Hmm?

James
jg_forum9
 
Posts: 4
Joined: Sun Oct 25, 2009 3:19 am

Postby jg_forum9 » Sun Oct 25, 2009 4:16 pm

ouch! no, my .php rewrite works but breaks the code you gave me - I get a 500 when going to domain.com/collections/item

So we need some kind of rule that does both....



James
jg_forum9
 
Posts: 4
Joined: Sun Oct 25, 2009 3:19 am

Postby richardk » Mon Oct 26, 2009 10:26 am

What was I missing? I assume it's something to do with the ? bit at the end of

No. I'm not sure why it didn't work. Was it an Apache 404 error? It might need an absolute path to the index.php file (/collections/index.php?col=$1).

So we need some kind of rule that does both....

For collections only
Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^collections(?:/([^/]+))?/?$ /collections.php?col=$1 [NC,QSA,L]


For all PHP files (FAQ: Removing (.php) file extensions)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^collections/([^/]+)/?$ /collections.php?col=$1 [NC,QSA,L]

# Redirect to remove /index.php files.
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

# Redirect to remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]

# Rewrite to add .php back.
RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jg_forum9 » Tue Oct 27, 2009 9:47 am

Thanks Richard, spot on!

I have swapped out your .php code with the non-trailing slash version from the FAQ's, now working exactly how I wanted!


Thank you


James
jg_forum9
 
Posts: 4
Joined: Sun Oct 25, 2009 3:19 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 8 guests

cron