开发者

mod_rewrite / redirection with ? in a query string

开发者 https://www.devze.com 2023-04-04 21:18 出处:网络
I\'m diving into the uses of htaccess/mod rewrite for the first time and I\'m having a lit开发者_如何学Pythontle bit of trouble with a redirect/mask.

I'm diving into the uses of htaccess/mod rewrite for the first time and I'm having a lit开发者_如何学Pythontle bit of trouble with a redirect/mask.

I have a test directory in my root folder called modrw, in that folder is a index.php file with a nice and simple:

<?php echo $_GET['name']; ?>

In the browser if I type www.domain.com/modrw/{word}/ then the word is echoed on the page, which is what I want.

If I type www.domain.com/modrw/name={word} then I am redirected to www.domain.com/modrw/{word}/ and the word is also echoed as I intended.

However, if I direct the browser to the URL www.domain.com/modrw/?name={word}/ the word is echoed but I am not redirected to www.domain.com/modrw/{word}/ like I hoped.

How is the ? causing troubles? In the RewriteRule code below the ? is included, I've tried it a couple different ways but can't get it working.

Here is my htaccess file:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

RewriteRule ^modrw/?name=([^/\.]+)/?$ http://www.domain.com/modrw/$1 [R]
RewriteRule ^modrw/([^/\.]+)/?$ modrw/?name=$1

What is causing the problem, is there a specific way to include the ?, does it not pick this up at all? Am I taking completely the wrong approach?

I've also tried using Options +FollowSymlinks but I'm not entirely sure what this does nor if it's needed at all...


Remember that RewriteRuleonly matches REQUEST_URI. To match query string you must use RewriteCond with variable %{QUERY_STRING}. For ex. in your case:

RewriteCond %{QUERY_STRING} ^name=(.*)(&|$) [NC]
RewriteRule ^modrw /modrw/%1? [L,R,NC]
0

精彩评论

暂无评论...
验证码 换一张
取 消