rewrite url to remove slashes in certain cases

-1-

I need to add a rewrite rule to the .htaccess file that does the following:

Rewrite http://www.mysite.com/library/xxx/ to http://www.mysite.com/library/xxx

Rewrite http://www.mysite.com/library/yyy/ to http://www.mysite.com/library/yyy

Rewrite http://www.mysite.com/library/zzz/ to http://www.mysite.com/library/zzz

and so on...

(xxx,yyy,zzz are placeholders for any subdirectory the user would enter after /library/)
SWB-ConsultingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ShaneJonesCommented:
No you don't need that rule! :) I'd leave it as it is as you will probably end up getting HTTP 500 internal server errors because of this!

Out of interest for what reason are you looking to do this. Trailing slashes are good plus Apache will automatically add the trailing slash to http://www.mysite.com/library/xxx

Not sure why you want to do this?

Thanks

Shane
0
ShaneJonesCommented:
That said I have found out how to do it here http://httpd.apache.org/docs/2.0/mod/mod_dir.html look for DirectorySlash Directive you can do this from a small modification to your apache config file. no need for redirects!

Although what you want to do has a security warning! see below

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.

Think wisely about your reasons behind this.

Shane
0
SWB-ConsultingAuthor Commented:
i need this feature because pages with slashes don't render in that directory.

i don't have access to the apache conf because it is a shared server
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

ShaneJonesCommented:
if you have access to your .htaccess file you can try this code
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)(\.php|\.html)/$  /$1$2 [R=301,L]

Open in new window

0
SWB-ConsultingAuthor Commented:
...it's not rewriting it
0
ShaneJonesCommented:
Did you clear your cache before testing it?

If not then you can try and add this to the .htaccess

This *should* override the shared server settings that I mentioned in my second post


DirectorySlash Off 

Open in new window

0
ShaneJonesCommented:
FYI this wont redirect but should not add the trailing slash to URL's like http://www.mysite.com/library/zzz
0
SWB-ConsultingAuthor Commented:
I cleared my cache and it's not redirecting. It should redirect.
0
ShaneJonesCommented:
Right then one last try from me

Add the following to htaccess

This in theory should redirect to the url minus the / and because the DirectorySlash is off it won't add the trailing slash
DirectorySlash Off 
 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)(\.php|\.html)/$  /$1$2 [R=301,L]

Open in new window

0
caterham_wwwCommented:
> (xxx,yyy,zzz are placeholders for any subdirectory the user would enter after /library/)

but those don't exist as a folder, do they? In that case I don't think that you'd have to turn DirectorySlash off. And if you don't call other dirs in that folder directly, you can strip a condition, too.

assuming that there is no /library/.htaccess:

In a /.htaccess
RewriteEngine On 
RewriteRule ^(library/[^/]+)/$  /$1 [R=301,L]
 
redirecting a local filepah starting with library/
[^/]+    followed by 1 or n chars not containing a slash
followed by a slash at the end of the string
 
to an URL-path containing the value matched within (....) parenthesis

Open in new window

0
SWB-ConsultingAuthor Commented:
caterham www: would this also take care of http://www.mysite.com/library/xxx/bbb/ which should be redirected to http://www.mysite.com/library/xxx/bbb ?
0
caterham_wwwCommented:
no, in that case change it to
RewriteRule ^(library/.+)/$  /$1 [R=301,L]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SWB-ConsultingAuthor Commented:
does this also take care of all deeper directory structures, such as http://www.mysite.com/library/xxx/bbb/ccc/ and so on?
0
SWB-ConsultingAuthor Commented:
yes it does, thanks!
0
SWB-ConsultingAuthor Commented:
oh, one issue remains that I didn't think about:

I would also like to transform Rewrite http://www.mysite.com/library/ to http://www.mysite.com/library
0
caterham_wwwCommented:
That dir doesn't exist, too? I'd use an extra rule for that, so that there's no conflict with other directories (or are there more than this one on root level?):


RewriteRule ^(library)$ /$1/ [R=301,L]

Open in new window

0
SWB-ConsultingAuthor Commented:
this directory doesn't exist either, correct. Yes there are a lot of real directories on the root level. Is there a way to incorporate library into the rewrite rule created above so I don't need to enter an extra line?
0
SWB-ConsultingAuthor Commented:
actually the rewrite rule above accomplishes the opposite of what I wanted.

it converts http://www.mysite.com/library to http://www.mysite.com/library/

i need

http://www.mysite.com/library/ to http://www.mysite.com/library
0
caterham_wwwCommented:
ah, sorry
RewriteRule ^(library)/$ /$1 [R=301,L]

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
File Sharing Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.