apache - 适合多个和长查询的友好 URL

标签 apache .htaccess redirect mod-rewrite

我尝试了几个不同的问题:

  1. php - .htaccess make URL user friendly for multiple and dynamic parameters
  2. .htaccess for friendly URL with multiple variables
  3. User-friendly URLs instead of Query Strings?

上面的问题没有用,所以我开始开发一种替代方案,但我的代码在多个请求(code1)中返回 404 错误,并且在一个请求中运行良好(代码2)所以:

(code1) - 不起作用 - 多次请求 (code2) - 工作正常 - 单个请求

代码1


    Options +FollowSymLinks -MultiViews
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /~loja/index.php?a=$1&genero=$2&material=$3&cor=$4&tamanho=$5&Ordenacao=$6 [NC,L,QSA]
    RewriteCond %{THE_REQUEST} ^.*/index\.php 
    RewriteRule ^(.*)index.php$ /~loja/$1/$2/$3/$4/$5/$6 [R=301,L]  
    DirectoryIndex index.php

现在查看正确的代码:

代码2


    Options +FollowSymLinks -MultiViews
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9_-]+)$ /~loja/index.php?a=$1 [NC,L,QSA]
    RewriteCond %{THE_REQUEST} ^.*/index\.php 
    RewriteRule ^(.*)index.php$ /~loja/$1 [R=301,L]  
    DirectoryIndex index.php

(code1)有什么问题?

最佳答案

第一个规则集不起作用,因为目标 URL 包含 7 个段,但 RewriteRule 匹配 6 个段。如果您在 RewriteRule 中的 $ 之前添加另一个 /([^/]*),它将起作用。

所以,它是:

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /~loja/index.php?a=$1&genero=$2&material=$3&cor=$4&tamanho=$5&Ordenacao=$6 [NC,L,QSA]

您可以通过在末尾附加 ? 字符来使段/字符成为可选。

第一个规则集的第二部分是完全错误的并且没有任何作用。首先,您应该注意到 THE_REQUEST 变量包含完整的 HTTP 请求行,如 documentation states :

THE_REQUEST contains the full HTTP request line sent by the browser to the server (e.g., GET /index.html HTTP/1.1). This does not include any additional headers sent by the browser. This value has not been unescaped (decoded), unlike most other variables below.

如果您不想使用它,请改用 REQUEST_URI

404

您收到 404 错误的原因是,由于 RewriteRule 无法匹配请求 URI,因此 URL 不会被重写为 index.php。文件系统上不存在该目录层次结构,因此出现 404。

可选的 URI 段

您的原始规则集需要 URI 参数的精确计数。如果您需要对此更加灵活,您可以尝试此规则集,它接受 1-7 个段并将它们映射到查询字符串:

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond %{REQUEST_URI} !index\.php
    RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)$ /~loja/index.php?a=$1&genero=$2&material=$3&cor=$4&tamanho=$5&Ordenacao=$6 [NC,L,QSA]
</IfModule>

测试时,请注意空查询字符串。在 PHP 方面,您需要使用 empty() 而不是 isset() 来查看查询字符串是否存在。因为它们总是已设置,但为空。

不包括admin/

为了排除此规则应用于 admin/ 路径,您可以像这样更新上面的 RewriteCond:

RewriteCond %{REQUEST_URI} !(index\.php|admin)

关于apache - 适合多个和长查询的友好 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41549370/

相关文章:

python - mod_wsgi 测试 wsgi 运行良好,应用程序给出 HTTP 500

.htaccess - nginx 上的细香葱,htaccess

.htaccess - 将站点强制为 HTTPS,某些页面和 Facebook 爬虫除外

javascript - 重定向 localhost 而不是 myserver.org :3000/login/callback

c# - 单个 NetworkCredential 实例是否仅适用于单个 URI

php - 为什么 CakePHP 授权组件在并行打开两个站点时会自动注销?

php.ini 不允许我禁用_functions

node.js - 在 node.js 服务器上从 http 重定向到 https

linux - htaccess 规则在 httpd.conf 中工作时不起作用

.htaccess - 重定向和 SEO(SSL 重定向)