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
Related
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).
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.
I would like to redirection from the whole old subdirectory to new location in the same website with htaccess file. (I run wordpress website with redirection plugins but it take to much resources by adding more records.)
For example, if I want to do the Redirect 301 process from
www.mydomain.com/olddirectory/page%
to
www.mydomain.com/newdirectory/specific-url/
I have tried to search the example of using regular expression but I can't find them. Please suggest me about this matter.
We have a subdomain (oldblog.oursite.com) and we've set up over 600 redirects to (newblog.oursite.com), however, there are likely some live links to pages on oldblog.oursite.com that we haven't caught and implemented a redirect.
Is there a way to make oldblog.oursite.com redirect any URL that doesn't already have a redirect rule in place to redirect to the index of newblog.oursite.com? Preferably in the .htaccess file?
This should work if you put it last:
RedirectMatch 301 "^/" http://newblog.oursite.com/
Please test it. Any problems let me know. Take care that you are testing server generated redirects and not ones cached in your browser. Using 302 redirects during testing can help.
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.