Solved

rewrite url to remove slashes in certain cases

Posted on 2008-10-27
19
600 Views
Last Modified: 2013-11-22
-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/)
0
Comment
Question by:SWB-Consulting
  • 9
  • 6
  • 4
19 Comments
 
LVL 12

Expert Comment

by:ShaneJones
ID: 22818069
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
 
LVL 12

Expert Comment

by:ShaneJones
ID: 22818103
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
 

Author Comment

by:SWB-Consulting
ID: 22818108
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
 
LVL 12

Expert Comment

by:ShaneJones
ID: 22818130
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
 

Author Comment

by:SWB-Consulting
ID: 22818170
...it's not rewriting it
0
 
LVL 12

Expert Comment

by:ShaneJones
ID: 22818194
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
 
LVL 12

Expert Comment

by:ShaneJones
ID: 22818207
FYI this wont redirect but should not add the trailing slash to URL's like http://www.mysite.com/library/zzz
0
 

Author Comment

by:SWB-Consulting
ID: 22818400
I cleared my cache and it's not redirecting. It should redirect.
0
 
LVL 12

Expert Comment

by:ShaneJones
ID: 22819737
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 27

Expert Comment

by:caterham_www
ID: 22844179
> (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
 

Author Comment

by:SWB-Consulting
ID: 22846202
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
 
LVL 27

Accepted Solution

by:
caterham_www earned 500 total points
ID: 22846382
no, in that case change it to
RewriteRule ^(library/.+)/$  /$1 [R=301,L]

Open in new window

0
 

Author Comment

by:SWB-Consulting
ID: 22846437
does this also take care of all deeper directory structures, such as http://www.mysite.com/library/xxx/bbb/ccc/ and so on?
0
 

Author Comment

by:SWB-Consulting
ID: 22846453
yes it does, thanks!
0
 

Author Comment

by:SWB-Consulting
ID: 22847634
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
 
LVL 27

Expert Comment

by:caterham_www
ID: 22848844
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
 

Author Comment

by:SWB-Consulting
ID: 22852794
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
 

Author Comment

by:SWB-Consulting
ID: 22852840
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
 
LVL 27

Expert Comment

by:caterham_www
ID: 22856416
ah, sorry
RewriteRule ^(library)/$ /$1 [R=301,L]

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

[Part 6 of a 6 part series called SEO Basics: 5 SEO Secrets for Creating Content that Drives Traffic (http://www.experts-exchange.com/Web_Development/Internet_Marketing/Search_Engine_Optimization_SEO/A_8369-SEO-Basics-5-SEO-Secrets-for-Creating-Cont…
Cloud file services can fill many different roles for your business. Often, the use of cloud file services begins with employees using consumer products, like Dropbox, to share files with customers and each other. While sync-and-share can be an effe…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now