Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 639
  • Last Modified:

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/)
0
SWB-Consulting
Asked:
SWB-Consulting
  • 9
  • 6
  • 4
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 9
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now