Check if canonical header is set in htaccess - .htaccess

I need to find a way to check if there is set a canonical URL via htaccess. If no canonical URL set then execute rewriterule
already tried %{HTTP:link} and %{HTTP:X-link} and %{HTTP:Link} and %{HTTP:X-Link}
RewriteCond %{QUERY_STRING} ^album=113$ [NC]
RewriteCond %{HTTP:Link} !canonical [NC]
RewriteRule ^gallery/(.*)$ https://old.mydomain.be/gallery/$1 [L,P,QSA,NC]
If canonical header set passthrough the content from old.mydomain.be. Now it doesn't show anything.

This is not possible. The Link header is a response header. You can only redirect based on request headers in .htaccess.

Related

How can I use variables with .htaccess Redirect?

So, I have a local URL /go/xxx where xxx is any string or number of the user's choice. I'd like to redirect that to /handlers/Redirect?q=xxx, and again, xxx being that variable.
I tried using Redirect, but I had no successor.
Redirect /go/$ /handlers/Redirect.html?q=$
I, then, read that you can't have variables in Redirect and should use mod_redirect. I did that, and still had no successor.
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^go/$ handlers/Redirect.html?q=$
Can anybody tell me what's wrong and what I have to do to make it redirect correctly?
Conclusion/example: Redirect from https://example.com/go/support to https://example.com/handlers/Redirect.html?q=support.
You're on the right way but you are checking %{QUERY_STRING} and you should check %{REQUEST_URI}.
Check this short rule ( rewrite /go/anything <> /handlers/Redirect.html?q=anything ):
RewriteEngine On
RewriteBase /
RewriteRule ^go\/(.*)$ /handlers/Redirect.html?q=$1 [L]

htaccess redirect url by adding parameters

Need Guidance of 301 redirecting Url through htaccess matching specific format
Existing URL
http://www.example.com/index.php?option=com_toys&limitstart=20
Proposed URL
http://www.example.com/index.php?option=com_toys&view=list&Itemid=2&limitstart=20
Here limitstart may change to 0,20,40,60 to even 1000 and thus should remain same in the new proposed url too
Can anyone advise on to redirect using htaccess of above
You need to match query string using RewriteCond using a regex to capture both parameters:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(option=com_toys)&(limitstart=\d+)$ [NC]
RewriteRule ^/?index\.php$ %{REQUEST_URI}?%1&view=list&Itemid=2&%2 [L,NC,R=301]

Redirect only urls with certain parameter to a new domain

I am trying to redirect any page on a site that contains a p=item parameter at any point in the url to a specific url on a different domain.
I've tried to piece it together from various instruction on how to do it via htaccess but to no avail.
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)p=item(&|$)
RewriteRule ^ http://different-domain/url? [L,R]

Rewrite URL Structure

I would like to change the URL structure of my site. My current URL structure is like this:
www.domain.com/events/events.php?location=san%20francisco
I want to change the URL structure to:
www.domain.com/events/san-francisco
My XMPL site map has the URLs listed like the first example. I want the search engines to index the URL like the second example. What are all the things I need to do to achieve this? The {city} in the location parameter is dynamic depending on the user's IP address. Do I need to change my XML sitemap to list new URL structure? What do I need to put in my .htaccess file. Do I need to change the actual URLs links on my website to the new structure or can I just use .htaccess?
This should work:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /events\.php\?location=(.*)%20(.*)\ HTTP
RewriteRule ^ /events/%2-%3\? [R=302,L]
RewriteRule ^events/(.*)$ /events.php?location=$1 [L]
Changing R=302 to R=301 when you know it redirect correctly
EDIT:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?location=(.*)%20(.*)&lid=(.*)&slid=(.*)\ HTTP
RewriteRule ^ /%3/%4-%5/%6/%7\? [R=302,L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ /$1/$1.php?location=$2&lid=$3&slid=$4 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?location=(.*)%20(.*)\ HTTP
RewriteRule ^ /%3/%4-%5\? [R=302,L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$1.php?location=$2 [L]
You have to use a .htaccess file similar to the one below. It will match fancy URL and forward the request to the right php file.
RewriteEngine On
RewriteRule ^([^/]*)/(.*)$ /$1/$1.php?location=$2
With this, both www.domain.com/events/events.php?location=san%20francisco and www.domain.com/events/san-francisco will lead to the same page.
But if you don't change your sitemap nor the links on your site, search engines won't know that.
You have then 2 solutions :
change all your links in your site and sitemap with the new structure URL
use a redirect rule to redirect "old" pages to the "new" ones
From what I know about SEO, the first option is the best. Or even better, a combination of both solutions : change all your links, and use a redirect 301 rule so pages already indexed by search engines will not lost their ranking and to avoid duplicate content.
RewriteRule ^([^/]*)/(.*).php?location=(.*)$ http://www.yourdomain.com/$1/$3 [R=301]

htaccess mod_rewrite with dynamic parameters

I am new to mod_rewrite. I am trying to forward a URL to another one, but I cannot get it to work.
Say I want to forward this URL:
/cansas.php?m=2&id=2-0-0-0&sid=cansas to
/cansas-is-good-for-you and let the header respond with a 301, or just update the URL through [R].
I have this in my .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^cansas.php?m=2&id=2-0-0-0&sid=cansas$ cansas-is-good-for-you [NC,R=301]
I figured I could just do a simple forwarding, but somewhere along the way it stops working. If I cut out the ?m=2&id= etc, it forwards just the cansas part to the new part so it looks like this: cansas-is-good-for-you?m=2&id=2-0-0-0&sid=cansas.
How can I forward it when I have several dynamic parameters in the URL string? Example on pages I need to forward:
/cansas.php?m=2&id=2-0-0-0&sid=cansas
/cansas.php?m=2&id=2-1-0-0&sid=cansas
/cansas.php?m=2&id=2-2-0-0&sid=cansas
Any help would be greatly appreciated :)
Maybe it isn't possible to do it this way? The way I have set it up at the moment is that I want to use new URLs called /cansas-is-good-for-you which reads from the source /cansas.php?m=2&id=2-0-0-0&sid=cansas, but the URL shown in the browser should be: /cansas-is-good-for-you. I need to forward that old cansas.php? URL to the new URL :)
You need to check the query of a URL with the RewriteCond directive as the RewriteRule directive only handles the URL path:
RewriteCond %{QUERY_STRING} ^m=2&id=2-0-0-0&sid=cansas$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]
If you want to check for just one parameter, use this:
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]
And to do this just for initial requests, you need to check the request line:
RewriteCond %{THE_REQUEST} ^GET\ /cansas\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]

Resources