mod_rewrite - need to match this code with wordpress - 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.

Related

removing part of the url using .htacess but redirect to original URL internally

I have modified the href link using preg_replace
for example the original link is
http://testdomain/test/save20
to
http://testdomain/save20
but when I click on this link I still get http://testdomain/test/save20
besides I only want to change it in the browser like
http://testdomain/save20
but internally it should continue to use the url
http://testdomain/test/save20
I have tried in .htaccess like this
RewriteRule ^test/([^/]+)/?$ /test=$1 [NC,L]
but it doesn't seems to have any effect.
The full code in .htaccess is like this:
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]
RewriteRule ^test/([^/]+)/?$ /test=$1 [NC,L]
</IfModule>
# END WordPress
You can use the following rule
RewriteEngine on
RewriteRule ^([^/.]+)$ /test/$1 [L]

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]

Rewrite domain to contain path prefix

Having some trouble with this rewrite code. I want to add a few domains (all pointing to the same server) and make it so that they have a "hidden" URL prefix being sent to the server.
Example: You type in example-one.org/contact, the server looks at it as /one/contact/
Updated code based on #anubhava's answer
I still receive a 500 Internal Server Error when using this code. I put it through an htaccess tester and it looks like it should work.
Here is the full htaccess code, including WordPress' defaults, and without using example.org:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^discoverdowntownspringfield.org$ [NC]
RewriteRule ^((?!downtown-springfield/).*)$ /downtown-springfield/$1 [L,NC]
# 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
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.org$ [NC]
RewriteRule ^((?!one/).*)$ /one/$1 [L,NC]

.htaccess Use RewriteRule, but skip Wordpress rewrite on match

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]

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