htaccess script for Wordpress to serve selected page as https - wordpress

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.

Related

Redirect url of wordpress site on the basis of string using htaccess

I have a wordpress site eg. http://www.domain.com/ with custom theme hosted on a linux server and working fine. Some marketing strategy requires me to distinguish between 2 kinds of URLs (that they will be shooting from ads) and based on that redirect the site to different links.
eg:
If the URL contains /xyz/ ie:
http://www.domain.com/xyz/category/post I want it to be redirected to
an intermediate page ie: http://www.domain.com/intermediate.php and if
the url doesnt contain /xyz/ ie:
http://www.domain.com/category/post the post should show up as
usual.
I found out that this cant be done inside the wordpress code as before the first hook is triggered, the url is processed and a 404 page is thrown.
The only way I can achieve this is by modifying .htaccess file.
I found a code which reads:
RewriteRule ^(.*)/xyz/(.*)$ http://www.domain.com/intermediate.php [L,R=301]
and second way reads:
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule .* http://www.domain.com/intermediate.php
I am really confused about using it with the existing code in the htaccess file created by wordpress which reads:
# 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 tried merging the codes ie:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /domain/index.php [L]
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule RewriteRule ^(.*)/xyz/(.*)$ http://domain.com/intermediate.php
</IfModule>
but only one of them work at a time ie: when the redirection works, wordpress posts doesnt show up and vice versa.
Kindly show me a better way.
Immediately after the RewriteEngine On and RewriteBase directives in the "existing" WordPress code try this:
RewriteRule xyz/ http://www.domain.com/intermediate.php [L,R=301]
This will search for "xyz/" anywhere in the requested URL and redirect when found. Note that in .htaccess files, the directory prefix (/ in this case) is removed from the URL path before pattern matching. So, a pattern that starts / will never match at the start of the URL.
External redirects should generally come before internal rewrites, which is what the default WordPress directives do.
The alternative method you mention is less efficient:
RewriteCond %{REQUEST_URI} /xyz/
RewriteRule .* http://www.domain.com/intermediate.php [R=301,L]
This will effectively do the same thing but will result in every request being processed because of the generic .* pattern. Note that the REQUEST_URI does start with a / (directory prefix).

Redirect all URLs to homepage except core WordPress files

I've recently gained access to a domain which used to have a very large site and product base, however the old host turned it off and all the URLs are lost. I am using WordPress on the domain (previously it used an e-com shop called Kyrptronic) and have set up a simple landing page. So far there are over 20,000 not found URLs and I need them to redirect to the homepage (except for css and jpg files that I am using on the landing page).
I tried to redirect all old links straight to homepage however this means the core wordpress files become inaccessable and when you try and click anything in the backend E.g. Plugins - it redirects to the homepage. Here is the code I used in the htaccess file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|xml)$
RewriteRule (.*) / [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Is there a htaccess code snippet that can redirect all URLs other than jpgs, pngs and NOT the entire wordpress and website directory with .php file extensions?
The site has too many not found URLs to do one by one.
Hope this makes sense, it's my first post on here!!
Thanks
You could try something like this (it must e above the wordpress routing rules):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|xml|php)$
RewriteRule ^ / [R=301,L]

Modifying wordpress htaccess to forward certain urls to subdirectory

For whatever reason or another I haven't been able to ascertain, my company has decided to go with wordpress for one of their websites. They asked me to build an affiliate application on the same domain, which I did. Everything works great with the exception of this dilemma:
wordpress is installed in the root directory. All pages, videos, sales, etc are made from within wordpress pages.
The affiliate application is in a subdirectory /aff/ and affiliates' pages are found at mydomain.com/aff/index.php?aff=affiliateusername
Affiliates (and their leads) should be able to load their pages simply by typing in www.mydomain.com/affiliateusername but I am struggling to understand how to translate wordpress htaccess rules to do this.
Obviously the best order in which to have this work is for wordpress to first determine if there are any blogs/posts/pages that match the url term FIRST, and if none is found, then to redirect all else to www.mydomain.com/aff/index.php?aff=whatever
Here's what I was finally able to come up with that works for the index page and for the affiliate pages, but does not correctly load any wordpress pages other than index.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(images|wp-admin|wp-content|wp-includes|go|compliance\.html)($|/) - [L]
#RewriteRule ^([^/].*)$ /aff/index.php?aff=$1 [R,L]
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You can tell I've attempted to exclude certain directories from the rewrite but have not been successful. I've read other advice via Googling, to put the redirect rules ahead of the wordpress block, but there are few issues. When I put this line ahead of the # BEGIN WordPress line, I get an endless redirect loop which keeps going to /aff/index.php?aff=aff/index.php?... etc (this is the same line I use for the same affiliate application on a different, wordpress-free, domain)
#RewriteRule ^([^/].*)$ http://www.mydomain.com/aff/index.php?aff=$1 [L]
I feel like I'm missing something terribly obvious. Should I just be setting up wordpress to redirect all 404's to /aff/index.php?aff=originalrequest? How would I go about doing that?
Thanks in advance.
You are using RewriteRules incorrectly in place of RewriteConds. Adding them in between the WordPress rules is surely breaking your blog as well. Change your .htaccess code to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)$ /aff/index.php?aff=$1 [R=301,QSA,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
If you want the URL to stay mydomain.com/affiliate remove the [R] and use only [QSA,L]. I've updated the rules above to show how to exclude a path from affiliate redirection. The following
RewriteCond %{REQUEST_URI} !^/blog\b
excludes all URLs pointing to /blog or its sub-directories /blog/sub/dirs from redirection. If there are root-level .php pages present (even if they are few) the exclusion can more easily be handled by changing the rule to
RewriteRule ^([^/.]+)$ /aff/index.php?aff=$1 [R=301,QSA,L]
assuming that a . and a / can never be present in an affiliate name.

Redirect Spam Query String URLs Created After Hack

One of my sites was hacked and after that thousands of urls were showing up.
I want to edit my htaccess so that all urls that have /?x= are redirected to page without the query string.
The site is a word press site so cant redirect all urls with a query string or the site stops working properly.
Basically I need:
www.example.com/?x=spamurl
to redirect to
www.example.com
and any deep page that includes ?x=
www.example.com/category/?x=spamurl
to redirect to
www.example.com/category/
This is my current htaccess
# 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 %{QUERY_STRING} x=
RewriteRule ^$ http://www.example.com/? [R=301,L]
</IfModule>
# END WordPress
As you can see I have managed to get any url with ?x= after the main url to redirect to the main page but I cant figure out how to do this when ?x= shows after category pages etc..
Is there coding that can redirect ALL urls that include ?x= to a 404 page without causing an issue for wordpress files?
Many Thanks
Scott
You need to move your redirect rules to before your wordpress rules. You also need to match any incoming URI:
RewriteCond %{QUERY_STRING} ^(.*)(^|&)x=[^&]+(.*)$
RewriteRule ^(.*)$ /$1?%1%3 [R=301,L]
# BEGIN WordPress
etc...
This will remove only the x=something parameter, and if you have other parameters before or after, those are preserved.

.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