3

I have the following url www.example.com/advice/ now notice the trailing slash at the end of the url? I would like that to be removed to be something like www.example.com/advice. Now when I enter that URL in the browser I get redirected to the with trailing slash, even with curl for example. Now I know the trailing slash is getting added because advice is an actual directory.

[steve@dev dev.www.example.com]$ curl -I  https://dev.www.example.com/advice -k
  HTTP/1.1 301 Moved Permanently
  Date: Fri, 25 Nov 2016 08:20:11 GMT
  Server: Apache/2.4.6 (CentOS)
  Location: https://dev.www.example.com/advice/
  Cache-Control: max-age=0
  Expires: Fri, 25 Nov 2016 08:20:11 GMT
  Connection: close
  Content-Type: text/html; charset=iso-8859-1

[steve@dev dev.www.example.com]$ curl -I https://dev.www.example.com/advice/ -k
HTTP/1.1 200 OK
Date: Fri, 25 Nov 2016 08:21:19 GMT
Server: Apache/2.4.6 (CentOS)
X-Powered-By: PHP/5.6.27
Set-Cookie: flarum_session=mfbou2hcbvcobhncnaqlvl9bm7; Path=/; HttpOnly
X-CSRF-Token: WV69M1oi8POqOcXi6MvwKhbJQ72Tmo2WpFn3oxwq
Content-Length: 10339
Cache-Control: max-age=0
Expires: Fri, 25 Nov 2016 08:21:19 GMT
Vary: Accept-Encoding
Access-Control-Allow-Origin: *.example.com
Connection: close
Content-Type: text/html; charset=UTF-8

What I tried so far


Inside of the .htacess I tried:

DirectorySlash Off

Which resulted into a 403 without the slash

[steve@dev dev.www.example.com]$ curl -I https://dev.www.example.com/advice -k
  HTTP/1.1 403 Forbidden
  Date: Fri, 25 Nov 2016 08:53:15 GMT
  Server: Apache/2.4.6 (CentOS)
  Connection: close
  Content-Type: text/html; charset=iso-8859-1

I also added the following rewrite rule but nothing changed

RewriteRule ^.*(advice)\/$ $1 [L,R=301]

More information


Now advice is where I have a forum platform installed, in the same directory I have a CMS installed, something like below

├── advice
│   ├── admin.php
│   ├── api.php
│   ├── assets
│   ├── composer.json
│   ├── composer.lock
│   ├── config.php
│   ├── CONTRIBUTING.md
│   ├── index.php <-- Forum entry point
│   ├── LICENSE
│   ├── Procfile
│   ├── readme.md
│   ├── scripts
│   ├── storage
│   ├── Vagrantfile
│   └── vendor
├── assets
├── cms
├── codeception.yml
├── commands
├── composer.json
├── composer.lock
├── crontask
├── dropzone
├── email-helpers
├── favicon.ico
├── framework
├── gridfield-bulk-editing-tools
├── gridfieldextensions
├── index.php <-- CMS Entry point
├── kickassets
├── liveseo
├── minify
├── README.md
├── reports
├── setup.php
├── shortcodable
├── silverstripe-cache
├── siteconfig
├── _ss_environment.php
├── tests
├── themes
├── trait-loader
├── vendor
└── web.config

Apache version Apache/2.4.6 (CentOS)

You can find the advice .htaccess here and the one in the cms here

If it helps the CMS is silverstripe and forum is flarum

Steve
  • 135
  • 1
  • 5
  • Some more information I also added `RewriteRule ^.*advice\/$ about-us [r=301,nc]` just to test it, it doesn't redirect to the about-us page. – Steve Nov 25 '16 at 09:41

1 Answers1

5

If you remove the slash on a directory and still expect the directory index document (in that directory) to be returned then you'll need to manually "fix" the URL by internally rewriting to the index document. (You basically need to redo what you have undone by turning the DirectorySlash Off.)

Try something like the following before other mod_rewrite directives in the root .htaccess file:

DirectorySlash Off

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule (.*) $1/index.php [L]

For all requests for physical directories, that don't end in a slash where index.php exists in that directory then internally rewrite to index.php.

Alternatively, you could just append the trailing slash (via an internal rewrite) and then allow mod_dir to issue a subrequest to the DirectoryIndex. For example:

RewriteRule (.*) $1/ [L]

Assuming you have a DirectoryIndex set (which you probably do have if this was working previously) then mod_dir will then internally rewrite the request (strictly a subrequest) from /advice/ to /advice/index.php. However, this is now a two stage process, so you might as well do this all in one with mod_rewrite (unless you have varying directory index documents).


The above is a general case for all directories. You could be more specific and check only for the directory/URL in question:

RewriteRule ^advice$ advice/index.php [L]

If the request is for /advice then internally rewrite to /advice/index.php - this does not check whether it is a directory, that is just assumed.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • I get 403 forbidden when I that the rewrite rules you told me. Mind if you exaplin this `Alternatively, you could just append the trailing slash (via an internal rewrite) and then allow mod_dir to issue a subrequest to the DirectoryIndex.` – Steve Nov 25 '16 at 10:05
  • Make sure your browser cache is cleared. These will need to go near the top of the `.htaccess` file in the document root (not the one in the `/advice` subdirectory) - sorry, just noticed your comment suggesting the two separate `.htaccess` files. I'll update my answer regarding the last comment. – MrWhite Nov 25 '16 at 10:40
  • hmm, just tried that, still didn't work (put it on top) and I am using curl as well to test to make sure I don't hit bower cache issues. – Steve Nov 25 '16 at 10:44
  • Nothing worked, I'm going to check if the forum might have something in its code that might be doing this. And I just check if it from the applications side as well but deson't seem to be (just returing a string in the index file) – Steve Nov 25 '16 at 10:49
  • And by "didn't work" - you're still getting a 403? If that's the case then the above code doesn't appear to be getting processed - maybe it's being overridden by other directives/.htaccess files? – MrWhite Nov 25 '16 at 10:52
  • Yes didn't work as in keeps giving me 403, ill go trough every .htacess file see if I am missing something. – Steve Nov 25 '16 at 12:16