我的.htaccess
文件:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9-]+)$ index.php?page=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?page=$1 [QSA,L]
我在URL中定义了一个键,即“页面”,这是正常的。现在我需要包含第二个键,但每个页面的“键”都不同。
例如:
example.com/index.php?page=user?id=john
example.com/index.php?page=product?url=product-url
需要将它们转换为:
example.com/user/john
example.com/product/product-url
如何根据页面定义单个第二个键?
您可以使用:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^user/([^/]+)/?$ index.php?page=user&id=$1 [NC,QSA,L]
RewriteRule ^product/([^/]+)/?$ index.php?page=product&url=$1 [NC,QSA,L]
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?page=$1 [QSA,L]