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
Related
I'm trying to redirect the user via the .htaccess file to create friendly URL example:
UI URL: https://example.com/courses/1
htaccess role
RewriteEngine On
RewriteRule ^courses/([0-9]{1})$ /courses.php?page=$1
Output URL: https://example.com/courses.php?page=1
And everything is working fine, Now I need to add other query params to the URL like this http://smart-courses.com/courses/1?p1=1&p2=2 so, I need htaccess to redirect me to https://example.com/courses.php?page=1&p1=1&p2=2
I tried to create a new role to check if p1 and p2 are exists and rewrite the URL
RewriteRule ^courses/([0-9]{1,5})?lid=([0-9]{1,})&did=([0-9]{1,})$ /courses.php?page=$1&p1=$2&p1=$3
Also, I tried to take any chars after ([0-9]{1,5}) (page number) and put it after ?page=1 but it did not worked
RewriteRule ^courses/([0-9]{1})\?(.*)$ /courses.php?page=$1&$2
The query string is not part of the path section of the URL that the rule pattern is matched against. If you'd have to capture something from the query string you need to use a RewritetCond, that is documented. In this case however you don't even need to capture anything:
RewriteEngine On
RewriteRule ^/?courses/(\d)$ /courses.php?page=$1 [QSA]
The QSA flag adds a potential query string to the (rewritten) target URL. The rewriting module takes care to use the correct concatenation here (the & character). Again, this behavior is documented: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Take a look yourself: htaccess.madewithlove.com
I am using Apache 2.4.7. I use mod_rewrite to alter some urls.
I want to rewrite http://example.com/servicename/oldpage?id=abcto http://example.com/servicename/newpage.
Other similar rewrites work so I belive the ? inside url is causing problems.
I have tried escaping it with \.
This works as there is no ? in url:
RewriteRule ^/servicename/old /servicename/new
But these don't work:
RewriteRule ^/servicename/oldpage?id=abc /servicename/newpage
RewriteRule ^/servicename/oldpage\?id=abc /servicename/newpage
I have also tried using RewriteCond from examples like this: .htaccess rewrite URL with a question mark "?" but I didn't manage to get them work.
How should rewrite url that contains question mark?
EDIT: I tried solutions given in Match Question Mark in mod_rewrite rule regex but was not able to make them work for me. That question is about preserving query string when rewrite while I want to remove it when rewriting.
RewriteRule pattern is matched against the part of the URL after the hostname and port, and before the query string.
When the requested URI contains a query string, and the target URI does not, the default behavior of RewriteRule is to copy that query string to the target URI. Using the [QSD] flag causes the query string to be discarded.
So, this should work:
RewriteRule ^/servicename/oldpage /servicename/newpage [QSD]
I'd like to redirect everything in /bar/ to a new subdomain but still pass in any query parametes from the original URL. E.g
Original URL examples:
http://www.foo.com/bar/
http://www.foo.com/bar/index.php
http://www.foo.com/bar/index.html
http://www.foo.com/bar/test.php?id=123&utm_source=google
Should redirect to:
http://bar.foo.com/
http://bar.foo.com/
http://bar.foo.com/
http://bar.foo.com/?id=123&utm_source=google
This is what I currently have:
RewriteEngine On
RewriteRule ^bar/?(.*)$ https://bar.foo.com/? [R=301,L]
However, it doesn't pass in the query parameters I need in the 4th example above. I know the ? prevents query parameters being passed in so this will most likely need to be removed I assume but not sure what to replace it with?
Cheers
Add a new rule like this:
RewriteEngine On
RewriteRule ^bar(/|$) https://bar.foo.com/ [NC,R=301,L]
QUERY_STRING will automatically be carried over to target URI.
Make sure there is no trailing ? in the target URI which strips existing query string.
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 working on some rewrite rules, and for some reason a regexp I'm not expecting to pass (and does pass not on any of my regexp testers) is passing in mod_rewrite.
The URL in question is:
http://url.com/api/projects.json?division=aa
And the rewrite rule is:
RewriteEngine On
RewriteBase /
RewriteRule ^api\/([^.?#/%\s]+)\.([^#?\s]+)$ api.php?type=$1&format=$2 [NC,L]
Because the second capture is immediately followed by $ I'd expect that URL to fail because of the query string, but it seems to accept just fine and pass the two parameters to GET.
Any thoughts?
Note: Query String
The Pattern will not be matched
against the query string. Instead, you
must use a RewriteCond with the
%{QUERY_STRING} variable.
Snip from the bottom of the docs