.htaccess htacces 2变量问题
之前我安装了SSL,一切正常!!以下是我在根webserver.htaccess文件中的代码:.htaccess htacces 2变量问题,.htaccess,ssl,ssl-certificate,.htaccess,Ssl,Ssl Certificate,之前我安装了SSL,一切正常!!以下是我在根webserver.htaccess文件中的代码: Options +MultiViews RewriteEngine On RewriteCond %{HTTP_HOST} andrea\.com [NC] RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://andrea.com/$1 [R,L] RewriteCond %{DOCUMENT_ROOT}/$1.php -f Rewrit
Options +MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} andrea\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://andrea.com/$1 [R,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
它很有效,而且完全符合我的要求。例如,如果我去:
www.andrea.com/account
它访问“www.andrea.com/account.php”。这就是我想要的。
不过,我在根目录中有一个名为“products”的文件夹。该文件夹中还有另一个“.htaccess”文件,我不知道这两个文件中的哪一个必须更改才能使下面的工作正常
当您转到此url时:
http://products/查看/Hello/再见
我希望它访问“products”文件夹中的“view.php”,在该php文件中,我可以执行以下操作:
$id = $_GET["id"]; // This would have "Hello"
$cat = $_GET["cat"]; // This would have "Goodbye"
当我在“产品”文件夹中使用此htaccess时,这项功能运行良好:
以上代码的问题是,如果我转到:
http://products/Hello/再见
我想让它访问“产品”文件夹中的“index.php”。但是它改为“view.php”!!这就像上面的htaccess代码强制所有人都转到view.php(只有在url中有“view/___;”时才应该这样做)。
我希望上面的url转到“products”文件夹中的“index.php”,在该文件中,我应该能够访问ID和CAT变量
你知道在我的.htaccess文件中要更改什么吗?抱歉,我花了两个多小时,我不理解代码底部的一行,但它不起作用://你可以使用以下规则将
/products/Hello/bye
重写为/products/index.php
RewriteRule ^Hello/GoodBye/?$ /product/index.php?id=hello&cat=Goodbye [L,NC]
这是您完整的/product/.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#rewrite /products/Hello/GoodBye to /products/index.php
RewriteRule ^Hello/GoodBye/?$ /products/index.php?id=Hello&cat=Goodbye [L,NC]
###################
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?(.*)$ /products/view.php?id=$1&cat=$2
首先,您应该禁用多视图。在您的问题中,我建议使用多视图是一种严格意义上的“替代”方法。您不能同时使用这两种方法(mod_rewrite和MultiViews)使用无扩展URL。由于您现在想做更多的事情并传递参数,多视图只会产生冲突。(多视图可能会“获胜”,并且不会传递任何参数。)
另外,您是否特别需要/products
子目录中的附加.htaccess
文件?在文档根目录中有一个.htaccess
文件(可以说)会更容易。这将避免重复HTTP到HTTPS重定向(尽管您实际上没有在子目录.htaccess
文件中包含HTTP到HTTPS重定向?)
此指令同时匹配view/Hello/debye
和Hello/debye
,这解释了为什么所有内容都要写入view.php
脚本。但是,它实际上也没有执行您所说的操作-这令人费解。如果您请求/products/view/Hello/debye
,那么它会将请求重写为views.php
之前,您需要在请求的URL路径中实际检查视图
。如果视图
不存在,则改为重写到index.php
。这种“条件分支”可以通过简单地按照“更具体”的顺序排列指令来实现规则第一
例如,在根.htaccess
文件中尝试以下操作(并将/products/.htaccess
文件一起删除)
#确保禁用多视图
选项-多视图
重新启动发动机
#HTTP到HTTPS规范重定向
重写cond%{HTTP_HOST}example\.com[NC]
重写cond%{SERVER_PORT}80
重写规则(.*)https://example.com/$1[R=301,L]
#如果请求已映射到(或看起来像)文件或目录,则提前中止
重写条件%{REQUEST_URI}\.\w{2,4}$[或]
RewriteCond%{REQUEST_FILENAME}-f[或]
RewriteCond%{REQUEST_FILENAME}-d
重写规则^-[L]
#1.将“/products/view/”重写为“/products/view.php?id=&cat=
重写规则^(产品/视图)/([^/]*)/?(.*)$1.php?id=$2&cat=$3[L]
#2.将“/products//”重写为“/products/index.php?id=&cat=
重写规则^(products)/([^/]*)/?(.*)$1/index.php?id=$2&cat=$3[L]
#3.其他请求的无扩展URL
重写cond%{DOCUMENT_ROOT}/$1.php-f
重写规则(.*)$1.php[L]
上面3条规则的顺序很重要。最具体的规则是第一条。包括L
标志以防止进一步(不必要)处理
请注意,根据您的原始指令,对于形式为/products/view/Hello/bye
(或/products/Hello/bye
)的请求,Hello/bye
部分是完全可选的,自然会导致设置id
和cat
URL参数,但为空
另外,根据您的原始指令,形式为/products/view/Hello/bye/foo/bar/baz
的请求将导致cat
URL参数设置为bye/foo/bar/baz
(初始路径段后面的任何内容)
如果您使正则表达式更具体,并且只匹配需要匹配的内容,则不一定需要检查请求是否映射到文件或目录(相对昂贵)。例如,您的正则表达式/([^/]*)/?(*)
当前几乎匹配任何内容。但是如果
和
变量只能由小写字母组成(例如),则可以避免文件系统检查的需要
其他说明:
- 是否需要检查HTTP到HTTPS重定向中的主机名?是否承载多个域?否则,不需要检查
服务器变量的条件HTTP\u host
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#rewrite /products/Hello/GoodBye to /products/index.php
RewriteRule ^Hello/GoodBye/?$ /products/index.php?id=Hello&cat=Goodbye [L,NC]
###################
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?(.*)$ /products/view.php?id=$1&cat=$2
Options +MultiViews
# /products/.htaccess
RewriteRule ^([^/]*)/?(.*)$ /products/view.php?id=$1&cat=$2
# Ensure that MultiViews is disabled
Options -MultiViews
RewriteEngine On
# HTTP to HTTPS canonical redirect
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://example.com/$1 [R=301,L]
# Abort early if the request already maps to (or looks like) a file or directory
RewriteCond %{REQUEST_URI} \.\w{2,4}$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# 1. Rewrite "/products/view/<id>/<cat>" to "/products/view.php?id=<id>&cat=<cat>
RewriteRule ^(products/view)/([^/]*)/?(.*) $1.php?id=$2&cat=$3 [L]
# 2. Rewrite "/products/<id>/<cat>" to "/products/index.php?id=<id>&cat=<cat>
RewriteRule ^(products)/([^/]*)/?(.*) $1/index.php?id=$2&cat=$3 [L]
# 3. Extensionless URLs for other requests
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]