开发者

Htaccess rewrite rules?

开发者 https://www.devze.com 2023-03-30 19:48 出处:网络
so my url is currently: http://www.mywebsite.com/search.php?keyword=stack+overflow I\'m wanting to have it accessible as:

so my url is currently:

http://www.mywebsite.com/search.php?keyword=stack+overflow

I'm wanting to have it accessible as:

http://www.mywebsite.com/?s=stack+overflow

Now I've also implemented rewrites for .php files as below

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FI开发者_如何学编程LENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

I'm a little confused now because the search.php is being rewritten to just search but is still accessable with search.php, so when I create my rewrite rule do I rewrite search.php or just search?.. either way I've tried and failed to accomplish it lol.

What should I add to have my desired url? Help is much appreciated :)


It's impossible to change parameter name with mod_rewrite. The easiest way to achieve what you want is to add these lines to .htaccess:

RewriteCond %{QUERY_STRING} ^s=
RewriteRule .? search.php [L]

and modify search.php to react to s get parameter the same as it reacts to keyword.

An alternative is to keep .htaccess intact and add these at the top of index.php:

if (!empty($_GET['s'])) {
    $_GET['keyword'] = $_GET['s'];
    require __DIR__ . '/search.php';
    exit;
}
0

精彩评论

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

关注公众号