Redirect multipe links using htaccess - wordpress

I have shifted my website from one place to another. I have high traffic in the first place, so I need to redirect it on specific pages mapping to the second place. There are almost one thousand articles, so I need one thousand redirections. How to do that through .htaccess?
I am using the described below .htaccess code for this purpose but it is not working:
Redirect https://websiteOLD.example/article1 https://websiteNEW.example/article1
Redirect https://websiteOLD.example/article2 https://websiteNEW.example/article2
Redirect https://websiteOLD.example/article3 https://websiteNEW.example/article3
Redirect https://websiteOLD.example/article4 https://websiteNEW.example/article4
Redirect https://websiteOLD.example/article5 https://websiteNEW.example/article5
Redirect https://websiteOLD.example/article6 https://websiteNEW.example/article6
Whats wrong with the above described code lines? I have almost one thousand of such lines mapping each Wordpress article from old domain to the new domain.

I copied the website from one place and pasted it to other place.
If you've simply migrated your site from one domain to another then you just need a single mod_rewrite rule at the top of the .htaccess file to 301 redirect all URLs from the old domain to the new.
For example, the following would need to go before the # BEGIN WordPress comment marker.
RewriteEngine On
# Redirect any request that is NOT for example.com to example.com
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
Where example.com is the "new" canonical hostname.
The ! prefix on the above CondPattern negates the expression, so it is successful when the requested hostname does not match.
You do not need to repeat the RewriteEngine directive if this already occurs later in the file - it should already be part of the WordPress code block.
Test first with a 302 (temporary) redirect to avoid potential caching issues and make sure you've cleared your browser cache before testing.
You need to use mod_rewrite (ie. RewriteRule) as opposed to a mod_alias Redirect (or RedirectMatch) directive because:
The Redirect (and RedirectMatch) directives do not match the requested hostname. They match the URL-path only. (Which is why your attempt is not doing anything.)
As a general rule, if you are already using mod_rewrite in other parts of the config file (ie. in the WordPress code block) then you should be using mod_rewrite throughout in order to avoid potential conflicts.
Whats wrong with the above described code lines? I have almost one thousand of such lines mapping each Wordpress article from old domain to the new domain.
As mentioned above, the Redirect directive matches against the URL-path only. So, these directives will never match. See the Apache docs.
The directive you are trying to use is also a 302 (temporary) redirect. This needs to be a 301 (permanent) redirect in order to preserve SEO. (But, as mentioned, it is a good idea to test with a 302 first.)
Aside: However, you should avoid using 1000+ individual directives/redirects in the main .htaccess file (when there is no discernible pattern from the old to new URLs). The issue is that these directives are processed (unnecessarily) on every single request. You need to prioritise your normal site visitors, not the "old" redirect. Ideally, you would perform such redirects in your application, once it has been determined that these URLs don't exist in the current site (a WordPress plugin is suitable for this). Although, there are techniques you can use in .htaccess to prevent all these directives being processed on every request (ie. only processed when the old domain is requested, otherwise they are all skipped).

Related

Redirecting specific urls to subfolder on same domain

I have moved my phpBB board from dommain.com to domain.com/forum.
I have launched a Wordpress website on domain.com.
As a consequence, all links to topics on my board end up on a 404 page of my Wordpress website on domain.com
I would like to redirect links to topics on my board.
For example:
Redirect: example.com/viewtopic.php?f=17&t=35
To: example.com/forum/viewtopic.php?f=17&t=35
So only /forum should be added.
Is changing the .htaccess file of my WordPress website the best way to do this? And if so, what would be the correct code?
Of are there better ways (faster/better for SEO) that I don't know of.
Is changing the htaccess file of my Wordpress website the best way to do this?
Implementing this in the root .htaccess file is a reasonable way to do this, unless you have access to the main server config. Alternatively, you could perform this redirect in PHP (in WordPress), but that puts the burden on your WordPress site.
For example, at the top of your root .htaccess file, before the existing WordPress directives (ie. before # BEGIN WordPress) try the following:
# Redirect old forum topics to "/forum" subdirectory
RewriteRule ^viewtopic\.php$ /forum/$0 [R=301,L]
Any query string is passed through by default. So, a request for /viewtopic.php?<something> is redirected to /forum/viewtopic.php?<something>.
The $0 backreference contains the URL-path matched by the entire RewriteRule pattern. ie. "viewtopic.php" (this simply saves repetition).
NB: You should test first with a 302 (temporary) redirect to avoid potential caching issues.

Why is this 301 Redirecting?

I have a WordPress site that I'm trying to speed up with the help of GTMetrix. I noticed whenever I test a page and omit the trailing slash to the URL (e.g. http://example.com/blog), it does a 301 redirect to that URL with the trailing slash appended (e.g. http://example.com/blog/).
The redirect is taking about 1 second and I would like to shave that time off, if possible. Questions:
What's causing this redirect? Is it a configuration in WP, .htaccess, or something else?
What can I do to prevent this redirect and not break my site?
That redirect is done by Apache using mod_dir. It does this by default to directories using the DirectorySlash directive. If you wish for it not to do that you can turn it off using this in .htaccess.
DirectorySlash Off
But becareful as this can have side affects (such as DirectoryIndex not working) which you can see about here.
https://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryslash

domain redirect to a page and rewrite url of landing to keep orgin domain

this it might be easy but I'm not sure how to achieve it (nor if it's actually possible..)
I've made a page in wordpress, with a custom template design, and not linked with the rest of the website, so it's a landing page which actually has nothing to spare with the base website.
I've bought a domain and redirected with a 301 to this page (to pass seo from that page as well).
the problem is that now I'd like to have the url of that page rewritten in order to be the same of the one I've bought, so when landing in that page, viewers won't notice that they are in another domain.
basically I want that a domain like this
my-new-domain.com
which redirects to this page (I've done this)
maindomain.com/landing-page/
and once you're there, the url displayed is:
my-new-domain.com
the page is wordpress, so I need it to keep passing the variables while the url is rewritten.
but I don't need it to handle sub-pages, as the page is a single landing page.
can I do this with an htaccess rule? and if yes, could you please tell me how this rule should ne written?
Thanx to anyone who'll help!
Andrea
You could use mod_rewrite for this
RewriteCond %{HTTP_HOST} my-new-domein.com [NC]
RewriteRule ^ /landing-page [L]
The first rule is the condition, so if the domein (http host) matches your domein the second line get executed, [NC] means no case, so my-new-domain.com and MY-NEW-DOMAIN.com both work
The second rule has 3 parts
^
as another condition (always matches)
/landing-page
that's the page which is passed to wordpress
[L]
means this is the last rule to be executed, handy when multiple rewrite rules at in the .htacess
Edit:
In addition, if you want mydomain.com/landing-page to show it's content but with my-new-domain.com in the url bar use the following to redirect to my-new-domain.com
Redirect 302 /landing-page http://my-new-domain.com

Using HTTPS instead of HTTP

I have a website that user have to loggin to. I want to use HTTPS so make a secure encrypted connection to the server to check the user name and password.
Currently on my server i have a folder called httpdocs, this is where my website is store, i also have a folder called httpsdocs.
Now as i stated my website is stored in httpdocs so logically i would go to http://website.com
But i found that if i go to https://website.com i get a secure connect to the pages stored in httpdocs.
Anyway, my question is, whenever i go to another link i loose the secure connect (URL goes back to http://) So how do i keep it all secure? Would i have to use the full URL in the href because that seems a bit lame instead of just using href=page2.php .
I'm new this area of website developing, but i am experienced in developing if that helps you.
Thanks a bunch for the help.
Some basic tutorials or reading material would be awesome if anyone knows of anything good?
Thanks again.
To answer your question in the comments you should use mod_rewrite (assuming its enabled):
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
that will send any webpage to https.
Do not use http://foo.com/path in urls, use //foo.com/path instead (or, even better, if you stay on the same server, use /path - it surely works). Doing full urls is bringing redundancy and brittleness. Avoid it.
Check out HTTP Strict Transport Security (HSTS):
https://www.owasp.org/index.php/HTTP_Strict_Transport_Security
Basically, you can force the browser (unless it's IE<12) to always use HTTPS by adding this header to the secure site's configuration:
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains"
This can also be combined with a 301 (PERMANENT) redirect for the non-secure/HTTP site's configuration, also outlined on the above referenced site.
Additionally/Alternatively, you can set the base href on your "website.com" pages by adding something like this towards the top of the <head> tag:
<base href="https://website.com/">
This will set all relative urls on the page (including links, stylesheets, scripts, images, etc.) to use the HTTPS site's root as their base. Depending upon the structure and size of your site, however, you will want to verify for each page that its base href and/or the urls within the page include any necessary path information (i.e. subdirectories). Otherwise a link on http://website.com/some/directory/page.html that points to "otherpage.html" will direct to http://website.com/otherpage.html instead of http://website.com/some/directory/otherpage.html . In such cases you would want either a base href of "http://website.com/some/directory/" or the relative url in the link should point to either "/some/directory/page.html" or "some/directory/page.html" (no slash needed in this case).

Redirect non-trailing slash to trailing slash on homepage URL only

I have a two-part issue.
I am trying to implement a redirect only for the homepage URL to render with a trailing slash. Example: redirect www.domain.com to www.domain.com/ BUT allow all other URLs to render without a trailing slash.
End Goal:
www.domain.com/
www.domain.com/pages
www.domain.com/pages/post-name
This get complicated because there is one WordPress install with one .htaccess file but 6 other sites (with different domains) installed under the main WP install. I need to implement the trailing slash only on the homepage URL for a site that is mapped under the main install and shares the same .htaccess file with the main instal domain. So a rewrite base will only affect the main install domain, not the other 6 sites.
This is the first time I have encountered this kind of structure and it has me totally confused. Is this even possible to achieve? Thanks for any help!
There really is no need to force a trailing slash when there is no path, because if the path is empty, it is implied to be / already. Having the slash there or not will make absolutely no difference in the rendering of your pages or how Apache handles the request. Not to mention, redirecting users to have the / appended will just cause them to waste an extra HTTP request.
However, you can prevent Apache from adding the / to URLs which resemble a directory by using this:
DirectorySlash Off
Which will prevent /pages from becoming /pages/. Although, if a user manually types it in with the / at the end, it will still be there. Again, redirecting just to remove a / at the end is a waste.

Resources