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

On Apache how to switch off DirectorySlash only for requests to a specific subdomain?

· 52CE 我爱测

Served by Apache I'd like on one subdomain site of mine (say sub.mydomain.com ) that URLs without trailing slashes point directly (without external redirect) to the index file in the underlying folder. The subdomain requests are …

问题原文

原帖链接 · halloleo · CC BY-SA 4.0

Served by Apache I'd like on one subdomain site of mine (say sub.mydomain.com) that URLs without trailing slashes point directly (without external redirect) to the index file in the underlying folder. The subdomain requests are internally redirected to a sub-folder. All other URLS should work in the normal Apache way with external redirect to the slashed version.

All the directives have to go in my .htaccess file. For this to work I am planning to do the following:

  1. Switch off DirectorySlash for requests to sub.mydomain.com/...
  2. Rewrite the sub.mydomain.com/… requests to /sub/...
  3. Rewrite slashless directory URLs with /sub/... to fetch the index.html inside the underlying directory

I have a good idea how to do 2. and 3., but how can I issue DirectorySlash off only for requests to sub.mydomain.com, but not to www.mydomain.com or other.mydomain.com?

已采纳解答

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

By the sounds of it, your sub subdomain maps to the same directory as the main domain and all other subdomains (www and other, etc.)

However, if all requests to the sub subdomain are internally rewritten to the /sub subdirectory then you can presumably just create another .htaccess at /sub/.htaccess in which you set DirectorySlash Off - this then applies to all requests to the sub subdomain. (Assuming you don't also access the same subdirectory via a different hostname. You can prevent this if you wish.)

The /sub/.htaccess file is also where you would be implementing #3 in your requirements.

The root .htaccess file simply rewrites all requests to the sub subdomain to the /sub subdirectory.


Aside: Since you are asking this question on ServerFault it is generally assumed that you have full control of the server. In which case it would be preferable to configure this sub subdomain in its own vHost container that points directly to the /sub subdirectory (or somewhere outside of the main domains directory tree ideally). In this case, you would not need to implement #2 of your requirements and you just set DirectorySlash Off for the entire subdomain.

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

资料来源与适用范围

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

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

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