Canonical URLs and trailing slash on root URL - ruby-on-rails

When working with canonical URLs, does a trailing slash make any difference on a root URL?
I put the following canonical tag into my Rails site head:
<link rel="canonical" href="<%= url_for(:only_path => false) %>" />
...to ensure any parameterized URLs resolve to the base URL.
However, when I navigate to http://www.example.com, the canonical link shows up with a slash at the end:
<link rel="canonical" href="http://www.example.com/" />
I know trailing slashes DO matter when a path element is present in the URL, but thought they didn't matter on root URLs. However, I then ran into Matt Cutts presentation on canonical tags, where he clearly states that they are considered different URLs:
From http://www.mattcutts.com/blog/canonical-link-tag/ (See slide 3):
These URLs are all different:
www.example.com
example.com
www.example.com/
example.com/
Can anyone shed some light on what he means?

URLs which point to directory names (often with the expectation that a web server handler will return some sort of 'index'') without a trailing slash are actually invalid. Most web servers will automagically correct these requests with a redirect to the same URL with a trailing slash added.
So behind the scenes, your request for http://example.com results in a redirect from the web server to http://example.com/ which is why you're seeing the surprise trailing slash.
The short answer is that proper URI paths matter everywhere- root directory or not. For a deeper and more ranty answer, take a look at this page.

Related

Leading slash in URL path with base URL does not work

I'm working on a plain html site on Godaddy (linux).
Client wants URLs to start with a slash, but adding the slash breaks the URL.
This works...
<img src="images/logo_cascade.png">
This does not...
<img src="/images/logo_cascade.png">
I tried adding a 'base url=""' both with and without a trailing slash, but no luck.
<base url="http://brinsterinc.com/cascade/" />
What am I missing?

Can I include canonical URLs in sitemap for SEO?

Can I include canonical URLs in sitemaps for SEO?
For example www.example.com/url.html is a duplicate page of www.example2.com/url.html.
So I used following tag in www.example.com/url.html page for SEO not to be penalized by search engines:
<link rel="canonical" href="www.example2.com/url.html">
Now my question is can I display www.example.com/url.html URL inside of www.example.com/sitemap.xml?
I already display www.example2.com/url.html URL inside of www.example2.com/sitemap.xml.
Please suggest me what I have to do.
You can include these two pages into your sitemap.xml and there won't be a problem for SEO because you're using the rel="canonical" tag. Indeed, when web crawlers will try to index the duplicate page, they will see the rel="canonical" tag and they will index the second page (the good one).
For better index you must leave one URL - canonical URL in XML site map

MVC / .NET Root URLs

In my layout page, I have:
<link href="~/Content/bootstrap.css" rel="stylesheet">
My understanding is that this should not be altered when it is sent to the client. However, when I set up the website as a virtual application under a "myapp" folder in IIS, the HTML is:
<link href="/myapp/Content/bootstrap.css" rel="stylesheet">
I'm a bit confused as I had thought I would need to change these URLs to:
<link href="#Url.Content("~/Content/bootstrap.css")" rel="stylesheet">
in order for this to work correctly.
So do I need to use URL.Content to get the correct root URL of the app/website, or can I just put tildes into the actual HTML src + href elements, and assume it will be outputted correctly by IIS?
As of ASP.NET MVC version 4 (or actually Razor version 2), the tilde links are essentially shortcuts to Url.Content(..).
You actually answered your own question. Yes, you should use Url.Content() for your relative paths. A simple tilde in front of relative paths are only parsed in the client's browser,which treats all URL's under the http://www.foo.com/ as a single domain, so will try to look for resources at http://www.foo.com/ and not http://www.foo.com/myapp/.

How to remove or disable the slash at the end of the URL?

I don't know exactly what is the problem, but I have done a lot of research about trailing slash in PHP.
My problem is that when I visit my site I always see trailing slash and it causes all the resources to fail to load.
How do I disable trailing slash that's being added in .htaccess.
Thanks!
PHP does not add / to URLs by default, it's your script doing that.
If you want to use the style.css regardless, use the absolute or the full path - so if your style.css is on http://domain.com/style.css, then you can either use
<link rel="stylesheet" type="text/css" href="/style.css">
or
<link rel="stylesheet" type="text/css" href="http://domain.com/style.css">
With your file.php thing, it looks in http://domain.com/file.php/style.css, while either of the above uses the file you created at http://domain.com/style.css.
That would as well work for http://domain.com/folder/file.php - simply use /folder/style.css in the href-tag or, once again, the full URL to it.
The / in a URL indicates a directory on the file systen of your webserver so your request for file.php/ is looking for a directory and not a PHP script file.
A htaccess rewrite so that any directory automatically has a trailing slash appended can be found in the rewrite guide:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]
Here is what I found:
Remove the trailing slash in url with htacess
I think this is what you are looking for.
How to find it:
Just do a search for remove ending slash of url
just use the full path to the css file for eg.
<link rel="stylesheet" type="text/css" href="http://yourdomain/folder/file.css"/>

URL with trailing slash breaks layout - how can I fix this without using base tag?

I am developing a website, and I have set up the following htaccess rules
RewriteEngine On
#RewriteRule ^([0-9]+)?$ index.php?page=$1
#RewriteRule ^([0-9]+)/([0-9]+)?$ index.php?page=$1&post=$2
RewriteRule ^([A-Za-z0-9-]+)?$ index.php?pagename=$1
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.php?pagename=$1&post=$2
This ensures that instead of http://www.mysite.com/index.php?page=2 showing the page
I get to use the friendlier method of http://www.mysite.com/about-us
* note I have not included a trailing slash.
In the page my css files are included as:
<link href="css/style.css" rel="stylesheet" type="text/css" />
and located at www.mysite.com/css/style.css
And this works well, however if I want to include a trailing slash (i.e. http://www.mysite.com/about-us/)
Then my css files do not load and I get an error where the Firefox source browser says:
The requested URL http://www.mysite.com/about-us/css/style.css was not found on this server.
This is because the page is determining about-us to be a directory instead of a page.
I am not keen to use the basehref tag like <base href="http://www.mysite.com/" />
Are there any other options?
Relative URLs are resolved from a base URL that is the URL of the document the relative URL is used in if not specified otherwise.
Now to fix this incorrect reference, you have two options:
change the base URL using the BASE element,
change the reference
by adjusting the relative URL path to the base URL path, or
by using just an absolute URL path, or
by using an absolute URL.
Since you don’t want to use the BASE element, you will probably need to adjust the URL you are using to reference the external resource.
The simplest would be to use the absolute URL path /css/style.css instead so that it is independent from the actual base URL path.

Resources