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

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.

Related

Simplifying redirect in htaccess

I have redirects:
RewriteRule ^(.*)/thema(.*)$ https://www.newurl.com [R=301,L]
RewriteRule ^(.*)/stichpunkt(.*)$ https://newurl.com [R=301,L]
RewriteRule ^(.*)/author(.*)$ https://www.newurl.com [R=301,L]
RewriteRule ^(.*)/2023(.*)$ https://www.newurl.com [R=301,L]
is there a way to simplify these into one line?
I need to disable category, tag, author and date archives in Wordpress
This should really be done in WordPress itself. Otherwise WP is still going to generate and publish these URLs (eg. Sitemap, RSS feed, etc.).
Otherwise, if .htaccess is your only option then you should serve a 404, rather than redirect to the homepage. Whilst a redirect to the homepage is likely to be treated as a soft-404 by Google (and possibly other search engines) it runs the risk of being indexed under these "archive" URLs (and accessible with a site: search).
For example, at the top of the root .htaccess file (before any existing WP directives):
# Whatever your custom 404 page is (could be WordPress)
ErrorDocument 404 /404.php
# Force a 404 for "category, tag, author and date archives
RewriteRule (^|/)(thema|author|stichpunkt|2\d{3})(/|$) - [R=404]
2\d{4} matches any 4 digit year (in the 2000's).
The regex matches any of those "words" only when they occur as a whole path segement (not partial matches).
R=404 - This is not a "redirect" (despite the use of the R flag). The 404 error document is served via an internal subrequest and a 404 HTTP response code is set on the initial response. If these URLs have previously been indexed then consider changing this to a "410 Gone" instead, ie. R=410 or simply G (shorthand flag).
You can use a simple alternation with |:
RewriteRule ^.*/(thema|author|stichpunkt|2023) https://www.newurl.com [R=301,L]
You don't need to capture parts that you don't need to refer back to, so I removed the () around the .*. Around the alternation they are still needed so even if you are not interested in capturing that part, otherwise it would not be clear where the first value starts and the last one ends.
And you don't need to match .*$ either, you can just leave of the $ that anchors this pattern at the end.

Redirect Query String via .htaccess

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

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

Using htaccess to redirect anything after a parameter - regex

Id like to have the following URL(s) redirect to the same URL just without the ?
For example:
https://www.example.com/this-is-static?numbersletterssymbols
goes to
https://www.example.com/this-is-static
"numbersletterssymbols" can be anything
Id like this to be a 301 , using htaccess ( apache )
I came across the following, however, the variable seems to be in parentheses
RewriteCond %{QUERY_STRING} ^product=(.*)$
RewriteRule ^test.php$ %1/? [R=301,L]
Any insight is appreciated
To remove the query string (any query string) from any URL you could do the following using mod_rewrite, near the top of your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI} [QSD,R=301,L]
The condition (RewriteCond directive) simply asserts that there is a query string consisting of at least 1 character (determined by the regex . - a single dot).
The QSD (Query String Discard) flag removes the original query string from the redirected response. The QSD flag requires Apache 2.4 (which you are most probably using). The method used on earlier versions of Apache, as in your example, is to append a ? to the susbstitution string (essentially an empty query string).
Note that you should test first with a 302 (temporary) redirect to avoid potential caching issues.
however, the variable seems to be in parentheses
The parentheses in the regex simply creates a "capturing group" which can be referenced later with a backreference. eg. In your example, the value of the product URL parameter is referenced in the RewriteRule substitution string using the %1 backreference in order to redirect to the value of the URL parameter. This is very different to what you are trying to do and is arguably a security issue. eg. It would redirect a request for /test.php?product=https://malicious.com to https://malicious.com/, allowing a potential hacker to relay traffic via your site.
UPDATE: is it possible to make this work only for when the URL begins with "this-is-static" (for example)
Yes, the RewriteRule pattern (1st argument) matches the URL-path, less the slash prefix. For example:
RewriteCond %{QUERY_STRING} .
RewriteRule ^this-is-static %{REQUEST_URI} [QSD,R=301,L]
Matches all URLs that start with /this-is-static.

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.