简体   繁体   English

用斜杠.htaccess替换GET变量

[英]Replace GET variables with slashes .htaccess

I am trying to use .htaccess to make my url's "prettier". 我正在尝试使用.htaccess使我的网址“更漂亮”。

Changing domain.com/page/val1 更改domain.com/page/val1

To domain.com/page.php?var1=val1 domain.com/page.php?var1=val1

If possible, I would like it to work with 2 variables as well: 如果可能的话,我希望它也可以使用2个变量:

Changing domain.com/page/val1/val2 更改domain.com/page/val1/val2

To domain.com/page.php?var1=val1&var2=val2 domain.com/page.php?var1=val1&var2=val2

This is my current .htaccess file that works to remove the .php extension: 这是我当前的.htaccess文件,用于删除.php扩展名:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

I've found other topics with solutions similar to adding something like this: 我发现其他主题的解决方案类似于添加这样的东西:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1/$2?var1=$3&var2=$4 [L,QSA,NC]

I've tried many ways to modify it and it never seems to work. 我已经尝试了很多方法来修改它,它似乎永远不会起作用。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

Try this code: 试试这段代码:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([^/]+)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?var1=$2 [L,QSA]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?var1=$2&var2=$3 [L,QSA]

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

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