Google result redirect url - .htaccess

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]

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

How to redirect multiple old urls to new urls using a 301 redirect via htaccess

I have recently change some URLs from my website to make it more SEO friendly. I have old urls that I would like to link to new urls. The old urls are no longer available but I would like the old urls to redirect to the new urls.
My current 301 redirect code below is for http:// to https://www, which I need.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301,NC]
How do I make it so that I can also redirect from old urls to new urls as well. Is it possible with the 301 redirect?
Old url examples:
www.mywebsite.com/book-stories.html
www.mywebsite.com/book-journals.html
New url examples:
www.mywebsite.com/stories.html
www.mywebsite.com/journals.html
Any help would be much appreciated.
If you only have a handful, static redirections you can do it appending RewriteRules just as you are doing:
RewriteRule ^book-stories.html$ stories.html [R=301]
RewriteRule ^book-journals.html$ journals.html [R=301]
If they all have a fixed pattern, you could exploit that to reduce the amount of rules.
RewriteRule ^book-([a-z0-9.-]+).html$ $1.html [R=301]
If you have many different rules that you will be updating, you can look into indexed maps or even database-based RewriteMaps
I tried the above code and didn't work and to be honest I could have been doing it incorrectly. I did look into it further and tried another 301 redirect
Redirect 301 book-stories.html https://www.mywebsite.com/stories.html
Redirect 301 book-journals.html https://www.mywebsite.com/journals.html
That worked for me. Hope this helps anyone else who will encounter this issue.

Exclude a substring in htaccess RewriteRule redirect

I manage an old site with multiple domain with the same name,
google has indexed some old url with the language structure
http://www.example.com/eng/whatever
and
http://www.example.it/eng/whatever
and now I don't use this structure anymore, but I want to do a redirect to my new structure
http://www.example.it/en/whatever
so everything after the /eng/ must be appended to the new structure, excluded the /eng/ substring.
But it doesn't work, I have a redirect to
http://www.example.it/en/eng/whatever
which is wrong! I did not want /eng in the redirect url
In my .htaccess I have this
#this will redirect the url with /eng/whatever to www.example.it/en/whatever, regardless from the top level domain
RewriteRule ^eng\/(.*)$ http://www.example.it/en/$1 [R=301,L]
#redirect example.com on www.example.it/en/
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*example\.(com|pt)?(:[0-9]*)?$ [NC]
RewriteRule ^(.*)$ http://www.example.it/en/$1 [R=301,L]
Try replacing all of that with just this:
RewriteRule ^eng/(.*) en/$1 [R,L,NE,DPI]
If it works, you can change the R to R=301
Warning
To test this, you must use a new browser. This is because browsers cache 301 redirects, so your current browser will keep redirecting to the old (wrong page) without even talking to your server and hitting the new .htaccess—unless you clear the browser cache.

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.

Google not indexing my URLs as expected

My site passes a variable (a phone number) in the URL between pages. It grabs the phone number from the database and writes the URL link as: url.com/phone-number/id.
However, if the target page does not have a phone number it is replaced with a 0, so,
url.com/0/id.
In my .htaccess file to rewrite old query php parameters to clean urls I put a default url.com/0/id in the rewrite. Old pages did not have a phone number so all redirect will have the 0.
# 301 redirects ad
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteCond %{QUERY_STRING} ^id=(\d+)$ [NC]
RewriteRule ^page.php$ /page/0/%1? [R=301,NE,NC,L]
My problem is Google is indexing all new pages with the url.com/0/id and not url.com/phone-number/id, even though if you browse the site you will see url.com/phone-number/id for pages with phone numbers.
I am not sure if it is my URL rewrite or Google bot's behavior that is causing this.
Don't use R=301 for above rule as 301 means permanent redirect. Use 302 (temporary redirect):
RewriteCond %{QUERY_STRING} ^id=(\d+)$ [NC]
RewriteRule ^page\.php$ /page/0/%1? [R=302,NC,L]

Resources