【问题标题】:Apache cannot detect and rewrite URLs to lowercaseApache 无法检测到 URL 并将其重写为小写
【发布时间】:2018-12-03 17:15:08
【问题描述】:

我想达到什么目的?

我想将 /migrate 目录(和子目录)中的所有 URL 转换为小写。

我做了什么?

我已将我的网站从 DNN 迁移到 Wordpress。因此,我的帖子中的 URL 引用了大小写字母组合的图像。

我在磁盘上 /migrate 文件夹(和子文件夹)中的所有目录和文件都是小写的。

当我查看一篇文章时,如果对图片的引用包含大写字符,我会收到预期的 404。

例如:

https://xxx.domain.net/wp-content/migrate/ABC/image.jpg

https://xxx.domain.net/wp-content/migrate/Xyz/image.jpg

https://xxx.domain.net/wp-content/migrate/abc/Image.jpg

https://xxx.domain.net/wp-content/migrate/ABC/Xyz/image.jpg

所以在网上和这里阅读了各种文章后,我尝试了一些事情:

1) 启用 mod_speling 并将其添加到 apache2.conf

LoadModule speling_module /usr/lib/apache2/modules/mod_speling.so
<IfModule mod_speling.c>
CheckSpelling On
CheckCaseOnly On
</IfModule>

这不起作用,继续得到 404。

2) 厌倦了在我的 /etc/sites-enabled/mysite.conf 文件中使用 Rewritemap。

RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} /migrate [A-Z]
RewriteRule  ^(.*)$ /${lc:$1} [R=301,L]

但是,这会将 所有 URL 重写为小写,并且由于磁盘上的文件包含大写字符,它会破坏一些 Wordpress 插件和编辑器。

我在Wordpress根目录下的.htaccess文件如下:

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]

RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

#BEGIN Wordpress

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

# END WordPress

【问题讨论】:

    标签: mod-rewrite apache2 lowercase rewritemap


    【解决方案1】:

    经过数小时尝试不同的事情并阅读日志,我已经解决了这个问题!

    基本上,我需要忽略所有其他 Wordpress 目录进行重写。

    RewriteMap lc int:tolower
    RewriteCond %{REQUEST_URI} [A-Z]
    RewriteCond %{REQUEST_URI} !^/wp-content/uploads/
    RewriteCond %{REQUEST_URI} !^/wp-content/themes/
    RewriteCond %{REQUEST_URI} !^/wp-content/plugins/
    RewriteCond %{REQUEST_URI} !^/wp-includes/
    RewriteCond %{REQUEST_URI} !^/wp-admin/
    RewriteRule ^/?(.*)$ /${lc:$1} [R=301,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 2011-06-17
      • 1970-01-01
      相关资源
      最近更新 更多