.htaccess permanent redirect to www - wordpress

I have a wordpress instalation that is not redirecting urls not starting with www.
Example: http://example.com/dir/ doesn't send to http://www.example.com/dir/ instead goes to http://example.com/
How do I change the .htaccess below to always redirect to the page either using www or not?
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
# END WordPress

You should not have to modify your .htaccess; actually I would not recommend it.
Sounds like you simply have your values for WordPress address and Site address set correctly in your admin console in the Settings > General section:
Another option is to add the following 2 lines somewhere within your /wp-config.php file but before require_once(ABSPATH . 'wp-settings.php'); (and be sure not to use a trailing slash):
define('WP_HOME',"http://www.example.com/dev");
define('WP_SITEURL',"http://www.example.com/dev");
I prefer this approach because I like to keep my configuration options in code whenever possible since it is easier to maintain that way than configuration options stored in the database.
Hope this helps.
-Mike
P.S. Next time you have a WordPress question, consider asking over at StackOverflow's sister site WordPress Answers? Lots of WordPress enthusiasts over the to help out.

I got the correct answer from webmasters.stackexchange.com. This works perfectly! Thanks Ruel for pointing me to webmasters.stackexchange.
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

More like a question for http://webmasters.stackexchange.com
Well anyway, if you are using cPanel, you can do a wildcard redirect from Redirects under Domains category.
But if not, try this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]

Works perfectly. Put this at the top of wp-config.php file:
define('WP_HOME',"http://www.example.com/dev");
define('WP_SITEURL',"http://www.example.com/dev");

Related

Redirect with .htaccess not working correctly when missing / in the end

I've Wordpress site with WPML installed. I've done 2 things recently:
Move HTTP only to HTTPS only
Move http://domain.pl to https://domain.xyz (English)
Move http://domain.pl/pl to https://domain.pl (Polish)
This is done to improve SEO. However while I got that working without much problem using:
# BEGIN HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} ^80$
#RewriteCond %{HTTPS} !^on$
#RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</IfModule>
# END HTTPS
# 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
In apache.conf file ServerName domain.xyz, ServerAlias domain.pl www.domain.pl and so on. I'm having problems with most of my old blog posts that are around the internet in different forms. For example:
This little entry http://domain.pl/email-address-policy/ redirects properly to https://domain.xyz/email-address-policy/
This little entry http://domain.pl/powershell-write-host doesn't. It simply goes to https://domain.pl/powershell-write-host which is 404 and that's it.
It seems that last / is making huge difference.
I've planned to add all redirects in even direct form 1 to 1 to new domain but since it's partially working, partially not I'm kind of lost... and I am not sure how I could translate them correctly.
EDIT:
I've tried multiple options, even one that supposedly rewrites all links to end with / and it still fails.
# BEGIN HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteCond %{SERVER_PORT} ^80$
#RewriteCond %{HTTPS} !^on$
#RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
#RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !^on$
#RewriteRule (.*) https://evotec.xyz/$1 [R=301,L]
#RewriteRule (.*) https://%{SERVER_NAME%}/$1 [R=301,L]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} !(.*)/$
#RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpg|png|jpeg|css|js)$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]
</IfModule>
# END HTTPS
Another update:
https://domain.xyz/url-is-here - works correctly
https://domain.xyz/url-is-here/ - works correctly
https://domain.pl/url-is-here - doesn't work, isn't convered to https://domain.xyz/url-is-here/
https://domain.pl/url-is-here/ - convers correctly to https://domain.xyz/url-is-here/
So it's not entirely not working for all slash vs non-slash. It's more about translation of some sort that doesn't happen if it points to old domain.
I've tried using Redirect Checker to see how it works (on proper example) but I can't make any meaning from it.
Check of http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1
http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1 301
Moved Permanently
https://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1/
301 Moved Permanently
https://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1/
200 OK
Check of http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1/
http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1/ 301
Moved Permanently
https://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1/
200 OK
Check of https://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1
https://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1
404 Not Found
The more options I try the weirder it gets. While the RedirectChecker seems to be showing one thing ... browser is behaving a bit different and doesn't work for the first entry anyways causing 404 straight away
Edit:
If I leave only "Wordpress" data following is true:
http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1
gets 404 http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1 exactly as is
http://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1/ gets https://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1/
https://evotec.pl/hub/scripts/office365-addbulkemailaddressses-ps1 gets 404 exactly as is
It only works properly if i use the correct/new domain then all works properly:
https://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1
https://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1
http://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1
http://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1/
I guess if I can't redirect it, i will leave it as it is and just give up.
I've actually solved it by using SEO Redirection Premium plugin for Wordpress.
It's able to redirect broken links without /. In this case I've done it manually but I'm working on a way to do it in more global way with Regex. Just need to find proper one.
Create a .htaccess file with the below code, it will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain.
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://www.newdomain.com/$1 [R=301,L]
Please REPLACE www.newdomain.com in the above code with your actual domain name.
In addition to the redirect I would suggest that you contact every backlinking site to modify their backlink to point to your new website(For SEO).
Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.
And Then for http to https :
RewriteCond %{HTTP_HOST} ^domain\.com.au$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://www.domain.com.au/$1 [R,L]
And additionally you also can add the below code in wp-config.php
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
Note*: This may not work for you not tried, but you should try once and let me know.

.htaccess 301 redirects in wordpress for select subfolders in a subfolder

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'm unable to change .htaccess file for wordpress multisite

I tried to search this question, but I haven't found the solution. So just want to ask here.
It's very strange that when I manually changed .htaccess file for WordPress multisite as below via SSH on my server, the file will automatically changed back to the default content if I refresh the website in the browser.
I just used vi .htaccess SSH command to modify it, after I modified, I did nothing changed in WordPress admin and I tried to deactivate all the plugin.
The permission of .htaccess is 644.
I don't know the reason. It would be appreciated if you can give me some help.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
After I refresh the homepage, it will changed back to
# 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
Finally I fixed this issue. Just put the solution here for the people who has the same problem.
I've upgraded wordpress to the latest version, but I forget to upgrade the whole network, that's it, so stupid mistake.
WordPress changes .htaccess file itself - if it has write permission and you are using pretty links. I suggest adding your rules before original WordPress rules
... your rules ...
# BEGIN WordPress
... original rules ...
# END WordPress
As far as I remember WordPress does search for that comment lines. Bellow text is taken from WordPress source code:
Inserts an array of strings into a file (.htaccess ), placing it between
BEGIN and END markers. Replaces existing marked info. Retains surrounding
data. Creates file if none exists.

Install Wordpress with Laravel in same domain in folder blog

I have a laravel installation at same domain.com. The site is up and running. I need to install a wordpress in blog folder at domain.com/blog. when I try to install the wordpress, it's not allowing me to run the installation and says "This webpage has a redirect loop" . I installed wordpress by using domain.com/blog/index.php, But after installation I was not able to run the wordpress blog from domain.com/blog/
I have provided relevant permissions to the wordpress blog folder.I will be managing wordpress from the blog admin and laravel site from laravel section.
I have seen https://laracasts.com/discuss/channels/general-discussion/install-wordpress-in-domaincomblog but could not make it work.
Mu working environment is : Xampp in Ubuntu
Any suggestions will be helpful.
I got a solution from laravel forum. Adding the line in the htaccess worked for me.
RewriteCond $1 !^(blog)
Blog is working properly as a separate folder.
this line in .htaccess is probably your culprit:
RewriteRule .*/$ /$1 [L,R=301]
comment it out and see it that solves your problem.
This line forces everything in the public domain to go through laravel's router. You could probably leave it by writing another regex above that line looking specifically for the /blog directory, or rewrite that line to route anything that != your blog directory.
You should really leave that line there if possible.
Add below lines in .htaccess
RewriteCond $1 !^(bmcblog)
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
It will look like this
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond $1 !^(bmcblog)
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

Apache RewriteRule Regex issue - rewriting url to point to external site

I have posted a similar question earlier that got answered and I believed it to solve this issue, but sadly it didn't.
I own two websites, where I am using one as a multisite WordPress site and the other as a site to redirect traffic from.
Updated .htaccess for secondary site:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?secondary.com$ [NC]
RewriteRule ^(.*)$ http://primary.com/secondary/$1 [P]
And this .htaccess on my primary site:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.(php|js|css|woff|ttf|svg))$ $2 [L]
RewriteRule . index.php [L]
# END WordPress
AddType x-httpd-php54 .php
At first glance, it seems to be working fine, but there are two main issues:
All links on the site seem to be pointing towards http://secondary.com/secondary/article-title instead of http://secondary.com/article-title and when I click on one of them I get redirected to http://primary.com/secondary/article-title. I think there's something wrong with my RewriteRules, but I am uncertain of where - anyone have a clue?
The rewrite doesn't seem to work in IE11 on Windows 8 or Chrome on a mobile device - it redirects instead. How could this be? And how can I fix it?
I'm fairly sure this has an easy solution to someone who knows what he/she is doing, but personally I am rather clueless when it comes to Apache, so any help with it would be greatly appreciated.

Resources