.htaccess Use RewriteRule, but skip Wordpress rewrite on match - wordpress

I recently asked a question about getting a specific Rewrite to work. I did get a working answer for that and used it on one of my sites. Now I'm trying to reuse this approach, but I'm finding that the logic is falling through to the Wordpress Rewrite section and my own redirects are not being triggered at all.
# BEGIN Redirect deprecated site links to wp equivalent
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^news=13$
RewriteRule ^news_comments.php$ http://example.com/example-directory/ [R=301]
RewriteCond %{QUERY_STRING} ^news=51$
RewriteRule ^news_comments.php$ http://example.com/example-page.php [R=301]
</IfModule>
# END Redirect deprecated site links to wp equivalent
# 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 obviously need the Wordpress rewrites, but I need for the ones that I specify at the top to be triggered and skip over the Wordpress rules.
I've searched around and seen some suggestions of using something like...
...
RewriteCond %{QUERY_STRING} ^news=51$
RewriteRule ^news_comments.php$ http://example.com/example-page.php [R=301]
RewriteRule ^news_comments.php$ - [L]
</IfModule>
...but I just can't seem to get it to work. Any ideas?

You need to make sure to add [L] flags to the end of your redirects, otherwise the URI continues its way through the rest of the rules and the redirect happens at the end. By then, wordpress has already had its way with the URI.
RewriteCond %{QUERY_STRING} ^news=13$
RewriteRule ^news_comments.php$ http://example.com/example-directory/ [R=301,L]
RewriteCond %{QUERY_STRING} ^news=51$
RewriteRule ^news_comments.php$ http://example.com/example-page.php [R=301,L]

Related

website redirections different from .htaccess configuration

I have web page created in Wordpress v5.4, Theme Twenty Twelve and just installed SSL certificate.
I want to have such redirection structure (redirection codes in rectangles), to optimize for SEO:
but according to Google Chrome Site Inspector I have the follwing one:
So the schema is not fully optimized as for two cases there are dwo cascade redirections whereas should be one.
What is weird, current schema doesn't reflect .htaccess file. My .htaccess file looks as follows:
# 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
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
So, my understanding of the code is that if url is typed in without https://, script rewrites it into version with https:// an nothing else, so there must be another place where other redirections (e.g. with www -> without www) are being made.
I have checked index.php - no redirections.
I also checked additional lines in .htaccess file don't overwrite those "hidden" ones, so if I simply add them I got multiple redirections error in web browser.
Could you please advise me what else should I do to get desired structure of redirections?
First rule redirect all www to https://domainname.com/, second all http to https://domainname.com/, one hop
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://domainname.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://domainname.com/$1 [R=301,L]
</IfModule>
# 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’m guessing the redirect from www to non-www is due to the address you’ve set in your settings (WordPress dashboard → Settings → General).
For the redirect you want to achieve, you have to add rewrite conditions for the www. in your .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Wordpress redirects with query (?) strings - HTAccess File being wiped every 'x' amount of time

So I have been experiencing a rather brain confusing issue that seems to have quite a few conflicting issues.
On a new website (wordpress) I have setup we had lots of redirects that had query strings in them. Now all the redirect plugins I activated could not handle these so I found topics on here that helped me manually add these to the .htaccess file.
Here is an example (see lines that are **)
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
**RewriteCond %{QUERY_STRING} unit=32x10**
**RewriteRule ^unit\.php$ /available-units/32ft-x-10ft/? [L,R=301]**
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I have a long list of these RewriteCond / Rules.
We noticed that after a period of time these manual redirects I added were all being wiped and the .htaccess file was reverting back to the normal version (see below). To prevent this, I changed the file permission to not writable.
# 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
However, since doing this I am now unable to use Hide My WP and Really Simple SSL as they both require access to the .htaccess file.
Is there anything else I can do with these redirects that won't end up being wiped ?
Perhaps I am putting them in the wrong place within the .htacess file?
Your help would be much appreciated.
Many Thanks
MORE DETAILS ADDED
This is more query string redirects that are present....
RewriteCond %{QUERY_STRING} unit=32x10
RewriteRule ^unit\.php$ /available-units/32ft-x-10ft-32x10/? [L,R=301]
RewriteCond %{QUERY_STRING} unit=4579
RewriteRule ^unit\.php$ /available-units/32ft-x-20ft-4579/? [L,R=301]
RewriteCond %{QUERY_STRING} unit=4147
RewriteRule ^unit\.php$ /available-units/unit-ref-4147/? [L,R=301]
RewriteCond %{QUERY_STRING} unit=4768
RewriteRule ^unit\.php$ /available-units/2020-range-unit-ref-4768/? [L,R=301]
Try this solution:
RewriteRule ^(.*)$ unit.php/available-units/32ft-x-10ft/?unit=32*10 [L,QSA]

htaccess redirect for WordPress posts but NOT pages to subdomain

I'm organizing a client site into several subdomains. I've used the following to redirect all the posts on oldsite.com/blog-post-name to blog.oldsite.com/blog-post-name and it works like a charm. However, we want oldsite.com, oldsite.com/page1 and oldsite.com/page2 to remain as they are and NOT be redirected to the subdomain. I feel like I should be able to add an exception for the applicable pages - there are only 5 of them - but I can't figure out how that exception should read. For reference, oldsite.com is in the root directory and WordPress files are in oldsite/subdomain/subfolder.
oldsite.com htaccess:
RewriteEngine on
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://blog.oldsite.com/$1 [R=301,L]
Update
Old code started breaking the theme CSS, and incidentally so does the new code. I'm thinking the negative condition is fighting with the basic WP htaccess. Here's what I have at the moment:
RewriteEngine on
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^(repairs|contact)/?$
RewriteRule ^(.*)$ http://blog.oldsite.com/$1 [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
Should I be adding the conditions within the WP set? Why did the code above work before (yes, with WP code)?
You can do:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(page1|page2)?$
RewriteRule ^(.*)$ http://blog.oldsite.com/$1 [R=301,L]

mod_rewrite - need to match this code with wordpress

I want to strip out the ?q= from my old drupal links for my site now in Wordpress
I need help with two things.
First, how do I specify a range of words instead of numbers, as in the snippet below? Once I do that will this code work to redirect ".../?q=postname" to simply ".../postname"?
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([0-9]+)$
RewriteRule .* http://www.mysite.com/?q=%1 [R=301,L]
Secondly, once i have that code working, how do I integrate it with the default Wordpress mod_rewrite (which is below)?
# 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
Try this:
RewriteCond %{QUERY_STRING} ^q=(\w+)$
RewriteRule .* http://www.mysite.com/%1? [R=301,L]
BTW I would use [R=302,L] at first until I make sure everything works as expected. If you use 301 with wrong RewriteRule browser will cache the redirect and any further RewriteRule changes would require you to restart browser for each code change to see any difference.

How do I combine WordPress permalink code and .htaccess redirect?

I'm trying to combine the following code so that the WordPress permalinks work in the main directory, waringis.com (top code) and a second domain, burrowpress.com, is redirected to the subdirectory 'waringis.com/burrowpress' (bottom code) -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} burrowpress.com$ [NC]
RewriteCond %{REQUEST_URI} !^/burrowpress/.*$
RewriteRule ^(.*)$ /burrowpress/$1
Right now permalinks are working in WordPress and the redirect works but images have to be direct links to waringis.com/burrowpress/images/ instead of burrowpress.com/images/ - Any help is much appreciated...
You need to swap your code blocks around. The [L] flag on the WordPress rules are stopping execution of the file at that line in their code since your special path would "pass" the WordPress REWRITE_COND statements:
RewriteEngine On
RewriteCond %{HTTP_HOST} burrowpress.com$ [NC]
RewriteCond %{REQUEST_URI} !^/burrowpress/.*$
RewriteRule ^(.*)$ /burrowpress/$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You don't need two RewriteEngine On statements, but since WordPress is able to rewrite your .htaccess file (depending on how you have it setup) you might want to leave it. If you are updating your file manually, you can remove the second RewriteEngine on directive.
The important part is that I moved your special rules ahead of wordpress.
I'm not a .htaccess expert but i would say that you don't need to include RewriteEngine On twice that you can put the redirect code in the if statement. So you want to redirect to the subdomain but not have your images be kept in the subdomain is this correct?

Resources