Rewriting / directories / extension less urls help needed!

Discuss practical ways rearrange URLs using mod_rewrite.

Rewriting / directories / extension less urls help needed!

Postby whitenoise » Fri Jun 27, 2008 3:23 am

Hi everyone,

I'm sorry to bother you all, but I have been trying do some htaccess rewrites for days now and I am tearing my hair out! I've had assistance from others elsewhere but I can't seem make much progress. I've been studying various help websites, forums, apache documentation but for some reason I just can't get it! PHP no problem - htaccess big problem!

Here is my situation. I have all my 'index' files in the root. Then I have directories (which are called the same as there corresponding root files) which have the supporting files. For example:

In root: blue-widgets.php, red-widgets.php, green-widgets.php
Directories: blue-widgets/, red-widgets/, green-widgets/

Inside blue-widgets/ they might be widget1.php, widget2.php etc

What am I trying to do? Well for a start I would like to remove the .php extension so you can do www.example.com/blue-widget This would bring up the blue-widget file in the root. (the files themselves still have .php extensions).

I would like people who type www.example.com/blue-widgets/ to be redirected to www.example.com/blue-widgets If there is a request for www.example.com/blue-widgets/widget1.php (or something) then simply redirect to www.example.com/blue-widgets/widget1 as the above paragraph.

Hopefully this makes sense, and probably seems really easy to you people! I've been playing around with RewriteConds, Rewrite Rules regular expressions etc but been going round in circles!

Any help is much appreciated! :)
whitenoise
 
Posts: 9
Joined: Fri Jun 27, 2008 2:47 am

Postby richardk » Sat Jun 28, 2008 4:44 pm

I would like people who type www.example.com/blue-widgets/ to be redirected to www.example.com/blue-widgets

You have to stop Apache's default functionality of adding trailing slashes to directory requests. This can be done with DirectorySlash, there is a warning, however
Security Warning

Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.


Try
Code: Select all
Options +FollowSymLinks -Indexes
DirectorySlash Off

RewriteEngine On

## Remove trailing slashes from directories.
# If it's a directory
RewriteCond %{SCRIPT_FILENAME} -d
# Redirect to remove the trailing slash.
RewriteRule ^([^/]+)/$ /$1 [R=301,L]

## Remove .php.
# If it's a .php file.
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

## Add .php.
# If a .php file exists.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])/?$ /$1.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby whitenoise » Sun Jun 29, 2008 1:00 pm

Thanks for that richardk, although when I try to do any of the things I mentioned I get stuck in endless loops?

At the moment I'm trying this from a test directory before pushing the changes live. So consequently my htaccess (and testing files) is in the root of example.com/test/. Once I have (hopefully!) managed to get it working, everything will move to example.com. This might not have any bearing on the problems, but I thought it best to let you know.

Thanks
whitenoise
 
Posts: 9
Joined: Fri Jun 27, 2008 2:47 am

Postby richardk » Mon Jun 30, 2008 12:42 pm

Oops, try adding
Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$

after
Code: Select all
## Remove .php.


If that doesn't work, it needs to be after
Code: Select all
## Add .php.

instead.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby whitenoise » Tue Jul 01, 2008 10:46 am

Thanks again Richard. I've tried that out and that seems to work good. It worked as hoped when it striped the / and it found the page when the .php extension wasn't entered. However if you entered the file with .php it didn't seem to remove it?

Here is the code so far

Code: Select all
Options +FollowSymLinks -Indexes
DirectorySlash Off

RewriteEngine On

## Remove trailing slashes from directories.
# If it's a directory
RewriteCond %{SCRIPT_FILENAME} -d
# Redirect to remove the trailing slash.
RewriteRule ^([^/]+)/$ /test/$1 [R=301,L]

## Remove .php
# If it's a .php file
RewriteCond %{ENV:REDIRECT_STATUS} ^test/$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /test/$1 [R=301,L]

## Add .php.
# If a .php file exists
RewriteCond %{DOCUMENT_ROOT}/test/$1.php -f
RewriteRule ^(.*[^/])/?$ /test/$1.php [QSA,L]


If I could be really cheeky as well, how could I modify the code so that http://www.example.com/directory/directory2/ gets resolved in a similar way to the first rule (the trailing slash removed) but this time gives the php file in the directory2 folder rather than the root?

Thanks, and sorry to be a pain!!
whitenoise
 
Posts: 9
Joined: Fri Jun 27, 2008 2:47 am

Postby richardk » Tue Jul 01, 2008 3:55 pm

However if you entered the file with .php it didn't seem to remove it?

^test/$ should still be ^$.

If I could be really cheeky as well, how could I modify the code so that http://www.example.com/directory/directory2/ gets resolved in a similar way to the first rule (the trailing slash removed) but this time gives the php file in the directory2 folder rather than the root?

Try replacing
Code: Select all
^([^/]+)/$

with
Code: Select all
^(.+)/$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby whitenoise » Wed Jul 02, 2008 12:46 pm

Many thanks for your continued help Richard :) It was my fault with the php files, that now works perfectly.

I implemented the code change regarding trying to get the directory to resolve to the php file, but got a strange result. If I type in www.example.com/test/directory/filename/ I was expecting to get www.example.com/test/directory/filename as in the very first bit of code you kindly provided. I however still got www.example.com/test/directory/filename/ and strangely still got the page, but with all the images missing. The images are setup on relative paths, so it seems to have sort-of worked? I played around with a few things but couldn't get any joy I'm afraid.

Can you help on this final problem - won't bother you again!

Here is the code so far:

Code: Select all
Options +FollowSymLinks -Indexes
DirectorySlash Off

RewriteEngine On

## Remove trailing slashes from directories.
# If it's a directory
RewriteCond %{SCRIPT_FILENAME} -d
# Redirect to remove the trailing slash.
RewriteRule ^(.+)/$ /test/$1 [R=301,L]

## Remove .php
# If it's a .php file
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /test/$1 [R=301,L]

## Add .php.
# If a .php file exists
RewriteCond %{DOCUMENT_ROOT}/dev/$1.php -f
RewriteRule ^(.*[^/])/?$ /test/$1.php [QSA,L]


Thanks! :D
whitenoise
 
Posts: 9
Joined: Fri Jun 27, 2008 2:47 am

Postby richardk » Fri Jul 04, 2008 2:13 pm

You want it to remove the trailing slash from PHP files, too? Try
Code: Select all
Options +FollowSymLinks -Indexes
DirectorySlash Off

RewriteEngine On

## Remove trailing slashes from directories.
# If it's a directory
RewriteCond %{SCRIPT_FILENAME} -d [OR]
# or it's a PHP file.
RewriteCond %{DOCUMENT_ROOT}/test/$1.php -f
# Redirect to remove the trailing slash.
RewriteRule ^(.+)/$ /test/$1 [R=301,L]

## Remove .php
# If it's a .php file
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /test/$1 [R=301,L]

## Add .php.
# If a .php file exists
RewriteCond %{DOCUMENT_ROOT}/dev/$1.php -f
RewriteRule ^(.*[^/])/?$ /test/$1.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby whitenoise » Fri Jul 04, 2008 2:58 pm

That's perfect Richard. Many thanks for all your trouble - you are the mod-rewrite guru! :D
whitenoise
 
Posts: 9
Joined: Fri Jun 27, 2008 2:47 am

Postby whitenoise » Sun Jul 06, 2008 6:00 am

Wonder if I can pick your brains Richardk once more? Sorry!

The code I have is perfect, so thanks again for that, its really appreciated. I've been trying to rewrite my directories to query strings. At the moment, I am trying to turn a request for:

www.example.com/test/directory/filename/filename001

into

www.example.com/test/directory/filename?id=001

But not having much luck. I've made some attempts, but its not just getting the code correct, its putting in the right place. Every time I've tried this, its messed up the excellent code you provided.

At the moment a request for the below works,

www.example.com/test/directory/filename.php?id=001
(it goes to www.example.com/test/directory/filename?id=001)

but can this request still work if the above rewrite is implemented?

Can you advise?
whitenoise
 
Posts: 9
Joined: Fri Jun 27, 2008 2:47 am

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 35 guests

cron