开发者

.htaccess mod_rewrite 301 redirection problem

开发者 https://www.devze.com 2023-04-03 05:26 出处:网络
I have an web application开发者_StackOverflow社区 in PHP with urls like this: https://www.domain.com/?mod=test&act=view

I have an web application开发者_StackOverflow社区 in PHP with urls like this: https://www.domain.com/?mod=test&act=view

My problem is that when I try to use mod_rewrite to redirect users to new link: https://www.domain.com/view-test

I have the following rule under my .htaccess:

RewriteRule ^\?mod=test&act=view$ /view-test [L,R=301] 

If I remove the leading "?" from the above rule the redirection works but obsiouly I get a 404 error. But if I keep it nothing happens

Can anyone help?


You can create a new rule for view-test:

RewriteEngine On

RewriteRule ^mod=test&act=view$ /view-test [L,R=301] 

# new rule for view-test
RewriteRule ^view-test$ /index.php [L]

Update:

This works perfectly for me:

Options +FollowSymlinks

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI}  ^/index\.php$
RewriteCond %{QUERY_STRING} ^mod=test&act=view$
RewriteRule ^(.*)$ /test-view? [L,R=301]

RewriteRule ^test-view/?$ /index.php [L]

Get the values:

<?php

$request = str_replace("/", "", $_SERVER['REQUEST_URI']);
$request = explode("-", $request);

print_r($request);

Output:

Array
(
    [0] => test
    [1] => view
)

Hope this helps!!

0

精彩评论

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