using .htaccess to 410 any wildcard url that contains a question mark - .htaccess

I have looked and searched and have not quite found the answer to this question - I think it should be pretty simple but I am not sure.
Basically, I would love to learn how to use .htaccss to 410 (Gone) any URL on my site that contains a question mark.
For example
A URL like this I'd want to have a 410
mysite.com/content/page.htm?no_redirect=true
but leave alone the original url
mysite.com/content/page.htm
When I created the mobile version of my site in May, a coding issue created a lot of pages with a wildard/question mark, and I'd like to tell google those are gone with .htaccess.
Thanks for any assistance - great site and resource here!

For all URLs including a ?, you are actually asking for any URL with a non-empty query string, matched with Apache's %{QUERY_STRING} variable. You may use a RewriteCond to match any character with a . to indicate it is nonempty, then use RewriteRule to send the 410 code.
You may want to couple this with an ErrorDocument directive to serve an error page accompanying the 410 code.
# Make sure an error page is defined for 410
ErrorDocument 410 /path/to/custom/410.html
# For any non-empty query string
RewriteCond %{QUERY_STRING} ^.
# Match any path and send a 410 response
# The [L] prevents further matches from being executed
RewriteRule ^ - [R=410,L]
Addendum after comments:
To apply the 410 only to URLs containing the no_redirect=true parameter, you may use that as the expression in RewriteCond. Since %{QUERY_STRING} contains the entire query string (except the ?) it will work to just use the entire key=value pair.
RewriteCond %{QUERY_STRING} no_redirect=true [NC]
RewriteRule ^ - [R=410,L]
Note I also added [NC] (no-case) to make it case insensitive.

Related

.htaccess 301 redirect from old RewriteRule to new

I have a problem with making a 301 redirection. I was using "flat" link system on website with RewriteRule:
domain.com/rubric-news
domain.com/article-815/news-title
RewriteRule ^rubric-([^*]*) news.php?kat=$1
RewriteRule ^article-([^*]*)/([^*]*)-([^*]*) article.php?id=$1&kat=$2&title=$3
Today I started to build better internal links system for SEO so now RewriteRules looks like this:
domain.com/news
domain.com/news/title-815
RewriteRule ^([a-z0-9]+)$ news.php?kat=$1 [L]
RewriteRule ^([^*]*)/([^*]*)-([^*]*) article.php?id=$3&kat=$1&title=$2 [L,NC,QSA]
now I should make 301 redirection from old links to the new ones and I don't know how to make it. Can anyone help me?))
The redirect directives would follow exactly the same principles as you have already used for the rewrites. (Although there are potential issues with the rewrites/regex you are currently using - see below).
Try it like this before your existing rewrites:
# Redirect "/rubric-<news>" to "/<news>"
RewriteRule ^rubric-([a-z0-9]+)$ /$1 [R=301,L]
# Redirect "/article-<815>/<news>-<title>" to "/<news>/<title>-<815>"
RewriteRule ^article-(\d+)/(\w+)-(\w+)$ /$2/$3-$1 [R=301,L]
I have assumed your "id" parameter is all numeric (as per your example). (Although your existing directives do not enforce this.)
Any query string will be be passed through by default (you do not need the QSA flag here).
You should test first with 302 (temporary) redirects to avoid potential caching issues. Clear your browser cache before testing.
Aside:
RewriteRule ^([^*]*)/([^*]*)-([^*]*) article.php?id=$3&kat=$1&title=$2 [L,NC,QSA]
The regex [^*]* matches anything that is not an asterisk (*) 0 or more times. You should be matching anything that is not a slash 1 or more times, ie. [^/]+. And you are missing the end-of-string anchor on the end of the regex (as you have on the preceding rule). With the very generic regex you are currently using (ie. [^*]*) this does not strictly matter, however, it is potentially ambiguous, ...
Your regex should be more restrictive (similar to the preceding rule). If your id consists of digits only then match only digits. If the title is only alphanumeric then match only letters and numbers, not everything. Currentrly it "looks like" the news and title parameters could contain hyphens, but that would introduce an ambiguity.
The NC flag is naturally superfluous here.
Consider something like this instead:
RewriteRule ^(\w+)/(\w+)-(\d+)$ article.php?id=$3&kat=$1&title=$2 [L,NC,QSA]
If your title can contain hyphens then change the regex accordingly. eg. ^(\w+)/([\w-]+)-(\d+)$

.htaccess forward silently (internally) url containing question mark and variable

There are so many questions and answers regarding forwarding with .htaccess, but unfortunately, I can't find any example that works for my situation.
I need to silently (internally) forward the URL
https://www.example.com/track?id=X
to
https://www.example.com/index.php?route=account/order/info&order_id=X
where X can be any number.
Please advise on what is the correct rewrite rule for this scenario.
I am using Apache 2.4.6 on CentOS 7
You can do something like the following using mod_rewrite at the top of your root .htaccess file to internally rewrite the request:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(\d+)
RewriteRule ^track$ index.php?route=account/order/info&order_id=%1 [L]
%1 is a backreference to the value of the id URL parameter, captured in the preceding RewriteCond directive.
You need to use a RewriteCond directive in order to match against the query string part of the URL, since the RewriteRule pattern matches the URL-path only.

trouble with simple mod_rewrite redirect rule

I have mod_rewrite working in a development environment.
This testing domain is using these rules in an .htaccess file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
# deal with potential pre-rewrite spidered / bookmarked urls
RewriteRule ^clothes/index.php?pg=([0-9]+)$ /clothes/index$1.php [R=301,L]
# deal with actual urls
RewriteRule ^clothes/[0-9a-z-]+-pr([0-9]+).php$ /clothes/product.php?pid=$1 [L]
The 2nd Rule works fine. Entering http ://testdomain.dev/clothes/shirt-pr32.php is silently delivered content from http ://testdomain.dev/clothes/product.php?pid=32 ...which is as desired and expected!
However, assuming this was applied to a live site, one that had originally used paths such as: http ://testdomain.dev/clothes/product.php?pid=32, I'd like to redirect any incoming requests following the old pattern to the new urls ...which is what the 1st Rule was intended to do.
My problem is my testing server seems to ignore the 1st Rule and serves the page as requested (page loads but address bar remains at http ://testdomain.dev/clothes/product.php?pid=32)
Any assistance or enlightenment would be most graciously accepted!
You need to match the query string within a RewriteCond, then backreference that RewriteCond from the rule. The RewriteRule only matches against the path, not the query string.
Here's a related post I previously answered with a similar request: Mod_rewrite rewrite example.com/page.php?v1=abc&v2=def to example.com/abc/def
You can't match against the query string in a rewrite rule, you need to use the `%{QUERY_STRING} variable in a condition and use the % to backrefernce groupings. So instead of:
RewriteRule ^clothes/index.php?pg=([0-9]+)$ /clothes/index$1.php [R=301,L]
You'll need:
RewriteCond %{QUERY_STRING} ^pg=([0-9]+)
RewriteRule ^clothes/index.php$ /clothes/index%1.php? [R=301,L]

Redirecting a URL that contains a question mark

It's been necessary to move the position of my blog and I'm trying to make sure I catch all of the links in via the old link and redirect them to the new one.
The blog has been moved from http://example.com/ to http://example.com/blog/
The URLs used to generate with the date before the post name (by default) which I've decided not to do, to keep the URLs memorable.
The issue is that there's a ? character in the original URL (the default URL produced by the CMS I'm using and it's causing problems with the redirect:
RewriteRule ^post.php?s=2012-01-01-blog-post$ /blog/blog-post? [R=301,L]
So I need to escape the ? somehow but I can't work out how!
I could use a more general redirect that avoids the ? but that would redirect to the listings page, not the article itself:
RewriteRule ^post.php$ http://tempertemper.net/blog/? [R=301,L]
How do I make it read the ? as part of the URL!?
Thanks for taking a look!
Martin :)
I think you need to use a RewriteCond to check for the presence of a QUERY_STRING value and then, assuming the query string matches your blog pattern, capture the blog name and then use the captured value in the RewriteRule. Something like this:
RewriteCond ${QUERY_STRING} ^s=[0-9]{4}-[0-9]{2}-[0-9]{2}-(.*)$
RewriteRule ^post.php$ /blog/%1 [R=permanent,L]
The %1 refers to the first capture pattern in the matching RewriteCond (and the RewriteRule will not trigger unless the RewriteCond is a match).
The reason this is necessary is that a RewriteRule cannot see the query string as part of the request path, so you have to check for (and capture) a query string using a RewriteCond before the RewriteRule.

Apache / Linux - .htaccess how to get specific variable from a query / url and apply to rewrite

I have a rule that works for one "direction" but, not the other.
A typical incoming url / query would be: (long url)
http://somedomain.com/getme.pl?dothis=display&partnum=1234567 (could be up to 9 digits)
I have this rule in place in my htaccess file:
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L]
Which works great for a bit of ease getting one of the unique part numbers:
http://somedomain.com/1234567.
However, I would like to make the long url "pretty" so, I assumed I could reverse(ish) it.
So, when a link on the site is clicked on (the long url) the htaccess file would process the long url to the beautified version.
I tried MANY attempts.
Here was my latest failure.
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(works)
RewriteCond %{QUERY_STRING} ^partnum=([0-9]*) #(tried to get partnum)
RewriteRule ^.* http://%{HTTP_HOST}/%1 [R] #(make the short url)
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(the known working rule)
I have tried a plethora of rules and visited many sites for advice.
I tried with just rules, just conditions and variations of query_string.
So, I believe I must just grab the "partnum" from the query and rewrite to /1234567 or http_host/1234567
Then, allow the other rule (works) to process.
So BOTH:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
and
http://somedomain.com/1234567
Display as: http://somedomain.com/1234567 in the browser.
and both passed the whole query to the getme.pl script properly.
I found some close answers here but, none that really explained what I needed.
Can someone please help?
From the sounds of it, this should get you moving down the right path:
# Your working rewrite, with extra param on the rewrite
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]
# Redirect for long urls, to pretty url
# -The appended '&rewrite' on the first rule will force this not to match
# -The trailing '?' on the rewrite will strip the query string
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]
Hope that helps.

Resources