How to remove part of URL .htaccess - Magento - .htaccess

I need to remove the last two sections [/../..] of any URL requests that include [/desc/name-of-product] via .htaccess.
Example:
[www.domain.com/product.php/id/10101/desc/apple-laptop-computer] ->
[www.domain.com/product.php/id/10101]
[www.domain.com/product.php/id/985/desc/hp-computer] ->
[www.domain.com/product.php/id/985]
I am working on a Magento site that currently redirects old URLs (Example: www.domain.com/product.php/id/10101 to the new Magento URL www.domain.com/product-name via the URL Rewrite Management tool in Magento. The problem is the old domain was allowing Google to crawl two URLs for the same product in the examples above.
Since Magento is rewriting the shorter URL in the example above properly, I need a rewrite rule to remove the last two sections [/desc/name-of-product] of any incoming links that have a [/desc/*] section.

RewriteEngine on
RewriteBase /
RewriteRule ^/product.php/id/(\d+)/desc/[^/]+$ /product.php/id/$1 [R=301]
This will do an HTTP 301 redirect to your new URL, so Google will be informed that the page has moved permanently.
More informations on the Apache rewrite mod
List of HTTP status codes

Related

Redirect all traffic if referrer is external

My old website is located in public_html. I have a new version of the site, which i installed in public_html/new/ .
I want to redirect all traffic from https://example.com to https:/example.com/new but i want to allow users to browse the old version (https://example.com) if they click a link from my new site.
Tried using:
RewriteCond %{HTTP_REFERER} !^https://example.com/.*
to match the referrer, but it doesn't seem to work.
Any help would be much appreciated.
RewriteEngine On RewriteCond %{HTTP_REFERER} !^https://example.com/.*
​RewriteRule ^$ https://example.com/new/? [R=301,L]
You've implemented this as a 301 (permanent) redirect (as it would need to be for SEO if you are migrating from an old to new site). However, the 301 redirect will be cached persistently by the browser. And redirect the user back to /new (from cache) without making a request to the server.
(This redirect also only redirects the old homepage. Inner pages are not redirected. Is that the intention?)
This would need to be a 302 (temporary) redirect - to avoid the redirect being cached, but that's not so good for SEO.
You could include a ?noredirect=1 parameter on URLs back to the old site (in the root) - a different URL - and only redirect when this param is not present. However, you would need to persist this URL param across all URLs having navigated from the new to old site. (And would allow anyone to access the old site by simply appending this URL param.)
However, #CBroe's suggestion in comments would be preferable. To move the old site to an /old subdirectory and the new site replaces the old site in the root. This would be better for SEO and users. Optionally, you could then simply block access to /old if the Referer is not the same domain (which would allow links from the new to old). And would prevent (most) bots from accessing /old (although you would still implement a X-Robots-Tag: noindex HTTP response header). Note, however, the Referer is unreliable and might not be set at all by some browsers (user's settings).

Need help redirecting one website URL to another with apache2 and .htaccess

I have two domains - https://hosting.opensimcity.org, and http://paradigm.pocketmud.com and I want to redirect the latter to the former. That is, when someone connects to http://paradigm.pocketmud.com I want it to redirect to https://hosting.opensimcity.org/paradigm
Any tips on doing this in my .htaccess file?
Just try the following htaccess redirect code:
Redirect 301 / https://hosting.opensimcity.org/paradigm
This 301 redirect response notifies the search engines that the page has moved permanently from the old URL to the new URL. The search engines also transfer the old URL page rank to the new URL.

Old pages PR to

I have site that has many links to its pages. And I will completely renew site, update CMS, content and page structure. Domain remains the same.
What will get users in browser if they find somewhere on the Internet old link
to old sites page and follow by that link while it's already a new site and
old page where link leads to doesn't exist?
How to make a redirect or something from these old links if old
pages not open to root of domain?
What's about Google in that case how do not lost PR and
redirect that PR weight of old pages to main domain?
I am kindly appreciate any relative discussion on this topic because it's really interesting from all sides.
What will get users in browser if they find somewhere on the Internet old link to old sites page and follow by that link while it's already a new site and old page where link leads to doesn't exist?
They will get a 404 Not Found.
How to make a redirect or something from these old links if old pages not open to root of domain?
You'll need to create a 301 redirect from every old page to the equivalent new page. You can do it using mod_alias:
RedirectMatch 301 ^/old_page/(.*)$ /new_page/$1
or mod_rewrite:
RewriteRule ^old_page/(.*)$ /new_page/$1 [L,R=301]
You'll obviously need to tailor the matching expressions and targets to your specific needs. If you have a lot of individual URL's that need redirecting, you may want to look into creating a RewriteMap.
What's about Google in that case how do not lost PR and redirect that PR weight of old pages to main domain?
As long as you use 301 redirects (a permanent redirect, as opposed to 302, a temporary redirect) Google's page ranking will transfer to the new URL.
What's better to use mod_alias or mod_rewrite in this situation and why?
Either is fine, but mod_rewrite gives you a lot more options and allows for rewrite maps. But if you are doing something simple, mod_alias is fine.
Again the same with 301 and 302 what's better to use here?
You want 301 here. It means "the resource that you requested has permanently moved to HERE" as opposed to a 302 which means "the resource that you requested isn't here right now, but in the mean time, you can find it HERE". Also, Google won't transfer any page ranking to the new page if you only do a 302 redirect, since it's meant only for temporary redirects. Not when a page has permanently moved to a new URL.
And the last I just looked on the old pages they all like domain.tld/index.php?id=77 does this rule correct RewriteRule ^index.php?id=(*)$ / [L,R=301] in that case for any id number to root?
This rule will not work. You cannot match against the query string (the ?id= part) in a RewriteRule, only against the %{QUERY_STRING} var in aRewriteCond. Also(*)` is probably not what you want.
This will 301 redirect any request for /index.php?id=N where N is any number, to the document root.
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^index.php$ / [L,R=301]

.htaccess for 301 redirect: which syntax is best?

I am permanently redirecting my website
http://www.oldsite.com
to
http://newsite.com/blog
Is there a difference between using
Redirect 301 / http://newsite.com/blog/
or
RewriteEngine On
RewriteRule ^(.*)$ http://newsite.com/blog/$1 [R=301,L]
Any reason I should use one over the other?
The first uses Apache's internal redirection engine to direct all requests to / to http://newsite.com/blog with a 301 Moved Permanently response code.
The other loads the Apache rewriting engine and rewrites all of the incoming requests that match ^(.*)$ to http://newsite.com/blog/ (appending the matched part of the request URI to the target URI) with a 301 Moved Permanently response code, like the former.
The difference? The former rewrites everything to http://newsite.com/blog/ regardless of the request, and the second takes into account the request URI rewriting it as specified. The first is also somewhat faster than the second because it does not load the rewriting engine, does not introspect the request itself, and (depending on the AllowOverride setting) does not have to look up and load .htaccess files.
I believe the performance difference between the two would be imperceptible to a user.
However, assuming that all of the URLs on the old blog site cleanly map to the new site, then I would recommend using the second method.
If you use the first method, all links to your old blog posts will end up on the home page of your new site, which is not a great experience for users who may have bookmarked links etc.
If you care about SEO, then its the same story, all of your page rank will go from your old blog posts to your new site home page.

mod_rewrite in .htaccess to replace .aspx

I have converted a website from ASP to php...and the urls no longer require an extension. I want to strip off ".aspx" from the end of my incoming strings (from search engines for example) and then 302 redirect them to the correct page without that extension so that the rest of my mod_rewrite rules can then take over. How would I do this?
OLD URL: www.mysite.com/test/page/here.aspx
NEW URL: www.mysite.com/test/page/here/
Thanks!
Place this rule into your .htaccess file in root folder (before catch-all rewrite rule, if you have such):
RewriteRule ^(.+)\.aspx$ http://www.example.com/$1/ [QSA,NC,R=301,L]
This will redirect http://www.example.com/test/page/here.aspx to http://www.example.com/test/page/here/.
Please note, it is better (at least from SEO/browser point of view) to have 301 redirect code (Permanent Redirect) instead of 302 (Found/Temporal Redirect). Unless you may consider changing website back to aspx.

Resources