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.
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 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 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've been trying to write a rewrite rule for apache to switch my gallery2 URLs to the gallery3 URL format:
Old url example linked: http://domain.com/gallery/photoalbumxyz/photo.jpg.html
New url example needed: http://domain.com/photos/photoalbumxyz/photo
Note that in the URL example above, "/photoalbumxyz/photo.jpg.html" is not an actual physical directory, it is just the way gallery2 rewrote "friendly" URLs. I can rewrite the /gallery/ to /photos/ by using a rule like the following:
RewriteRule ^(.*)$ /photos/$1 [QSA,L,R=301]
However I'm having trouble figuring out the matching and removal of the ".jpg.html" extension if it exists in combination with the /gallery/ -> /photos/ rewrite. The regex matching I believe is going to be .jpg.html to escape the periods, but how do I write rules to remove the ".jpg.html" extension and rewrite the directory?
RewriteRule ^\.jpg\.html$ $1 [NC]
RewriteRule ^(.*)$ /photos/$1 [QSA,L,R=301]
Edit:
Sorry! I neglected earlier to mention the album URL formats can change (doesn't have to specify a photo, and can include sub albums), I've added some specific examples:
The url rewrite rule also needs to follow:
old: http://example.com/gallery
new: http://example.com/photos
old: http://example.com/gallery/album
new: http://example.com/photos/album
old: http://example.com/gallery/album/subalbum/
new: http://example.com/photos/album/subalbum/
old: http://example.com/gallery/album/subalbum/photo.jpg.html
new: http://example.com/photos/album/subalbum/photo
Try:
RedirectMatch 301 ^/gallery(.*?)(\.(jpe?g|gif|png)\.html)?$ /photos$1
Or alternatively using mod_rewrite:
RewriteRule ^/?gallery/([^/]+)/([^.]+)\.(jpe?g|gif|png)\.html$ /photos/$1/$2 [L,R=301]
You don't need the QSA flag as query strings will automatically get appended if you don't have a ? in your rule's target.
I'm working on a webpage that takes a URL as a parameter, and would like it to be easily indexed by search engines. One requirement is that each URL appears as a directory.
My script is in the format:
myscript?url=<a url>&page=1
I'd like redirects to look something like:
lookup/<a url>/page:1/
The URL is predictably giving me trouble... I just want to tell mod_rewrite to select anything after "lookup/" and before "/page:". Of course, nothing is ever as simple as it could be.
Here's the rewrite as it is now:
RewriteEngine on
RewriteRule ^/lookup/(.+)/page:([0-9]+)(/?)$ /myscript?url=$1&page=$2 [L]
This works great, except it fails when URLs are properly encoded. Take the example of "www.google.com/finance". Here's what happens when I enter these URLs into my browser's address bar:
#this works
lookup/www.google.com/finance/page:1/
#this doesn't work. url is cut off before the ?
lookup/www.google.com/finance?foo=bar/page:1/
#doesn't match rewrite at all!
lookup/www.google.com%2Ffinance/page:1/
I'm at a loss as to how to do this... Shouldn't (.+) select anything? Do I need to tell mod_rewrite to ignore query parameters somehow?
Try this:
RewriteCond %{THE_REQUEST} ^GET\ /lookup/([^\s]+)/page:([0-9]+)/[?\s]
RewriteRule ^/lookup/ /myscript?url=%1&page=%2 [L]
But you should really consider encoding that embedded URL properly instead of just guessing where it might end. So /lookup/www.google.com/finance?foo=bar/page:1/ should be at least /lookup/www.google.com/finance%3Ffoo=bar/page:1/ so the ? is part of the URI path and not the indicator for the query.