简体   繁体   中英

.htaccess mod_rewrite 301 redirection problem

I have an web application 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!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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