htaccess condition specific pages - wordpress

I am having a little issue with my htaccess, what my goal is and what is currently working on my WordPress site is:
/blog/title-of-blog-post/1923
is being redirected to
/title-of-blog-post/
That is done with this rule:
RewriteRule ^blog/(.*)/.*/$ $1/ [R=301,L,QSA]
However, that means it is also redirecting
/blog/page/2/ to /page/
/blog/page/3/ to /page/
etc
Is there a condition to which I could stop the /blog/page/nr being redirected, but still keep those rules as they are?
Many thanks

You could add an RewriteCond before your rule.
RewriteCond %{REQUEST_URI} !^/blog/page/[0-9]+/?$
RewriteRule ^/?blog/(.*)/.*/?$ $1/ [R=301,L,QSA]
You could probably optimize it a bit if needed.
Hope it helps you forward.

Related

How to redirect http to specific URL using htaccess in WordPress

I am looking for assistance with the redirection rules.
I have url www.example.com.au and I want the following redirections
https://www.example.com.au =>https://www.example.com.au
http://www.example.com.au =>https://www.example.com.au/page-2
I found the following .htaccess rules but its not working for me
RewriteEngine On
RewriteCond %{HTTP_HOST} www^example\.com.au [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%.
{HTTP_HOST}%www^example.com.au/page-2 [302 L]
can anyone please help me the correct rule.
I will be using the above rule for a WordPress site so do i need to paste the above rules at the bottom so that it doesn't overruled by other rewrite rule?
Thanks in advance.
For the first redirection it is impossible, it will create an infinite loop. You can not redirect an url on itself.
And for the second one ( http://www.example.com.au => https://www.example.com.au/page-2 ) you can do something like that
Redirect 301 / https://www.example.com.au/page-2
Pay attention to 301, it indicates that the redirection is permanent, I do not know if it is your goal.
But if your goals was to redirect http to https you can do something like that (put on the top of your .htaccess file):
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://www.example.com.au/$1 [R=301,L]
Or use a plugin like Really Simple SSL
Then you can define your rules to redirect one page to another before the rule above, like this:
Redirect 301 /old-page /new-page

htaccess to https subdomain and wordpress child theme - too many redirects?

A bit out of my comfort zone, hoping someone can point me in the right direction!
I need to force all http://www.mywebsite.com to go to https://sub.mywebsite.com.
So, I need to force all visitors to the sub-domain, but also the secure https version.
I've tried editing the root htaccess file but I get 'too many redirects' error.
The new site is using a Wordpress child theme in case that makes a difference - when I edited the root htaccess the site appeared unstyled and the 'too many redirects' error related to CSS and JS files.
I'm sure it's something silly and small but any help, much appreciated! :-)
Thanks
Try putting this into the htaccess file above all other existing rules:
RewriteEngine On
# First redirect to the subdomain
RewriteCond %{HTTP_HOST} ^(www\.)?example.com
RewriteRule ^(.*)$ https://sub.example.com%{REQUEST_URI} [L,R=301]
# Then ensure HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://sub.example.com/$1 [L,R=301]

Apache HTAccess Remove Query String for redirect

It sounds like a relatively simple one but here goes.. I have a wordpress website called example.com It has recently been redeveloped using wordpress however the old sites structure used the following www.example.com/?page_id=22 I want to redirect anything after the domain name that contains /?page_id=[any digit] to the homepage, im having great difficulty in accomplishing this and I also think I may have issues because wordpress itself uses the query string page_id before the permalinks redirects kick in, I could be wrong though but any help on removing the query string directly after a domain name would be great especially keeping in mind that it is a wordpress install.
I have tried various iterations that look like the following to no avail
RewriteCond %{QUERY_STRING} ^page_id=(.*)$
RewriteRule (.*) http://www.example.com/ [R=301,L]
RedirectMatch 301 ^/page_id=(.*)$ /
Try this rule right at top, just below RewriteEngine On line:
RewriteCond %{QUERY_STRING} ^page_id=\d+$ [NC]
RewriteRule ^/?$ /? [R=301,L]
/? will strip off any query string.

Doing 301 redirects of urls containing a specific string caused by hacking

I have a wordpress site that was hacked and hundreds of urls were created that no longer exist after I cleaned the site up. I have noticed a lot of these urls contains a specific keyword as displayed below
http://www.capecrossfit.com/kamagra-holland/ - I need to rewrite all urls containing the word kamagra to http://www.capecrossfit.com/ - I have tried all the recommendations on near similar requests on this site with no joy, can anyone please advise how this can be done with htaccess?
Pace this rule as very first rule (just below RewriteEngine On line):
RewriteEngine On
RewriteCond %{THE_REQUEST} /kamagra-holland|buy-kamagra-europe|kamagra-super-jelly [NC]
RewriteRule ^ / [L,R=301]

HTACCESS Redirect from URL including ? to static page

I have a couple of hits everyday from a now defunct tutorial I wrote. Rather than letting them sit on the 404, I have created a page with links to more up to date ones in the hope that someone else can help.
I am attempting to edit my htaccess to rediret
My site is WP powered and the incoming link is
/tutorials/?p=3
I would like it to go to:
/wiki/sorry-this-tutorial-no-longer-exists
I have tried:
Redirect /tutorials/?p=3 http://domain.com/wiki/sorry-this-tutorial-no-longer-exists
The closest I got was
RedirectMatch 301 ^/tutorials/?(.*) http://domain.com/wiki/sorry-this-tutorial-no-longer-exists
But that of course sticks the ...exists/?p=3 on the end which doesn't work
Any help with the bloody question mark? (I tried htaccess url rewrite with question mark but it would not work?)
I believe this should do what you are looking for
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/tutorials/$
RewriteCond %{QUERY_STRING} ^p=3
RewriteRule ^(.*)$ http://domain.com/wiki/sorry-this-tutorial-no-longer-exists? [R=302,L]

Resources