Wordpress Mod Rewrite Same Server - wordpress

I've been researching this for got to be around 6 hours now (had I known I'd spend this long I would have read the manual from start to finish to learn things in-depth) and out of desperation I'm finally asking.
I have a client with two Wordpress sites.
www.myoldsite.com and www.mynewsite.com.
www.myoldsite.com is hosted on a server I can't access while I have access to www.mynewsite.com.
Because of this restraint I've decided to use a .htaccess file on www.mynewsite.com to handle specific 301 redirects and point the domain www.myoldsite.com to www.mynewsite. Most of the redirects are working without any issues but there are a few that aren't. I know why but I can't figure out how to fix it.
For example:
On the www.myoldsite.com they had the page www.myoldsite.com/about-us.
On the new site the new URL is www.mynewsite.com/about-us/what-we-do.
The htaccess file looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mynewsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mynewsite\.com$
RewriteRule /about-us "http\:\/\/mynewsite\.com\/about\-us\/what\-we\-do\/" [R=301,L]
# 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
At the moment this causes the recursive error "too many redirects". If I change the Rewrite Rule to:
RewriteRule ^/about-us$ "http\:\/\/mynewsite\.com\/about\-us\/what\-we\-do\/" [R=301,L]
The URL www.myoldsite.com/about-us returns a 404 /index.php not found. This I can tell is coming from Wordpress.
All I need www.myoldsite.com/about-us to point to www.mynewsite.com/about-us/what-we-do using a redirect. If anyone can assist that would be most helpful.
Thanks.

Your RewriteRule is awfully long. I just successfully did the following with no fuss:
RewriteRule ^about-us(/?)$ https://www.google.com/about/ [R=301,L]
I think the forward-slash at the beginning of about-us in your rule is causing a problem.
Also, I'm not 100% sure whether or not browsers cache these redirects, but I've had some trouble in the past with Firefox and 301 redirects where after a change Firefox's behavior continued as before the change and opening a new browser (Safari) worked. You might check into that.

Related

htaccess script for Wordpress to serve selected page as https

I have two pages on my Wordpress site that need to be served as https; the rest is vanilla http. Pushing the whole site to https would involve a lot of database editing to avoid mixed content, so I'm looking to write an htaccess script to use a RewriteRule that will make the change just for those two pages.
The browser addresses for the two pages are like
http://example.com/draw/membership-login/ and
http://example.com/draw/membership-join/membership-registration/
Note they both contain the word 'membership' and no other page does (or so I believe), so I thought I'd write a rule to look for any REQUEST_URI containing 'membership'. I did this and put it before Wordpress's own script in .htaccess (.htaccess is in directory draw, the home directory for the Wordpress installation). It looks like this (only the first 4 lines are mine; the rest is WordPress's, except I've commented out their RewriteEngine On).
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} membership
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=302, L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
#RewriteEngine On
RewriteBase /draw/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /draw/index.php [L]
</IfModule>
# END WordPress
Result? All pages serve ok except the two membership pages, which serve as 404.
I guess it's good news in a sense, as the RewriteCond must be finding the membership pages, but the RewriteRule is clearly not delivering what I had hoped.
Any help much appreciated.
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} membership(.+)
RewriteRule ^membership(.+) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The (.+) will match any character one or more times. A 301 redirect means that the page has permanently moved to a new location. A 302 redirect means that the move is only temporary.

Specific Wordpress page SSL exception through htaccess

I know this is not the first time someone has asked this, but I tried other solutions and they don't seem to work.
So I have a website with a SSL certificate and a redirect rule in my .htaccess file to point visitors to https. However, there is one specific page I want to exclude from SSL.
This is the code in my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
So my question is, how do I create a rule that excludes www.example.com/shop from SSL?
Most likely you can use another RewriteCond on the redirect to block redirecting for that particular URL. However, how are you going to handle the resources loaded by that page? It will load stuff like images, CSS, Javascript, etc. Should those be loaded over plain old HTTP too? And what happens when something clicks on a like to view their shopping cart, will it go to another URL (and thus go over HTTPS again)?
I'm not entirely sure what you want to achieve, but it sounds a bit silly to me. Why would you want to exclude only one particular page from a safe & secure connection? (And why wouldn't you serve the whole site over TLS?) But maybe you tell us the reason for this so maybe we can help you find a different solution?

.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/

Using additional Re-Write Rules in WordPress .htaccess to Stop DDOS attacks

We have suddenly been the victim of what we are being told is a DDOS attack. We are trying to use some .htaccess rules to block the affending parameters, using the following, to show a 403 forbidden page to any posts requests made on the home page and any requests with specific fields in the beginning of the query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(ptrxcz|xclzve).* [NC]
RewriteRule ^(.*)$ http://%{REMOTE_ADDR} [F,L]
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*)$ http://%{REMOTE_ADDR} [F,L]
Basically this works fine whenever I use wget to access the file with the offending query string in SSH but not in the browser window.
In addition, the Postmaster Google Chrome add on blocks the post requests to the homepage however a curl -d request doesn't get blocked so I'm finding it hard on which tools to trust as they are giving varying results (browser, SSH, Postmaster).
I am also using Wordpress with permalinks and I wonder if these affect this in some way as these are the only other things in the .htaccess file. Once I remove these from the .htaccess file I then everything works as expected. This is the Wordpress .htaccess which comes after my current rules.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
In addition, if I change my pre-Wordpress rules and remove the [F] and REMOTE_ADDR parts and replace it with the following to redirect it to a URL that doesn't exist this redirects properly, which means it could be related to the [F] and REMOTE_ADDR sections.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(ptrxcz|xclzve).* [NC]
RewriteRule ^(.*)$ http://www.pleasegoawayandnevercomebackagain.com [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.pleasegoawayandnevercomebackagain.com [R=301,L]
In addition to the above and on further investigation. The following rule DOES work:
RewriteEngine On
RewriteCond %{QUERY_STRING} !.*test.* [NC]
RewriteRule ^(.*)$ http://%{REMOTE_ADDR} [F]
The difference here is I'm checking for a negative match on the query string value. This means every page is forbidden except for those with test in the query string. Do you have any idea why a negative match works but a positive search does not? Is it a syntax issue or something to do with searching positive values in htaccess Rewrite Conditions?
Some ideas that may help you:
Make sure you maintain # BEGIN WordPress and # END WordPress in your .htaccess file. place the rest on your rules above it
try installing the plugin: Better WP Security: http://wordpress.org/extend/plugins/better-wp-security/ - it's a life saver :)
If you replace QUERY_STRING in the code above with THE_REQUEST then this works fine and does exactly as required within WordPress.

.htacess custom redirect rules conflicting with Wordpress redirect rules

I am in the process of moving my current static site to Wordpress, using the same domain name.
Both old and new sites have their content in the root folder and also have identical file names, but on the new (Wordpress) site, I have removed the .php extension that exists on the current site and added a trailing slash, as per Wordpress permalinks.
So I need to redirect all the old pages (for example):
/contact-us.php -> /contact-us/
I have tried the well-documented options such as (and variations on this):
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /$1 [R=301,L]
But this seems to be conflicting with the Wordpress .htaccess rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
When I try to access www.site.com/contact-us.php, I am getting either an infinite loop OR a redirect to www.site.com/index (no .php, no trailing slash), which Wordpress displays as a 404 page.
I have tried with and without the first set of Rewrite conditions (as I see they are duplicated in the Wordpress rules), and also placed my rule before and after the Wordpress rules. No joy. I've also been Googling for the last few hours but no one seems to have addressed this specific problem. I do usually find what I am looking for by searching, so it's in desperation that I'm actually posting (and which is why it's my first post!)
If anyone can help out, I would be very grateful.
It appears you will run into loops when trying an .htaccess redirect because of the php suffix. Maybe someone else knows a solution.
But try a plugin called Redirection « WordPress Plugins. Out of curiosity, I tried a redirect from contact-us.php to contact-us/ and it worked fine. The plugin redirects via php rather than writing to .htaccess, and that may be the difference.
I use the plugin all the time to mostly log 404's and do a few redirects. It doesn't take too much site overhead.
Be sure and set Options to not do "URL Monitoring," as that will set up automatic redirects to to posts/pages that have their permalinks manually changed. And set your 404 logging to a day or two, because the logs can quickly get big and result in huge database tables.
Try using this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Rewrite /something.php to /something/
RewriteRule (.*)/(.*)\.php$ $1/$2/ [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This method would rewrite:
/contact-us/test/hello.php -> /contact-us/test/hello/
If you don't want subdirectory rewriting replace line 6 with:
RewriteRule /(.*)\.php$ /$1/ [R=301,L]

Resources