Home > Software design >  add " ? " in url via htaccess RewriteRule
add " ? " in url via htaccess RewriteRule

Time:05-16

i try use RewriteRule in htaccess

i want my url

site.com/f_search.php?langs=en&langs=en

works like

site.com/en/new.php?/en/f_search

i used this code in htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?/new.php ? /([^/]*)/?/f_search/?$ f_search.php?langs=$1&langs=$2

but its not work with " ? "

if i change new.php ? to new.php @

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?/new.php @ /([^/]*)/?/f_search/?$ f_search.php?langs=$1&langs=$2

it works good

site.com/en/new.php@/en/f_search

any idea to change @ to ? in my url ???

thanks

CodePudding user response:

The question mark and what follows is not part of the subject the pattern of a RewriteRule is matched against. That is clearly documented. Instead you need to use a RewriteCond to access the content of the query string.

I assume this is roughly what you are looking for, though you might have to tweak it to match your actual situation:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^/([a-z] )/f_search$
RewriteRule ^/?(?:([a-z] )/)?new\.php$ /f_search.php?langs=$1&langs=%1

From your question it is unclear where the two values for the argument "langs" in the rewrite target should get taken. So you may have to adjust that to your needs in the rule.

Also it is puzzling that you are trying to set that argument "langs" twice ... That makes little sense, the second attempt will simply overwrite the value of the first attempt when those values are handed over to your php based logic.

The documentation of the rewriting module for the apache http server offer a lot of precise help and good examples: https://httpd.apache.org/docs/current/mod/mod_rewrite.html

CodePudding user response:

the final code

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-z] )/new.php$ f_search.php?langs=$1 [L,NC]
  • Related