大网数据广告活动
网站运营与故障问答

How to remove slash from directory when there are more rewrite rules in the subdirectories on Apache?

· 52CE 我爱测

I've an Angular applicaion in multiple languages. After the build process there exist a distribution for each language. These are put onto the server in locale directories: /en/index.html /fr/index.html Also all the assets, JavaS…

问题原文

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

I've an Angular applicaion in multiple languages. After the build process there exist a distribution for each language. These are put onto the server in locale directories:

/en/index.html
/fr/index.html

Also all the assets, JavaScript files etc. are located per language in each directory. There's also a .htaccess file in each locale directory - only their last RewriteRule differs per language:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /fr/index.html [L]
</IfModule>

Now the user is redirected based on the browser's language using PHP. So there's no .htaccess in the root folder yet.

The URLs look like:

example.com/fr/
example.com/fr/contact

I want to get rid of the slash of the root directory:

example.com/fr

How can I solve this?


I tried DirectorySlash off without success. I was also thinking about having only one single .htaccess in the root that handles the local directory and requests for all languages. But I can't figure out, how to solve it.

已采纳解答

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

  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /fr/index.html [L]

Aside: If you removed the RewriteBase directive and the /fr/ prefix on the RewriteRule substitution then all your .htaccess files in the locale subdirectories would be the same.

However, I would go for creating a single .htaccess file in the document root (parent directory) instead, as you suggest.

In order to remove the trailing slash from the language subdirectory you do need the DirectorySlash Off directive - in the root .htaccess file. However, you also need to manually rewrite the request to append the slash in order to "fix" the URL. (mod_dir etc. won't work correctly without the trailing slash.)

Try something like the following in the root .htacces file, instead of multiple .htaccess files in subdirectories:

# Prevent mod_dir from appending the trailing slash to directories (with a 301 redirect)
DirectorySlash Off
DirectoryIndex index.html

RewriteEngine On

# Allow mod_rewrite to match directories that do not have a trailing slash (when DirecrorySlash Off)
RewriteOptions AllowNoSlash

# Rewrite directory without trailing slash directly to the front-controller
RewriteRule ^([a-z]{2})$ $1/index.html [L]

# Front-controller to respective language controller
RewriteRule ^[a-z]{2}/index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/. $1/index.html [L]

Currently this does not do anything with requests for the document root, or "files" therein.

Also, any request for a language code that simply looks-like a language code is routed to that fron-controller, whether it exists or not (404). eg. /ab/contact is routed to /ab/index.html.

A request for /fr/ (with a trailing slash) - if the user should request it - is handled by mod_dir in the usual way and will naturally issue an internal subrequest for index.html (if the directory exists) - the DirectoryIndex. This could be externally redirected to remove the trailing slash, although you should already be linking to the slash-less URL and the appropriate canonical URL set - so it shouldn't be an SEO issue (unless the slashed URL is indexed/linked to in the wild).

Note that by disabling the DirectorySlash, this is then disabled for all child directories as well, unless you explicitly override this.

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

资料来源与适用范围

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

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

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