RewriteRule that preserves GET parameters
The regex on the RewriteRule is only run against the path part of the URL, not the query parameters. Fortunately there is the [QSA]
flag to preserve existing query parameters.
.htaccess RewriteRule to preserve GET URL parameters
You need to append with the [QSA] (query string append) tag. Try
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?lang=$1&page=$2 [QSA]
See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
.htaccess rewriterule keep parameters
You need to add a QSA flag to your rewrite rule, so that the brackets look like this:
[L,QSA]
This tells apache to append any existing query string to the new query string in the target (mode=allBrands).
more Info there: https://cwiki.apache.org/confluence/display/HTTPD/RewriteFlags+QSA
Redirect and keep the parameter in the url on .htaccess
You may use this redirect rule at the top of .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)foo= [NC]
RewriteRule ^/?$ /new-nice-page [L,R=301]
Note that query string is automatically carried forward to target URL.
.htaccess rewrite rule for urls with parameters
Ordering of rules is probably causing this problem. Try these rules in proper order:
RewriteEngine On
RewriteBase /app/
RewriteRule ^user/([0-9]+)/?$ user.php?id=$1 [QSA,L,NC]
# hide .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/app/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
How can I mod_rewrite and keep query strings?
You need to add the [QSA]
flag ("query string append")
RewriteRule ^apps/([A-Za-z0-9-_]+)/?$ index.php&app=$1 [L,QSA]
For page 301 redirects with the [R]
flag as opposed to internal rewrites like this one, the query string is automatically appended. However, you must force it with [QSA]
for the internal rewrite.
REWRITE URL and keep QUERY STRING in .htaccess
If you want clean URLs then you can simply use:
RewriteRule ^([^/]*)$ /buy?r=$1 [L]
to get example.com/abc123
Or you can use:
RewriteRule ^release/([^/]*)$ /buy?r=$1 [L]
to get example.com/release/abc123
.
Just make sure you clear your cache before testing this.
htaccess Redirect URL with GET Parameters
RewriteCond %{QUERY_STRING} ^s=(.*)$ [NC]
RewriteRule ^$ /search/%1? [NC,L,R]
You will likely need the NE
(noescape
) flag on the RewriteRule
directive if you are receiving a %-encoded URL parameter value, otherwise the target URL will be doubly-encoded. The QUERY_STRING
server variable is not decoded by Apache.
It also depends on how you are rewriting /search/query
back to /?s=query
(or presumably more like /index.php?s=query
?) - presumably you are already doing this later in the config? You only want this redirect to apply to direct requests and not rewritten requests (otherwise you'll get a redirect loop). An easy way to ensure this is to check that the REDIRECT_STATUS
env var is empty.
For example:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^s=(.*) [NC]
RewriteRule ^$ /search/%1 [NE,QSD,R,L]
Other points:
- The
QSD
flag would be preferable (on Apache 2.4) to appending?
to the end of the susbtitution string in order to remove the query string. - The regex
^s=(.*)
(the trailing$
was superfluous) does assume thats
is the only URL parameter at the start of the query string. As it stands, everything is assumed to be part of this value. eg.s=foo&bar=1
will result in/search/foo&bar=1
. - The
NC
flag on theRewriteRule
directive is superfluous. - Should you also be checking for
/index.php?s=<query>
? (Or whatever file/DirectoryIndex is handling the request.)
Related Topics
Pkill -F Doesn't Work for Process Killing
How to Get the Character from Unicode Code Point in PHP
How to Find Unused Functions in a PHP Project
Reconstruct/Get Source Code of a PHP Function
How to Embed Images in HTML Email
Mysql_Real_Escape_String VS Addslashes
How to Convert JSON to Xml in PHP
Caching Http Responses When They Are Dynamically Created by PHP
Is It Okay to Use Array[Key] in PHP
Prevent Innodb Auto Increment on Duplicate Key
MVC (Laravel) Where to Add Logic
PHP Artisan Make:Auth Command Is Not Defined
Can't Install Laravel Installer via Composer
Ajax File Download Using Jquery, PHP
Csrf (Cross-Site Request Forgery) Attack Example and Prevention in PHP