【问题标题】:.htaccess file with some exceptions.htaccess 文件,但有一些例外
【发布时间】:2014-04-24 21:08:09
【问题描述】:

我需要一个遵循以下规则的 .htaccess 文件:

  • images、css、js、admin、fonts 文件夹的所有文件(和子文件夹/子文件)都应该是可访问的
  • 像 bla.com/info 这样的 URL 应该被重定向到 bla.com/index.php?p=info
  • 像 bla.com/products/22 这样的网址应该重定向到 bla.com/index.php?p=products&cat=22
  • 网址 bla.com/products 应重定向到 bla.com/index.php?p=products

谁能帮我解决这个问题?这是我到目前为止得到的:

Options +FollowSymLinks

RewriteEngine On
RewriteBase /staging/

RewriteRule     ^favicon.ico           favicon.ico         [NC,L]
RewriteRule     ^([A-Za-z0-9-]+)/?$     index.php?p=$1       [NC,L] 

但它只适用于前两个条件...

谢谢!!

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    这两条规则应该适合你:

    Options +FollowSymLinks
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    RewriteRule ^([a-z0-9-]+)/?$ index.php?p=$1 [NC,QSA,L]
    
    RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ index.php?p=$1&cat=$2 [NC,QSA,L]
    

    【讨论】:

    • 这似乎适用于页面,但图像植根于产品页面上的 bla.com/products/images/bla/bla.jpg
    • 好的,这是常见问题。您应该在 css、js、图像文件中使用绝对路径,而不是相对路径。这意味着您必须确保这些文件的路径以http:// 或斜杠/ 开头。您可以尝试将其添加到页面的 HTML 标头中:<base href="/" />
    • 好的,将所有内容都更改为绝对网址,它就像一个魅力!
    【解决方案2】:

    试试:

    Options +FollowSymLinks
    
    RewriteEngine On
    RewriteBase /staging/
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([A-Za-z0-9-]+)(?:/(.*?)/?|)$ index.php?p=$1&cat=$2 [L,QSA]
    

    【讨论】:

      猜你喜欢
      • 2014-04-26
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多