Links starting with double slashes cause invalid requests - .htaccess

All links on my website are protocol-less and start with double slashes:
href="//site.com/page.html".
And in the log I see many requests like: 404 - site.com/site.com/page.html
Which means some browsers are interpreting these absolute links as relative. By looking at the user agents I assume those are mostly bots.
Can I fix requests such as site.com/site.com/page.html with .htaccess by directing them to the proper URI? (site.com/site.com/page.html => site.com/page.html)

Try adding this to your document root (of the site that's hosting these protocol relative links):
RedirectMatch 301 ^/site.com/(.*)$ http://site.com/$1
or:
RewriteEngine On
RewriteRule ^site.com/(.*)$ http://site.com/$1 [L,R=301]
If the site that is hosting these links is already site.com, you can remove the http://site.com bit from the targets.

Related

How to remove duplicate urls that are affecting my SEO

Hoping someone can help me. I've tried everything I can think of and have spent almost 2 weeks now trying to solve this issue. I'm using SERanking for a site audit and it is indicating I need to fix the duplicate url issue.
Pages With Issues:
https://www.droneworxphotography.com/
0 (Referring Pages)
https://www.droneworxphotography.com/index.html
10 (Referring Pages)
https://www.droneworxphotography.com
10 (Referring Pages)
My htaccess:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.droneworxphotography.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]
I've tried adding a 301 redirect, but it is not resolving my issue.
Redirect 301 /droneworxphotography.com/index.html /droneworxphotography.com
I'm hoping someone can help me with fixing this.
Thanks
As mentioned in comments, you need to actually fix your internal links so you are consistently linking to the canonical URL throughout your site, before attempting to implement redirects.
The "reported issues" you are seeing are presumably discovered by crawling your site.
As far as I can tell, my internal links are consistent.
Unfortunately not...
<!--begin footer_details -->
<ul class="footer_details">
<li>
<li> Redacted Photography
(Ignoring the incorrectly nested li elements - this is not valid HTML.)
You have a relative link to index.html in the footer of every page. This should be either, a root-relative URL (a single slash):
Redacted Photopgraphy
Or, an absolute URL (include the scheme + canonical hostname). For example:
Redacted Photopgraphy
Note the trailing slash after the hostname.
These two are the same. (Providing the hostname is correctly canonicalised before reaching your site. It appears to be - you have HTTP to HTTPS and non-www to www redirects in place.)
<!--begin logo -->
<img src="lib/images/cropped-DroneworxLogo_small.png" alt="Logo">
<!--end logo -->
<!--begin nav -->
<ul id="nav">
<li>
<i class="icon-home"></i><br>Home
You have 3 links on every page (the two above and one in the footer - copyright link) that links to scheme + hostname but without the trailing slash.
Strictly speaking, the "correct" URL includes the trailing slash after the hostname.
User-agents (ie. browsers) will correct this and always append the trailing slash, but you should be consistent and link to href="https://www.example.com/" - with a trailing slash. (Or use href="/" as mentioned above.)
Your XML sitemap already correctly links to the canonical absolute URL (with a trailing slash): <loc>https://www.example.com/</loc>
It is this inconsitency that is causing the SEO tool to report on both https://www.example.com/ (slash) and https://www.example.com (no slash). Although this is not really an SEO issue, since user-agents will always append the trailing slash to form a valid URL, as mentioned above. See my answer to the following question on the Webmasters Stack for more information about the trailing slash after the hostname: Is trailing slash automagically added on click of home page URL in browser?
Note that the trailing slash immediately after the hostname (at the start of the URL-path) is not the same thing as the trailing slash at the end of the URL-path.
Redirect to canonical
Once you have corrected the above then you can implement a redirect to correct any indexed URLs (or backlinks from external third parties).
The only redirect you could implement here is from /index.html to / (to remove index.html). You cannot redirect to append a trailing slash after the hostname (this is the same URL).
(Your mod_rewrite directives remove the trailing slash at the end of the URL-path - which is something different entirely. Your site does not have an issue with this, but this redirect causes no harm.)
To remove index.html you could add the following rule at the top of the .htaccess file, before your existing redirects (and after the RewriteEngine directive):
RewriteRule ^index\.html$ https://www.example.com/ [R=301,L]
NB: Test first with a 302 (temporary) redirect to avoid potential caching issues.
This does assume you aren't using a front-controller pattern (and rewriting requests to /index.html). It doesn't look like you are - your site looks like an entirely static HTML site?
A quick check in Google using a site: search does not show that index.html has been indexed. So, this was unlikely causing you an SEO issue at the current time.

How do I redirect a web URL using .htaccess

I want to redirect users who enter this website URL:
http://www.myWebsite.com/bananas
to:
http://www.myWebsite.com/fruits/bananas
I cant test it because I'm sending this to somebody.
I have these but I don't know for sure which one works:
RedirectMatch 301 http://www.myWebsite.com/bananas(.*) http://www.myWebsite.com/food/bananas $1
Options +FollowSymlinks
RewriteEngine on
rewriterule ^bananas(.*)$ http://www.myWebsite.com/food/bananas $1 [r=301,nc]
Please specify if you want to redirect or rewrite. The rules you are using serve different purposes and you used both in your example.
Redirect: Actually load a different site when entering the url (end up at url and content of /fruits/bananas)
Rewrite: Url stays the same but server provides rewritten content (url stays at /bananas, but show /fruits/bananas content)
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also it is not clear if you only want one single directory to be redirected or all files that are contained in that directory.
Checkout this as a guide: http://www.htaccessredirect.net/
I believe you are looking for
Redirect 301 /bananas http://www.myWebsite.com/fruits/bananas
The HTTP 301 stands for Moved Permanently.
I haven't tested it, though.

Redirect all subdomains to subdomain at different domain

Hi I need to redirect all subdomains in a domain to the same subdomain but at a different domain. The best way im guessing is through a htaccess file but im not sure how the file would be.
Example:
sd1.example.net ---> sd1.example.com
sd2.example.net ---> sd2.example.com
sd3.example.net ---> sd3.example.com
But I need this to be done for all of the subdomains in example.net. Thanks.
If you have an Apache server running on example.net and the requests for all the subdomains look in the same parent directory you can do something like the following:
RewriteEngine On
### Find the subdomain part (it will be available in %1)
### Use one of the RewriteCond-s and delete the other one
# Only redirect subdomains, not plain example.net
RewriteCond %{HTTP_HOST} ^(.*)\.example\.net$
## Redirect both subdomains and plain example.net (uncomment to enable)
#RewriteCond %{HTTP_HOST} ^(.*\.)?example\.net$
# Find the path requested (it will be available in $0)
# This rule does not attempt to match the domain, only the path
# Redirect subdomain and path to example.com
RewriteRule ^.*$ http://%1.example.com/$0 [L]
I haven't tested this so it might be missing query strings, etc. It will also undesirably redirect https:// to http://. As long as you have a single .htaccess file that can affect all your subdomains this should work, or at least be a very good starting point. Check out Apache's mod_rewrite documentation for more information about how this works.
EDIT
Having recently wanted to do exactly this myself recently, I have worked out a short .htaccess file that does the trick:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*\.)?olddomain\.com$
RewriteRule ^(.*?/)?public_html/(.*)?$ "http\:\/\/%1newdomain\.org\/$2" [R=301,NE,L]
It assumes the following file structure:
.htaccess
public_html/
+-content
lots/
+-public_html/
| +-content
of/
+-public_html/
| +-content
subdomains/
+-public_html/
+-content
My main site (newdomain.org) is in /public_html/. I have a number of subdomains, e.g. subdomains.newdomain.org which is in /subdomains/public_html/. This keeps all the files of each my subdomains completely separate from each other and my main site. (My hosting service recommends /public_html/, /public_html/subdomains/ but that means each subdomain is also accessible at newdomain.org/subdomains/ which is not what I want). The only restriction this gives me is that I can never have a subdomain called public_html, which I think you'll agree is perfectly acceptable.
The flags on the rule are as follows:
R=301 - Redirect with a 301 Moved Permanently code. You can change the code if you don't need a permanent redirect, e.g. 302.
NE - No Encoding - Don't URI encode the new address, i.e. keep % as %, not %25
L - Last - Stop processing rules
Note that the .htaccess file must be in the root directory of your web server, not in the directories with your content files. This is because the rewrite rule works at the file system level, not the URL address level.
An address:
any.subdomain.olddomain.com/any/address.html?any=query&you=like
is changed to:
any.subdomain.newdomain.org/any/address.html?any=query&you=like

redirect ALL urls from old site to index.php on new one with .htaccess?

A couple questions, simply:
Is the .htaccess file generally stored in the public_html directory? I think so, no?
If I do not find such file, can I simply create it and upload it with FTP?
3 (most importantly). What is the code I need to redirect ALL URLS to ONE new URL, namely, http://www.newsite.com
Is it nothing more than Redirect 301 / http://www.newsite.com/?
Thanks
Is it nothing more than Redirect 301 / http://www.newsite.com/?
Correct, it is the only thing you need. It will redirect anything starting with / to the appropriate place in http://www.newsite.com/. Example:
You go to http://oldsite.com/some/path/to/file.php, you'll get redirected to http://www.newsite.com/some/path/to/file.php.
If you want everything to go simply to the document root of the new site, you can use a RedirectMatch instead:
RedirectMatch 301 .* http://www.newsite.com/
So if you go to http://oldsite.com/some/path/to/file.php, you'll get redirected to http://www.newsite.com/
Not all apache installations come with mod_rewrite installed, if it's not installed you'll get a 500 server error if you attempt to use the rewrite engine. However, mod_alias is usually always installed.
Question 1: Whether you can just upload .htaccess to public_html depends on the web host, but that will likely work.
Question 2: I would set up the redirect like this:
RewriteEngine On
RewriteRule ^(.*) http://www.newsite.com/ [R=301,L]

URL masking (?in .htaccess) from one domain to another

I've been searching the archives but I can't find anything that is making too much sense to me.
I have a site with a couple of subdomains which redirect to other sites.
E.g.
the visitor types - www.jmp.redtwenty.com.au - and is redirected to - http://creator.zoho.com/redtwenty/jmp-conversion-tracking
Is there any way to mask this redirect so that the visitor still sees jmp.redtwenty.com.au in the address bar?
I keep seeing mention of a rewrite rule in .htaccess but not sure if that is what I want.
Thanks
Mike
You can do this a few ways, but you'll need to make sure mod_proxy is enabled.
If you have control of the server config or the vhost config of the www.jmp.redtwenty.com.au/ domain, you can add this to it:
ProxyPass / http://creator.zoho.com/redtwenty/jmp-conversion-tracking/
Or in the htaccess file in the document root of http://www.jmp.redtwenty.com.au/:
RewriteEngine On
RewriteRule ^(.*)$ http://creator.zoho.com/redtwenty/jmp-conversion-tracking/$1 [L,P]

Resources