.htaccess 单变量 url 重写

标签 .htaccess mod-rewrite

我正在尝试重定向http://test.pearlsquirrel.com/explore_all?u=hiphophttp://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/

相关文章:

apache - htaccess - 删除 .php 扩展名并保留变量

.htaccess - Htaccess 从 example.com/en/app 重定向到 example.com/app?lang=en

.htaccess - 为 Apache httpd 中的每个 Set-Cookie 响应添加 Secure 和 httpOnly 标志

.htaccess - 在 fastcgi 的 codeigniter 上强制使用 https

php - .htaccess RewriteRule 保留 GET URL 参数

validation - 如何处理未在 HTML5 中验证的元元素?

php - 强制浏览器下载

php - .htaccess 重定向到任何目录中的index.php

javascript - 将特定虚拟网址重定向到index.html

.htaccess - 在 .htaccess 重定向中保留 HTTP/HTTPS 协议(protocol)