用一个或多个参数重写URL?

时间:2023-02-09 10:52:00

I'm strugglening with my .htaccess file in orther to achieve this:

我正在努力使用我的.htaccess文件来实现这个目标:

a.com/male-items  (OR)     
a.com/male-items/popularity   ->    a.com/index.php?g=m&sort-popularity 

a.com/female-items  (OR)     
a.com/female-items/popularity ->    a.com/index.php?g=f&sort=popularity 

a.com/male-items/alphabet     ->    a.com/index.php?g=m&sort=alphabet
a.com/male-items/alphabet/a   ->    a.com/index.php?g=m&sort=alphabet&l=a    
(and same for female)

I know it should be something like

我知道应该是这样的

RewriteRule ^a$ a.com/index.php?q=$1

But actually looking into the different mod-rewrite / regex explanations and cheat-sheets doesn't help a lot with getting it to work. The hard part is to understand how do you define the different parametes in the address and then use them in the rewritten url.

但实际上,查看不同的mod-rewrite / regex解释和备忘单对于让它工作起来并没有多大帮助。困难的部分是了解如何在地址中定义不同的参数,然后在重写的URL中使用它们。

(any explanations with your solution would be appretiated)

(对您的解决方案的任何解释都会被认可)

2 个解决方案

#1


1  

Use these rules in your DOCUMENT_ROOT/.htaccess file:

在DOCUMENT_ROOT / .htaccess文件中使用这些规则:

RewriteEngine On

RewriteRule ^male-items/?$                   /index.php?g=m&sort=popularity [L,QSA]
RewriteRule ^male-items/([^/]+)/?$           /index.php?g=m&sort=$2 [L,QSA]
RewriteRule ^male-items/([^/]+)/([^/]+)/?$   /index.php?g=m&sort=$2&l=$3 [L,QSA]

RewriteRule ^female-items/?$                 /index.php?g=f&sort=popularity [L,QSA]
RewriteRule ^female-items/([^/]+)/?$         /index.php?g=f&sort=$2 [L,QSA]
RewriteRule ^female-items/([^/]+)/([^/]+)/?$ /index.php?g=f&sort=$2&l=$3 [L,QSA]

#2


0  

These htaccess lines redirect all nonexisting files/folders to index.php:

这些htaccess行将所有不存在的文件/文件夹重定向到index.php:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

inside index.php you can use: $_SERVER['REQUEST_URI'] to parse your parameters.

在index.php里面你可以使用:$ _SERVER ['REQUEST_URI']来解析你的参数。

#1


1  

Use these rules in your DOCUMENT_ROOT/.htaccess file:

在DOCUMENT_ROOT / .htaccess文件中使用这些规则:

RewriteEngine On

RewriteRule ^male-items/?$                   /index.php?g=m&sort=popularity [L,QSA]
RewriteRule ^male-items/([^/]+)/?$           /index.php?g=m&sort=$2 [L,QSA]
RewriteRule ^male-items/([^/]+)/([^/]+)/?$   /index.php?g=m&sort=$2&l=$3 [L,QSA]

RewriteRule ^female-items/?$                 /index.php?g=f&sort=popularity [L,QSA]
RewriteRule ^female-items/([^/]+)/?$         /index.php?g=f&sort=$2 [L,QSA]
RewriteRule ^female-items/([^/]+)/([^/]+)/?$ /index.php?g=f&sort=$2&l=$3 [L,QSA]

#2


0  

These htaccess lines redirect all nonexisting files/folders to index.php:

这些htaccess行将所有不存在的文件/文件夹重定向到index.php:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

inside index.php you can use: $_SERVER['REQUEST_URI'] to parse your parameters.

在index.php里面你可以使用:$ _SERVER ['REQUEST_URI']来解析你的参数。