我正在尝试重定向http://test.pearlsquirrel.com/explore_all?u=hiphop至 http://test.pearlsquirrel.com/explore_all/hiphop使用 htaccess。然而,它一直重定向到这里:http://test.pearlsquirrel.com/explore_all/?u=site_error.php 。我在 Stackoverflow 上到处寻找答案,但似乎找不到。我非常感谢您提供解决此问题的帮助,谢谢!
这是我的 .htaccess 文件
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) explore_all?u=$1 [L]
最佳答案
你可以试试这个:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} [^=]+=([^=]+)
RewriteRule .* /%1? [L]
将映射此:
http://test.pearlsquirrel.com/explore_all?u=hiphop
对此:
http://test.pearlsquirrel.com/explore_all/hiphop
将 .htaccess 文件中的相应代码替换为此代码。
我还没有检查您的 .htaccess 文件中的规则。
选项
现在,如果情况相反,我认为是这样,并且想法是映射此传入 URL:
http://test.pearlsquirrel.com/explore_all/hiphop
到此资源:
http://test.pearlsquirrel.com/explore_all?u=hiphop
将上面的规则集替换为以下规则集:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([^/]+)/?
RewriteRule .* http://test.pearlsquirrel.com/explore_all?u=%1 [L]
关于.htaccess 单变量 url 重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14147015/