Redirect Query String via .htaccess - regex

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

Related

Having trouble with some redirect/rewrite codes .. 301 and 410s

Brand new here and totally NOT a coder, so be gentle. The level of understanding I have here is about that of a toddler, so pretend you're talking to a 5 year old and I should be able to keep up.
I'm switching to a new server, and no longer using coppermine gallery. I can't get the redirects from the old cpg galleries and images to work.
For albums and categories that I will not redirect
(The url I want to redirect here would be)
http://www.example.com/stock/thumbnails\.php\?album=62
They're gone and no longer exist, I wrote the 410 rule as
RewriteRule ^stock/thumbnails\.php\?album=62$ - [R=410, L]
That breaks the new site and creates a 500 error
For old albums that I want to redirect to a new url, such as this url
http://www.example.com/stock/thumbnails\.php\?album=3
I wrote
RewriteRule ^stock/thumbnails\.php\?album=36$ https://www.example.com/gallery/appalachian-trail-photos/ [R=301,L]
But it does nothing. The urls show as 404 pages. I also tried it as
Redirect 301 /stock/index.php?cat=2 https://www.example.com/gallery/outdoor-recreation-photos/
which also does nothing.
I also want to redirect any image display pages to the root gallery of their relative album. So an image from a particular cpg album would go to the gallery page for that on the new site.
A url like this
http://www.example.com/stock/displayimage.php?album=1&pid=4563#top_display_media
I wrote the redirect as
RewriteRule ^stock/displayimage\.php\?album=1&.*$ https://www.example.com/gallery/canadian-wildlife-photos/ [R=301,L]
RewriteRule ^stock/displayimage\.php\?album=32&.*$ - [R=410,L]
to send any image from album 1 to the CA wildlife photos album and any image from album 32 is gone.
None of these last work but I can't see what's wrong with them?
Any help would be superduper appreciated, thanks. Apologies in advance for my ignorance.
RewriteRule ^stock/thumbnails\.php\?album=62$ - [R=410, L]
^--ERROR HERE
The erroneous space in the flags argument is likely the cause of the 500 error. This is a syntax error, so just "breaks". It should be [R=410,L] (no space). The L flag is actually redundant here when you specify a non-3xx status code, so this is the same as simply [R=410], which can be further simplified to just [G]. The G flag is shorthand for R=410 (ie. "410 Gone").
However, the RewriteRule pattern (first argument) matches against the URL-path only. This does not match against the query string (the part of the URL after the first ?). To match the query string you need a separate condition (RewriteCond directive) and match against the QUERY_STRING server variable (which does not include the ? prefix).
For example, the above should be written something like:
RewriteCond %{QUERY_STRING} ^album=62$
RewriteRule ^stock/thumbnails\.php$ - [G]
This matches the URL /stock/thumbnails.php?album=62 exactly. And serves a "410 Gone". If there are any other URL parameters (in the query string) then the match will fail. eg. /stock/thumbnails.php?foo=1&album=62 will not match.
To match the URL parameter album=62 anywhere in the query string (if there are other URL params) then tweak the CondPattern (2nd argument to the RewriteCond directive) like so:
RewriteCond %{QUERY_STRING} (^|&)album=62(&|$)
:
The additional alternation subgroups (^|&) and (&|$) before and after the URL parameter, ensure we only match that exact URL parameter and not fooalbum=62 or album=623, etc.
RewriteRule ^stock/displayimage.php?album=1&.*$ https://www.example.com/gallery/canadian-wildlife-photos/ [R=301,L]
The above points should resolve the main issue with this rule (matching the query string). However, you also need to add the QSD flag in order to remove the original query string from the redirect response. Otherwise, the original query string (ie. album=1&....) will be appended to the end of target URL (eg. /gallery/canadian-wildlife-photos/?album=1&...).
For example:
RewriteCond %{QUERY_STRING} (^|&)album=1(&|$)
RewriteRule ^stock/displayimage\.php$ https://www.example.com/gallery/canadian-wildlife-photos/ [QSD,R=301,L]
Redirect 301 /stock/index.php?cat=2 https://www.example.com/gallery/outdoor-recreation-photos/
Note that the (mod_alias) Redirect directive does not match the query string either (only the URL-path). To match the query string you need to use mod_rewrite (RewriteRule and RewriteCond) as mentioned above.

Redirect the URL from one query string to another

I have spent a great many hours trying to find a solution to this and tried many different approaches but nothing I have tried has worked so far.
I would like to redirect a URL with a query string to another URL that contains the value of that query string.
I want to redirect:
https://example.com/component/search/?searchword=XXXXXXXXX&searchwordsugg=&option=com_search
to
https://example.com/advanced-search?search=XXXXXXXXX
You can do something like the following using mod_rewrite at the top of your root .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)searchword=([^&]*)
RewriteRule ^component/search/?$ /advanced-search?search=%1 [NE,R=302,L]
The RewriteRule pattern matches against the URL-path only, which notably excludes the query string. To match against the query string we need a separate condition that checks against the QUERY_STRING server variable.
%1 is a backreference to the first capturing group in the preceding CondPattern, ie. the value of the searchworld URL parameter.
The regex (?:^|&)searchword=([^&]*) matches the searchworld URL parameter anywhere in the query string, not just at the start (as in your example). This also permits an empty value for the URL parameter.
The NE flag is required to prevent the captured URL parameter value being doubly encoded in the response. (Since the QUERY_STRING server variable is not %-decoded.)
The L flag prevents further processing during this pass of the rewrite engine.
Reference:
Apache docs: RewriteRule Directive
Apache docs: RewriteCond Directive

Apache2: Rewrite url containing question mark

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]

301 redirect directory with query parameters

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.

How to rewrite this URL to a redirect page?

I am using Microsoft-IIS/7.5 on a hosted server (Hostek.com)
I have an existing site with 2,820 indexed links in Google. You can see the results by searching Google with this: site:flyingpiston.com Most of the pages use a section, makerid, or bikeid to get the right information. Most of the links look like this:
flyingpiston.com/?BikeID=1068
flyingpiston.com/?MakerID=1441
flyingpiston.com/?Section=Maker&MakerID=1441
flyingpiston.com/?Section=Bike&BikeID=1234
On the new site, I am doing URL rewriting using .htaccess. The new URLs will look like this:
flyingpiston.com/bike/1068/
flyingpiston.com/maker/1123/
Basically, I just want to use my htaccess file to direct any request with a "?" question mark in it directly a coldfusion page called redirect.cfm. On this page, I will use ColdFusion to write a custom 301 redirect. Here's what ColdFusion's redirect looks like:
<cfheader statuscode="301" statustext="Moved Permanently">
<cfheader name="Location" value="http://www.newurl/bike/1233/">
<cfabort>
So, what does my htaccess file need to look like if I want to push everything with a question mark to a particular page? Here's what I have tried, but it's not working.
RewriteEngine on
RewriteRule ^? /redirect.cfm [NS,L]
Update. Using the advice from below, I am using this rule:
RewriteRule \? /redirect/redirect.cfm [NS,L]
To try to push this request
http://flyingpiston2012-com.securec37.ezhostingserver.com/?bikeid=1235
To this page:
http://flyingpiston2012-com.securec37.ezhostingserver.com/redirect/redirect.cfm
There's a couple of reasons what you're trying isn't working.
The first one is that RewriteRule uses a regex, and ? is a regex metacharacter, which therefore needs be escaped with a backslash (\?) to tell it to match the literal question mark character.
However, the second part of the problem is that the regex for RewriteRule is only tested against the filename part of the URL - it specifically excludes the query string.
In order to match against the query string you need to use the RewriteCond directive, placed on the line before the rule (but applied in between the RewriteRule matching and replacing), acting as an additional filter. The useful bit is that you can specify which part of the URL to match against (as well as having the option for using non-regex tests).
Bearing all this in mind, the simplest way to match/rewrite a request with a query string is:
RewriteCond %{QUERY_STRING} .
RewriteRule .* /redirect/redirect.cfm
The %{QUERY_STRING} is what the regex is tested against (everything in CF's CGI scope can be used here, and some other stuff too - see the Server Variables box in the docs).
The single . just says "make sure the matched item has any single character"
At the moment, this rule will preserve the existing query string - if you want to discard it, you can place a ? onto the end of the replacement URL. (If you need to use a query string on the URL and not discard the old version, use the [QSA] flag.)
In the opposite direction, you're losing the filename part of the URL - to preserve this, you probably want to append it onto the replacement as PATH_INFO, using the automatic whole-match capture $0.
These two things together provides:
RewriteCond %{QUERY_STRING} .
RewriteRule .* /redirect/redirect.cfm/$0?
One final thing is that you'll want to guard against infinite loops - the above rule strips the query string so it will always fail the RewriteCond, but better to be safe (especially if you might need to add a query string), which you can do with an extra RewriteCond:
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} !/redirect/redirect\.cfm
RewriteRule .* /redirect/redirect.cfm/$0?
Multiple RewriteCond are combined as ANDs, and the ! negates the match.
You can of course add whatever flags are required to the RewriteRule to have it behave as desired.