Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

rewrite url to remove slashes in certain cases

Posted on 2008-10-27
19
Medium Priority
?
621 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

 
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
 
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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With more and more people tethered to the internet, advertisers can reach you in new and creative ways. Push notifications on your smartphone, customized advertisements based on your previous searches, and email inboxes filled with promotional pitch…
Files go missing when using DFS (Distributed File System) Replication and how to recover them and fix it.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
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…
Suggested Courses

661 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