Trailing Slash issues with Search Engines - .htaccess

When websites return page content with or without a trailing slash on their URLs, Search Engines might see these as separate pages with duplicate content which they could penalize. So how to use a 301 redirect to force search engines to see the correct version of a website ?
I tried with a .htaccess but was not successful.
I am not interested to add canonical meta tag in every page.
ISSUE;
example.com/siteinfo/user/login
example.com/siteinfo/user/login/
should not show same content.
Suggestions appreciated.

You can use in your .htaccess to redirect without slash:
RewriteEngine on
RewriteRule ^([^.]+)/$ $1 [R=301,L,NE]
Or for the version with slash:
RewriteEngine on
RewriteRule ^([^.]*[^/])$ $1/ [R=301,L,NE]

Related

Changing my website from dynamic CMS to static HTML. Should I 301 redirect or use other methods?

I am changing my website from a dynamic CMS-system (Umbraco) to a static classic .HTML. It is the on the same domain, but the URL will change.
Example: The URL is changing from:
www.example.com/information
To:
www.example.com/info.html
My question is:
What is the best way to redirect while keeping the best SEO page rank.
I am thinking about 301 redirect through .htaccess, but I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
Or maybe there is a different better way?
I do have a fine 404.
Also I need the right redirect code for .htaccess - if that's the right way.
I hope you guys can help me out.
I haven't try anything out yet, because I don't wanna do 301 before the site go live.
You need to implement 301 redirects from the old URL to the new URL in order to preserve SEO and ensure that any "old" links that have been bookmarked or linked to from other websites still work.
Exactly how you implement the 301 redirect (either in your server-side script or in .htaccess) does not really matter. However, if you are moving to an entirely static site then .htaccess is likely the only option you have.
I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
You need to redirect from the "old" URLs to the "new" URLs that you are using/linking to on the new site. (It makes no sense to redirect the other way as that would just break everything!)
You can probably just use the simple mod_alias Redirect directive.
For example, to 301 redirect from /information to /info.html you could do the following:
Redirect 301 /information /info.html
Bear in mind that 301 redirects are cached persistently by the browser. To prevent caching issues it is advisable to test first with a 302 (temporary) redirect.
Have you considered keeping the same URLs? This would obviously negate the need for implementing redirects. You could employ URL-rewriting if the underlying file is called info.html. For example, using mod_rewrite:
RewriteEngine On
RewriteRule ^information$ info.html [L]
The above would internally rewrite a request for /information to info.html. The user only sees /information in the browser address bar, but info.html is served from your site.
Taking this further, it would be easier if the new "file" is simply the same as the old URL, just with a .html extension. For example, the URL is /information and the underlying file is information.html. You can then use a single rule to rewrite all your URLs. For example:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
The above assumes the old URLs do not contain additional slashes (ie. consist of a single path segment. In other words, all files are in the document root) and do not contain dots.
White
I finally got my page ready to go live, and i changed all my new URLS to the same name as the old URL, just with a .html extension - as u said. After that i used:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
And it works fine.
I do have a question about if a "RewriteRule ^(.*).html$ /$1 [L,R=301]" would be better? I mean both "/page.html" and "/page/" works, and this could mess with my former SEO ranking?
Also: what do u think of this:
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
And by the way. Do you use "page.html" or just "/page" in your href to prevent the .html showing?
//MM

.htaccess redirection for image link

I would like to know if it is possible to set up a redirect in the .htaccess file so that if another website directly links to an image on my site, instead of the image opening in a browser window on its own, the page that the image is hosted on is displayed.
This page has the same name as the image i.e. if the image the other site is linking to is
Case1
www.example.com/subdirABC/gallery/image.jpg
**to**
www.example.com/subdirABC/
**or**
www.example.com/subdirABC/index.php
Case2
www.example.com/subdirXYZ/gallery/image.jpg
**to**
www.example.com/subdirXYZ/
**or**
www.example.com/subdirXYZ/index.php
Its work for me
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_REFERER} !^https://example.com
RewriteRule ^(.*)/gallery/(.*.jpg) /$1/ [L,R=301]
Try something like the following in .htaccess:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com
RewriteRule ^([^/]+/)gallery/[\w-]\.jpg$ /$1 [R,L]
What this says is... for all requests that match the pattern /<subdir>/gallery/<filename>.jpg and are not being linked to from your site (example.com) then redirect to /subdir/.
Additional notes...
Direct links (which includes search engine bots) and user-agents that fail to send the HTTP Referer header will also be redirected to the "page".
<filename> contains just the characters a-z, A-Z, 0-9, _ and -.
$1 is a backreference to the first captured group in the RewriteRule pattern, ie. ([^/]+/) - which matches subdirXYZ/ in your example.
This is a 302 (temporary) redirect.

Google result redirect url

I updated my site by removing some unnecessary paths from the url. Is it possible to redirect people accessing the old url directly to the new one?
Old: https://example.com/board/index.php?/topic414
New: https://example.com/topic414
Some people have old links, google also has old links indexed. As a temp fix I have made an index page in /board that redirects all old links to the new homepage. Otherwise it would be just a white screen.
You can use this rule as your very first rule in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /board/index\.php\?(/\S+)\s [NC]
RewriteRule ^ %1 [L,NE,R=301]

Joomla 3.0 - redirect 301 remove index.php add .html

I migrated an old joomla site to a Joomla 3.0. I changed the URLs into SEF friendly URLs and i need to redirect all old URLs (www.mysite.com/index.php/mypage) to the new one (www.mysite.com/mypage.html)
I tried to add this code at the bottom of my .htaccess but it doesn't work:
RewriteEngine on
RewriteRule ^/index.php/(.*)$ http://www.nextlog.it/$1 [R=301,L]
RewriteRule ^/index.php/(.*)$ $1.html [R=301,L]
You don't need the first rule, and if this is in your htaccess, remove the leading slash in your regex pattern:
RewriteEngine on
RewriteRule ^index.php/(.+)$ /$1.html [R=301,L]
I would use com_redirect which is built in and will do permanent redirects. Over time the search engines will update their indexes to go to the new pages, then you can delete the redirect pages ... but in the meantime monitor where direct links are coming from. THose you need to leave in place until the direct links are updated.

Redirect hundreds of urls with htaccess

hello my url structure right now for the majority of my links is:
www.url.com/category1/sample-keyword.html
I am looking to redirect them to the new url that has dropped the word sample from the url structure ie to this:
www.url.com/category1/keyword.html
what should i put in htaccess that auto redirects all the urls in the www.url.com/category1/ section to redirect to the new url structure?
This should do the trick:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*/)sample-(.*)$ $1$2 [L,R=301]
It will match all URLS with the substring /sample- and strip it from the URL. Depending on your site organization, you may need to adjust the pattern, but that should be a good jumping-off point.
RewriteEngine On
# Redirect sample-*.html to *.html
RewriteRule ^\/?category([0-9]+)\/sample\-([^\/]+)\.html$ http://www.url.com/category$1/$2.html [R=301]
# Serve *.html
RewriteRule ^\/?category([0-9]+)\/([^\/]+)\.html$ page.php?category_id=$1&keyword=$2 [L]

Resources