I have the following code in my htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I avoid editing the automated code by Wordpress because the changes may be erased and I'll have to do it all again. I have another site running the first three lines of code and it's working perfectly to replace example.com for www.example.com. That's all I really need for this example.com site but still preserving the Wordpress properties.
The problem
example.com is not redirecting to www.example.com but instead displaying a 301 error.
I'm using the following lines in all my WordPress-Sites before the # BEGIN WordPress. This should solve your problem. The L (for last) is the solution
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
"I avoid editing the automated code by Wordpress because the changes
may be erased and I'll have to do it all again."
You can avoid a WordPress installation from modifying its root .htaccess file.
Simply connect via FTP, or other means and chmod your WordPress root .htaccess file properties to 0444.
Where is the conflict? I can't see any conflict. WordPress only touches the lines after # BEGIN WordPress
Related
I am running a website with a WordPress installation contained within a /blog/ subdirectory. The Rewrite rules I have in place currently transform correctly for the main site, but not for the Wordpress install.
http://mywebsite.com
is transformed to
https://www.mywebsite.com
without issue.
However, these URLS:
http://mywebsite.com/blog/
http://mywebsite.com/blog/title-of-article/
are not transforming into what I want, which is this:
https://www.mywebsite.com/blog/
https://www.mywebsite.com/blog/title-of-article/
Is this a Wordpress issue, or is my .htaccess wrong?
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
RewriteRule ^(blog/.*)$ https://www.mywebsite.com/blog/$1 [R=301,L]
UPDATE:
The new .htaccess, placed in the /blog/ directory, has made some progress.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.com/blog/$1 [R,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
So mywebsite.com/blog/ is being transformed to https://mywebsite.com/blog/. Great! mywebsite.com/blog/article-title/ is being transformed to https://mywebsite.com/blog/article-title/. The issue is that the www. is not being added. They need to be https://www.mywebsite.com/blog/article-title.
Any ideas as to why the www. is missing?
UPDATE 2:
Turns out I forgot to add the "www." in the default URL in the Wordpress settings (General > Settings > Wordpress URL). With that set to https://www.example.com/blog/ and the above .htaccess in the /blog/ directory, all is now working.
It's better to manage rewrite rules separately by directories.
For blog folder, you should add the rewrite rule in .htaccess within blog folder.
If you want server-wide changes, you shouldn't use .htaccess and edit relevant details within the server configuration file.
/.htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
/blog/.htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.com/blog/$1 [R=301,L]
When adding .htaccess entries in WordPress directory, note that there is a section that will be automatically overwritten by WordPress.
As long as you place your own rules outside of # BEGIN WordPress and # END WordPress, the rules will stay.
Finally, you should also change your WordPress Address and Site Address within the WordPress settings (Settings > General) to use https instead.
Greetings and thank you in advance for your help!
I am trying to redirect certain subfolders from the "category" subfolder, but not all of them. Only about 5 of them.
Example:
http://domain.com/category/fish
http://domain.com/category/lamb
Should redirect to
http://domain.com/tasty/fish
http://domain.com/tasty/lamb
But:
http://domain.com/category/lead-paint
Should not.
What I've tried (in .htaccess):
RewriteRule ^/category/fish http://example.com/tasty/fish [R=301,L]
RewriteRule ^/category/lamb http://example.com/tasty/lamb [R=301,L]
I tried both before and after the Wordpress module. I also tried inserting inside the Wordpress module like so:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/category/fish http://example.com/tasty/fish [R=301,L]
RewriteRule ^/category/lamb http://example.com/tasty/lamb [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Changes to .htaccess definately work. Typos generate a 500 response, and I can block by IP address. I'm not sure if Wordpress is overriding my redirects, or if I have a syntax error.
Thank you again.
You need to remove the leading slash just after ^ in the two rules. You also don't need to specify the full domain. The following will do
RewriteRule ^category/fish/?$ /tasty/fish [R=302,L]
(Note the 302 - always test with a temporary redirect, then change to permanent when you're happy.)
I have a WordPress website that is set to direct to www by default. Everything works perfectly, however I have a few sections that are custom coded, to go to "profile" pages of local services, accommodation etc. this works fine in development however on the live server when you access the link via www.example.com/services/example-here/ it redirects to example.com/wp-content/themes/theme-here/custom/services.php?slug=example-here
I've been told by my webhost that it is WordPRess that is causing the issue, however everything says www.example.com/ (siteurl etc.)
Here is the code in the htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^.*services/(.*)/$ /wp-content/themes/theme-here/custom/services.php?slug=$1 [QSA,L]
RewriteRule ^.*accommodation/(.*)/$ /wp-content/themes/theme-here/custom/accommodation.php?slug=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any ideas I've checked all my settings, Could it be a host issue inside of apache conf to do with mod re-write? I also might add that the code works perfectly without the www's added but my client requires the www.
Add a www forcing rule on top of this .htaccess just below RewriteBase / line:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
Make sure in your permalink settings of WP you have blog address with www
I have a problem redirecting links using htaccess rewrite. Originally I had only the Rewrite Rule part without the WordPress code underneath and it was working fine. Now, after I tried to add a permalink using Wordpress, the link is giving me a redirect loop problem.
Essentially what I want to do is allow the viewers to go straight to 'www.example.com/blog' when they type 'www.example.com'
I think I am missing something because I thought that the rewriteRule would stop the moment
RewriteRule ^$ /blog [R=301,L]
is processed because of the [L] flag and then other RewriteRules would not be looked at. Can someone help me understand how the server actually go through the steps of the code below?
RewriteEngine on
RewriteRule ^$ /blog [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
You need to put the line:
RewriteRule ^$ /blog [R=301,L]
In the htaccess file in your document root, not in the /blog directory.
I'm trying to force www. on all the pages of my site.
I have wordpress install in a folder on my main site (/blog). In the /blog folder there is an .htaccess file with the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
In the main dir of the site. I have a .htaccess file that has the follow:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.sitename.com [NC]
RewriteRule (.*) http://www.sitename.com/$1 [R=301,L]
RewriteRule ^blog/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)$ /blog/index.php [L]
In wordpress is have the option set for www. The www. appears on every page except the when I go straight the the site.com/blog address
Go Settings -> General:
WordPress Address (URL)
Site Address (URL)
And set both of them to www.yoursite.com (instead of yoursite.com), wordpress automagically takes care for setting all the urls to www.yoursite.com and that's it.
If that isn't enough, you shouldn't edit the .htaccess file anyway, because anytime you save or reload your permalink settings, Wordpress will overwrite your .htaccesss (some plugins may do that as well).
If you do want to rewrite your URL, you should do it with WP Rewrite (Codex), or if you want a little more simple approach, there is a good plugin for that called Redirection (Wordpress.org Plugin Directory).
In Redirection plugin you can just set the options to *.mydomain.com/* to redirect to www.mydomain.com/* with a 301, and that will take care of your htaccess as long as you keep the plugin active.
Hope this helps :)
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
In the .htaccess of the WordPress installation (/blog/ in your case), you can add the following code ABOVE the line that reads # BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ "http\:\/\/www\.domain\.com\/subdirectory\/$1" [R=301,L]
</IfModule>
## Redirect primary WordPress URL to WWW version
Obviously, replace "domain.com" with your actual domain name and "subdirectory" with the name of the subdirectory where WordPress is installed. (/blog/ in the OP's request).
Have Option for you:
using wp-config.php add:
define('WP_HOME','http://www.domainname.com');
define('WP_SITEURL','http://www.domainname.com');
using wp-admin
Log into your wp-admin
Browse to ‘Settings’ -> ‘General’
Under ‘WordPress Address (URL)’ and ‘Site Address (URL)’ add www to the address as shown below