【问题标题】:htaccess list directory and rewrite rules for ithtaccess 列出目录并为其重写规则
【发布时间】:2021-04-08 22:58:07
【问题描述】:

我正在尝试制作一个 .htaccess 代码来自动重写目录的规则。 例如:

URL: https://example.com/assets/test.png
Folder: /src/assets/test.png

或者,

URL: https://example.com/js/test.js
Folder: /src/js/test.js

我的 htaccess 文件:

#Rewrite to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]

#remove html file extension
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

#remove php file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L] 

我该怎么做?

【问题讨论】:

  • 请在您的问题中分享您的 htaccess 文件,谢谢。

标签: list .htaccess redirect mod-rewrite cpanel


【解决方案1】:

您能否在您的 .htaccess 文件中尝试以下规则一次。请确保在测试 URL 之前清除浏览器缓存。

RewriteRule ([\w-]+)\.png/?$ src/assets/$1.js [R=301,NC,L]

【讨论】:

  • 我可以将它与其他文件扩展名一起使用吗?不仅仅是js或者png
  • @BenMert,是的,您可以尝试从特定格式重写/重定向到另一种特定格式(通过在 .htaccess 文件中添加特定的更多规则),根据您的问题,我是从 .png 编写的到 .js 让我知道这是否适合你。
猜你喜欢
  • 1970-01-01
  • 2019-03-13
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多