I have a rule in .htaccess that generates a query string according to the url syntax:
RewriteEngine on
RewriteRule a_([0-9]+)_(.*).html$ article.php?id=$1 [L]
So a url like this:
a_52_how-to-use-htaccess.html generates: article.php?id=52
My question is how to allow htaccess to add to the query string other get variables sent on the url:
a_52_how-to-use-htaccess.html?debug=true
in order to have:
article.php?id=52&debug=true
Any ideas? Thanks.
Use QSA flag:
RewriteEngine on
RewriteRule a_([0-9]+)_(.*)\.html$ article.php?id=$1 [L,QSA]
QSA (Query String Append) flag preserves existing query parameters while adding a new one.
Related
I am trying to redirect the following URL:
url/efx.aspx?xxxxxxx
to url/car-audio/efx-hardware/amp-install-kits
However it is redirecting whatever contains efx.aspx with the letters without the ? sign. I was wondering how I can fix this?
for example it is redirecting the following:
domain.com/efx.aspxlsdkjfhlasdf
but it is not redirecting
domain.com/efx.aspx?lsdkjfhlasdf
here is the .htaccess rule I wrote. how can I correct it?
RewriteCond %{REQUEST_URI} /efx.aspx[^/]+$
RewriteRule (.*) /car-audio/efx-hardware/amp-install-kits [R,L]
You can use this rule:
RewriteRule ^efx\.aspx$ /car-audio/efx-hardware/amp-install-kits? [R=301,NC,L]
Query string is not part of REQUEST_URI hence [^/]+ after efx.aspx fails your rule.
Also ? at the end of target URI removes any existing query string.
I have a .htaccess rule that goes like this
RewriteRule ^account/(.*) myaccount.php?p=$1 [NC,L]
This works fine for URLs like https://www.example.com/account/abcd.
However, now I want to pass query parameters to this URL, something like https://www.example.com/account/abcd?ab=1&cd=2.
Can't figure out the exact .htaccess rule that can accommodate this.
Any pointers?
Use:
RewriteRule ^account/(.*) myaccount.php?p=$1 [QSA,NC,L]
With QSA:
QSA|qsappend
When the replacement URI contains a query string, the
default behavior of RewriteRule is to discard the existing query
string, and replace it with the newly generated one. Using the [QSA]
flag causes the query strings to be combined.
http://httpd.apache.org/docs/current/rewrite/flags.html
I have the following line in my htaccess
RewriteRule ^([^/.]+)/([^/.]+)/r/([^/.]+)/?$ /index.php?slug_1=$1&slug_2=$2&ref=$3 [L]
So, blah.com/a/b/r/c/ goes to blah.com?slug_1=a&slug_2=b&ref=c
I would also like to catch and append all extra query params
So if I go to blah.com/a/b/r/c?d=e&f=g then it redirects to blah.com?slug_1=a&slug_2=b&ref=c&d=e&f=g
Your rule is correct but you need QSA flag:
RewriteRule ^([^/.]+)/([^/.]+)/r/([^/.]+)/?$ /index.php?slug_1=$1&slug_2=$2&ref=$3 [L,QSA]
QSA is called Query String Append that preserves original query string while adding new one.
I'm using index.php as my entry script. I want to apply specific Rewrite rules to my htaccess to see my URLs in better format
For now I have succeeded to match my controller & action only by using
RewriteRule ^(.*)$ /mysite/index.php?r=$1 [L]
So I can use any of these URLs
domain.com/users/user => /mysite/index.php?r=users/user
domain.com/contacts/contact = /mysite/index.php?r=contacts/contact
I want to add some additonal get variables to my URLs, to get specific records like
domain.com/users/user/id/10
or
domain.com/contacts/contact/id/2/name/5
Now I can do this only this way:
domain.com/users/user&id=10 = > /mysite/index.php?r=users/user&id=10
domain.com/contacts/contact&id=2&name=5 => /mysite/index.php?r=contacts/contact&id=2&name=5
How can I change my Rewrite rule to support GET variables as well?
Add QSA flag:
RewriteRule ^(.*)$ /mysite/index.php?r=$1 [L,QSA]
QSA stands for Query String Append which make sure to preserve existing query string while adding new query parameters.
Reference: Apache mod_rewrite Introduction
I'm looking for the regex url rewriting entries for .htaccess - specifically, I'd like a way to re-write:
/site/this-is-my-slug/
to
/site/?slug=this-is-my-slug
Is this possible?
Try this:
RewriteRule ^site/(.*)$ site/?slug=$1 [L,QSA]
The QSA flag will copy over any query string from your original URL request so remove it if you don't want this functionality.