【问题标题】:multiple complex .htaccess RewriteRules多个复杂的 .htaccess RewriteRules
【发布时间】:2021-03-04 01:06:18
【问题描述】:

我需要帮助在我的 .htaccess 文件中设置 RewriteRules。我需要首先检查任何现有文件,然后进行一些自定义重写,如果其中任何一个不匹配,请将其重写为 index.php

我当前的.htaccess,位于文档根目录,文件如下:

RewriteEngine On

RewriteBase /

# Don't rewrite existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

# Custom rewrites
# TODO: Don't know how to make these work
# RewriteRule ^/css/style\.css$  /css/style.css.php [L,NC,QSA]
# RewriteRule ^/js/config\.js$   /js/config.js.php [L,NC,QSA]
# RewriteRule ^/js/post\.js$     /js/post.js.php [L,NC,QSA]

# If any of the above don't match hand it to index.php
RewriteRule ^(.+)$ index.php [QSA,L]

php_value upload_max_filesize 10485760
php_value post_max_size 10485760

提前致谢!

【问题讨论】:

    标签: regex apache .htaccess redirect url-rewriting


    【解决方案1】:

    这样吧:

    RewriteEngine On
    RewriteBase /
    
    # Custom rewrite rule
    RewriteRule ^(?:css/style\.css|js/(?:config|post)\.js)$ $0.php [L,NC]
    
    # Don't rewrite existing files
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    # If any of the above don't match hand it to index.php
    RewriteRule . index.php [L]
    

    这是新规则:

    RewriteRule ^(?:css/style\.css|js/(?:config|post)\.js)$ $0.php [L,NC]
    

    匹配 /css/style.css/js/config.js/js/post.js 并在这些 URI 的末尾添加 .php$0 是完整匹配的反向引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多