简体   繁体   English

如何使用 .htaccess 重写 URL 路径以查询字符串

[英]How can I rewrite URL paths to query strings using .htaccess

How can I get $_GET requests from each existing PHP file from directory listings in my URL, like this:如何从我的 URL 中的目录列表中获取每个现有 PHP 文件的 $_GET 请求,如下所示:

# Scenario 1: Assuming existence of file.php
    URL like: /path/to/file/v1/v2/v3 ...
    Act like: /path/to/file.php?v1&v2&v3
              or
              /path/to/file.php?query=[v1,v2,v3 ...]
    HTTP GET: array(v1 => null,v2 => null, v3 => null, ...) or array(v1, v2, v3)
    or
    HTTP GET: array(query => [v1,v2,v3])

# Scenario 2: Assuming existence of file.php
    URL like: /path/to/file/k1/v1/k2/v2 ...
    Act like: /path/to/file.php?k1=v1&k2=v2 ...
    HTTP GET: array(k1 => v1, k2 => v2, ...)

Since I'm not familiar enough with Apache configs, hope someone could help me with that.由于我对 Apache 配置不够熟悉,希望有人能帮助我。

Just note that I already have these codes for other reasons in my .htaccess file:请注意,由于其他原因,我的 .htaccess 文件中已经有了这些代码:

# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]

# To remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# To check whether the PHP file exists and set it back internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L]

# To redirect /index to root
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

Thanks谢谢

You have to use mod-rewrite for redirecting based on query string:您必须使用 mod-rewrite 进行基于查询字符串的重定向:

Make sure in your httpd.conf, the following lines are uncommented:确保在您的 httpd.conf 中,取消注释以下行:

LoadModule rewrite_module modules/mod_rewrite.so

And here is an example:这是一个例子:

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)thiscontent=2660(&|$)
RewriteRule ^/?applications/newWeb/www/index\.php$ http://www.example.come/index.php/folder1/foler2/folder-resources? [L,R=301]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM