Rewriting from a SEO friendly URL to another SEO friendly URL on the same site - apache

For our SEO needs in my company, we need to change some SEO URLs to another ones through 301.
Example:
/seo/keywords-1-2-3/ to /seo/mynew301page-1-2-3/
Is there some Apache rewrite rule that I can specify that certain URLs should be redirected to the new one?

As simple as this:
Redirect 301 /seo/keywords-1-2-3/ /seo/mynew301page-1-2-3/
The above will redirect ALL requests for URLs that start with /seo/keywords-1-2-3/ to /seo/mynew301page-1-2-3/. For example: /seo/keywords-1-2-3/something?say=hello ==> /seo/mynew301page-1-2-3/something?say=hello
If it has to be precise match (only /seo/keywords-1-2-3/ and not /seo/keywords-1-2-3/something, then use this one:
RedirectMatch 301 ^/seo/keywords-1-2-3/$ /seo/mynew301page-1-2-3/
The same, but using mod_rewrite:
RewriteEngine On
# broad (base) match
RewriteRule ^/seo/keywords-1-2-3/(.*)$ /seo/mynew301page-1-2-3/$1 [R=301,L]
# exact match
RewriteRule ^/seo/keywords-1-2-3/$ /seo/mynew301page-1-2-3/ [R=301,L]

You can do it n two ways:
.htaccess
httpd.conf
Mostly the .htaccess configuration method is used.
So you can use this code in your .htaccess file:
RewriteEngine On
RewriteRule ^/seo/keywords-1-2-3/ /seo/mynew301page-1-2-3/ [R=301,L]

Related

.htaccess 301 redirect whole URL including Domain

I need to redirect around 300 URLs on a multidomain site that has the same URL structure on the different domains. For example:
https://www.example.com/de/products.html needs to be redirected to https://www.example.org/de/products.html
So my usual approach does not work:
RedirectMatch 301 /de/products.html$ /de/products.html
I would need something like
RedirectMatch 301 https://www.example.com/de/products.html$ https://www.example.org/de/products.html
which obviously doesn't work or I just didn't get to work.
Not sure if important, but it's a TYPO3 instance.
The mod_alias RedirectMatch directive matches against the URL-path only. To match the hostname you'll need to use mod_rewrite with an additional condition (RewriteCond directive) that checks against the HTTP_HOST server variable (the value of the Host HTTP request header).
Also, since the URL structure is the same on both domains then you only need a single rule - just use the same URL-path from the initial request. No need to do one-by-one redirects as you seem to be trying to do.
For example, the following would need to go at the top of the .htaccess file before any existing rewrites:
RewriteEngine On
# Redirect everything from example.com to example.org and preserve the URL-path
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^ https://www.example.org%{REQUEST_URI} [R=301,L]
This checks for both example.com and www.example.com.
The REQUEST_URI server variable already contains a slash prefix, hence it is omitted in the substitution string.
Test first with a 302 (temporary) redirect to avoid potential caching issues.
UPDATE:
But I don't want to redirect all URLs, just some.
To redirect a specific URL to the same URL at the target domain, as per your original example:
# Redirect "/de/product.html" only
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^de/products\.html$ https://www.example.org/$0 [R=301,L]
The above redirects https://www.example.com/de/products.html only to https://www.example.org/de/products.html.
The $0 backreference contains the entire URL-path that the RewriteRule pattern matches against.
How to extend your snippet with /de/ or /fr/ etc.? For example I want to redirect example.com/de/products.html but not example.com/products.html
Maybe the above example is what you require. Alternatively, to redirect /de/<something> (or /fr/<something>) only and not just /<something>, you could do something like this:
# Redirect "/<lang>/<something>" only, where <lang> is "de" or "fr"
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^(de|fr)/[^/]+$ https://www.example.org/$0 [R=301,L]
The above will redirect https://example.com/de/<something> to https://www.example.org/de/<something>.

htaccess 301 redirect not redirecting

tried several ways to make redirect, but not successfull
Redirect 301 /ru/pages/portfolio/ /ru/pages/portfolio/3/ [END,R=301]
or
Redirect 301 http://myssite/en/pages/portfolio http://myssite/en/pages/portfolio/3
and many others from internet, but all of them not working.
Need to redirect pages/portfolio to pages/portfolio/3 (for all languages - en, ru)
This is content of file
<IfModule mod_rewrite.c>
# Turn Off mod_dir Redirect For Existing Directories
DirectorySlash Off
# Rewrite For Public Folder
RewriteEngine on
Redirect 301 /ru/pages/portfolio/ /ru/pages/portfolio/3/ [END,R=301]
RewriteCond $1 !^(pma)
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Since you are already using mod_rewrite (for an internal rewrite) you should also use mod_rewrite for this redirect, rather than using a mod_alias Redirect. Different modules execute at different times during the request, despite the apparent order of the directives in the config file.
However, your example is unclear. The first example includes a trailing slash; the second does not? Is there a trailing slash or not?
Try something like the following instead after the RewriteEngine directive:
RewriteRule ^(en|ru)/pages/portfolio$ /$1/pages/portfolio/3 [R,L]
This excludes the trailing slash. And assumes "all languages" are just en and ru. This is also a temporary (302) redirect. Change to a permanent (301) redirect (if that is the intention) only when you are sure it's working OK, since 301s are cached by the browser. You will need to clear your browser cache before testing.
Redirect 301 /ru/pages/portfolio/ /ru/pages/portfolio/3/ [END,R=301]
:
Redirect 301 http://myssite/en/pages/portfolio http://myssite/en/pages/portfolio/3
Aside: Neither of these would have worked anyway. End flags like [END,R=301] are a mod_rewrite syntax, and do not relate to mod_alias (Redirect). And the URL-path matched by the Redirect directive should be a root-relative path beginning with a slash, not an absolute URL. See the Apache docs... https://httpd.apache.org/docs/current/mod/mod_alias.html#redirect

How to rewrite url and redirect with apache mod rewrite?

I have got url:
ipaddress/panelname/main/index.php
How to rebuild it to
ipaddress/center/index.php
?
ofcourse we can see another pages, not only index.php, but this folders in url we can see forever.
I tryed to do this in .htaccess:
RewriteEngine on
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]
RewriteRule ^/panelname(.*)$ /center$1 [QSA,L,R=301,NC]
Redirect 301 ^/panelname(.*)$ /center$1
but i don't see redirect from panelname to center.
but if i type center all works good (but i don't shure, that it works good by my htaccess or by symlink, which i was created in filesystem)
How to rewrite all to another links and howto see redirect from old links to my new? Thank you.
RewriteRule in directory context (which .htaccess is), does never begin with a slash, because the common prefix is stripped from the matched portion first.
Redirect does match strings, not regex'es. The variant that works on a regex is RedirectMatch. Both only work on absolute URL's (the one beginning with a slash).
You either have to do the following:
RewriteRule ^panelname(.*)$ /center$1 [R,L]
or:
RedirectMatch 302 ^/panelname(.*)$ /center$1
Change [R] to [R=301] once you have tested that EVERYTHING works. If you choose the second option, only change 302 to 301 after testing that everything works.
If you want to show /center/index.php to your visitors and keep a redirect from old URL to this URL then you will need one redirect and one rewrite rule (that you already have).
RewriteEngine on
# external redirect from old URL to new one
RewriteCond %{THE_REQUEST} /panelname/main/(\S+) [NC]
RewriteRule ^ /center/%1 [R=302,L]
# internal forward from new URL to actual one
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]

Website article .htaccess 301 redirect

I have a site in a folder called patients and my urls look like so:
http://site.com/patients/post-name
http://site.com/patients/articles/another-post-name
I want to redirect them all to a clean:
http://newsite.com/post-name
http://newsite.com/another-post-name
In other words, lose the /articles/ which sometimes appears and 301 to the new site.
Any help on how to do this with htaccess?
Edit your .htaccess in http://site.com server and put this(must be the first rule):
RewriteEngine On
RewriteRule .* http://newsite.com/ [R=301]
or
Redirect 301 / http://newsite.com/
Regards.

url rewrites dynamic urls redirects - Magento

I'm looking for htaccess lines that do the following
Redirect old existing urls to new url (301)
Example
www.example.com/categorya/categoryb/product.htm
TO
www.example.com/product.htm
There can be more category parts, or less, it all has to go to /product.htm (Magento).
Who can help?
Try putting these rules in the htaccess file in your document root, preferably above any rules that you may already have there:
RewriteEngine On
RewriteRule ^(.+)/product.htm$ /product.htm [L,R=301]
Try:
RewriteEngine On
RewriteRule ^[^/]+/[^/]+/([^.]+)\.htm$ /$1.htm [L,R=301]