大网数据广告活动
服务器运维问答

Drop trailing slash in apache for directory

· 52CE 我爱测

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

问题原文

原帖链接 · Steve · CC BY-SA 3.0

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

社区高分解答

原帖链接 · MrWhite · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-11-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。