简体   繁体   中英

How to make this rewrite rule work?

What I'm trying to do is a pretty simple idea but somehow it doesn't work. I need this rewrite rule to work:

RewriteRule ^([a-zA-Z0-9-]+)/?([a-zA-Z0-9-=]*)$ index.php?op=$1&$2 [L,NC]

I think it's pretty much self explanatory but I want the rule to pass the first regex group as the op parameter and the the second regex group is passed untouched to the script. Like this:

http://example.org/view/?post_slug=awesome-apache-mods --> http://example.org/index.php?op=view&post_slug=awesome-apache-mods

The problem here is that the first part works just fine, meaning the op parameter is set correctly but the second part gets lost. I mean literaly lost. When I issue print_r($_GET) there is just the op parameter.

Array
(
    [op] => view
)

I wonder what's wrong here?

I tested your pattern using preg_match_all its not matching the URL because _ is not defined in the pattern, but when I added _ it matched the URL:

$text = 'view/?post_slug=awesome-apache-mods';
$pattern = '/^([a-zA-Z0-9-]+)\/\?([a-zA-Z0-9_=-]*)$/';
preg_match_all($pattern, $text, $out);
print_r($out);

Then your .htaccess pattern should be like this:

^([a-zA-Z0-9-]+)/\?([a-zA-Z0-9_=-]*)$ [L,NC, QSA]

Its better to use this if /view is constant:

RewriteRule ^([a-zA-Z0-9_-=]*)$ index.php?op=view&$2 [L,NC, QSA]

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