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

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.

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.

non-www to www redirect not working for articles in subdirectory installation of WordPress

I am helping out with a website that has a main/root website as well as a WordPress installation in a sub-directory. The .htaccess file of the main site has rules to redirect non-www traffic to www:
RewriteCond %{HTTP_HOST} ^site\.com [NC]
RewriteRule (.*) http://www\.site\.com/$1 [R=301,L]
However, this has no effect on the WP site. For instance:
site.com would redirect to www.site.com
site.com/some-content-belonging-to-root-site would redirect to www.site.com/some-content...
site.com/wordpress does not redirect to www.site.com/wordpress
I added similar rules to the WordPress .htaccess, playing around to try to get the result I would expect, but the best I could come up with is this:
RewriteCond %{HTTP_HOST} ^site\.com [NC]
RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]
...which successfully redirects site.com/wordpress to www.site.com/wordpress, but does not redirect site.com/wordpress/a-blog-post.
I feel like I must be missing something simple or obvious, but hours of experimentation and research have not yielded a thing except that .htaccess files in sub-directories are not advised (but I think this is unavoidable in this case).
I would be grateful for any suggestions on how to fix the rewrite condition or rule to successfully redirect the post links of the WP site.
Thanks!
EDIT / ADDITIONAL INFO: I modified the RewriteCond to better match the incoming URLs, thinking that this might help, but it has the same effect. The base wordpress URL is rewritten, but article URLs are not:
RewriteCond %{HTTP_HOST}/wordpress/.* ^site\.com/wordpress/.* [NC]
RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]
I wouldn't think that it should matter, but just in case, I'll mention that the article URLs look like this:
site.com/wordpress/2014/05/16/article-slug
It turned out that the problem was not the rewrite conditions or rules, but the order in which they appeared in the .htaccess file.
I read a WP forum post in which someone suggested moving the rewrites to the top of the file for performance reasons. I gave it a shot just to see what would happen and noticed that the URL for the articles was affected - not in a good way, but affected nonetheless. I got something like this:
www.site.com/wordpressindex.php?/2014/05/16/article-slug
There were some rewrite rules to rewrite the base and redirect to the index given a nonexistent directory or file, which was what was building the garbled URL above.
In case it happens to help anyone else, I began with a .htaccess file that looked like this:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
And ended up rearranging it to look like this, which now works to properly redirect all blog-related URLs to the www site:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{HTTP_HOST}/wordpress ^site\.com/wordpress [NC]
RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
Try:
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]
And clean your browser history to make sure.
I also own a WordPress website and I was looking for www to non www redirect for my website : fasterthemes.com
The website is hosted in DigitalOcean and after struggling with several DAYS, I could figure out that all the solutions (.htaccess) codes available work fine if you've set the A record in your DNS settings properly. If it's not set then the redirection doesn't work !
So in my case,
I created one A record in my Digitalocean DNS like below :
i.stack.imgur.com/ie7f8.png
I added following code :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.)$ [NC]
RewriteRule ^(.)$ http://%1/$1 [R=301,L]
And now as you can see if you browse http://www.fasterthemes.com , it redirects you to fasterthemes.com. Earlier it was throwing an error.
I hope this helps.

.htaccess redirect not working in WordPress 3.5.1 on Apache

I'm trying to fix a canonical issue with WordPress 3.5.1
http://www.whatisanaddiction.com/index.php
needs to redirect to
http://www.whatisanaddiction.com/
so I don't end up with a duplicate content issue with search engines.
I don't know the code that needs to be used in .htaccess very well but I never had a problem before WordPress' code inserted in the .htaccess.
I've tried all sorts of variations and many cause the whole website to "Server Error".
Here's what I've got so far but it still doesn't redirect the index.php verions correctly.
# 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]
RewriteCond %{THE_REQUEST} ^.*\/index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
# always www
RewriteCond %{HTTP_HOST} !^www\.whatisanaddiction\.com$ [NC]
RewriteRule ^(.*)$ http://www.whatisanaddiction.com/$1 [L,R=301]
</IfModule>
# END WordPress
Redirect 301 http://www.whatisanaddiction.com/index.php http://www.whatisanaddiction.com/
... I see all sorts of people asking this question online but no "working" solution - so far. Maybe I'm not searching for the right terms???
Any help would be greatly appreciated!
Ok for some strange reason the sample I used above just started working... weird, maybe the server needed some time to "think about it." ??? weird.
Your arguments for your 301 redirect are not correct. The page you are redirecting from, should not have the domain name on it. Try this instead.
Redirect 301 /index.php http://www.whatisanaddiction.com/

.htaccess file problems

So I have a Joomla instance which is sitting in my ROOT directory. I have a Wordpress Multi-site installation sitting in the subdirectory /blog.
It seems like the server is having a hard time finding or getting sites in the /blog directory. Seems like it gets to the main /blog page fine but if I want to go to /blog/{sitename}, sitename being an instance of the Wordpress Mulitsite, it has a hard time getting there. Probably complicating matters is that I have Fancy URLs on both the Joomla instance as well as the Wordpress Multisite instance.
Both .htaccess files for Joomla and Wordpress Multisite are from stock installs of their respective applications (I'm 99% sure).
I'm going to post them here and see if anyone can see possible conflicts in the .htaccess
Joomla .htaccess with core SEF
version $Id: htaccess.txt 21064 2011-04-03 22:12:19Z dextercowley $
RewriteEngine On
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?|feed|pdf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
END Joomla .htaccess
Now I have added this to the Joomla .htaccess file in hopes that it would not use SEF on the /blog folder but it doesn't seem to work. I am trying to exclude the blog directory from the top level .htaccess and just let the /blog .htaccess take over control of that subdirectory.
#RewriteCond %{REQUEST_URI} ^/blog/.*$
#RewriteRule ^.*$ - [L]
And here is my Wordpress .htaccess
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
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]
Everything on the top level domain (Joomla) runs very fast when pages are requested.
The server eventually finds the /blog directory but it takes more time than it should. The really big problem is that when i go to one of the blog sites (/blog/sitename) it can take upwards of 8-10 seconds. And then once the page loads and I go to another page on that /blog/sitename like "About" it can take about the same amount of time.
Overall very frustrating. Any help is appreciated. I've been googling for awhile and have not found much help besides
#RewriteCond %{REQUEST_URI} ^/blog/.*$
#RewriteRule ^.*$ - [L]
And it has not helped the problem.
It seems like all the sites in the /blog directory are very slow to get to. I have about 6 in there and they are all very slow loading.
Well, I'm not sure if you're developing locally but one thing you could try would be to place all the Joomla files in their own directory and use .htaccess to resolve the site correctly - then you shouldn't have the Joomla .htaccess affecting your /blog directory.
Just a consideration. I'm not sure that it would resolve the issue entirely but at least then you could separate out the issue of 'is the Joomla .htaccess conflicting with the Wordpress .htacceess and causing a slowdown'.

.htaccess permanent redirect to www

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");

Resources