htaccess 301 redirect querystring to root - regex

I'm stuck trying to redirect my old posts on my wordpress blog from www.domain.com/?p=444 to just the root, this is because the posts have been removed.
This is what i have tried so far.
RewriteCond %{QUERY_STRING} p=[0-9]+
RewriteRule ^p$ / [L,R=301]
Any help would be much aprreciated.
Best Regards

Use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} p=[0-9]+ [NC]
RewriteRule ^$ /? [L,R=301]
/? means root URL with ? for removing existing query string removed.

Related

Write `.htaccess` regex to redirect URL to previous category/sub-directories

I have a site at www.test.com. The site has many nested directories and categories such as www.test.com/cat/1/321/.
I want to use a Regex to remove any url ending in /321/ or /321 to the previous category.
Example: Redirect www.test.com/cat/1/321/ to www.test.com/cat/1/
Needed for a Wordpress site using .htaccess. Thanks!
You can write your conditions and rules for exam in this code we have to conditions for /321/ and /321
RewriteCond %{REQUEST_URI} /321/$ [NC]
RewriteRule ^(.*)/321/$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} /321$ [NC]
RewriteRule ^(.*)/321$ /$1 [R=301,L]
Simple enough
RewriteEngine On
RewriteRule ^(.*/)321/?$ $1 [R,L]

Redirect Query String from Site Root to Sub-Domain Site Root

I'm trying to redirect
http://example.com/?download_invoice=true&attendee_id=28916&r_id=504-55803a183a0cf
to
http://secure.example.com/?download_invoice=true&attendee_id=28916&r_id=504-55803a183a0cf
but aren't having any luck. I'm able to redirect Query Strings from sub-directories just fine using this syntax:
RewriteCond %{REQUEST_URI} ^/sub-directory/$
RewriteRule ^.*$ http://secure.example.com/sub-directory/ [L,R=301]
but can't figure out how to do it from the site root. Any help would be greatly appreciate, thanks in advance!
You need to match host name in RewriteCond and pattern ^$ in RewriteRule:
RewriteCond %{QUERY_STRING} .
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$
RewriteRule ^/?$ http://secure.%1/ [L,R=301]
QUERY_STRING gets forwarded automatically to target URL.

HTACCESS 301 redirect URL with query string with no request URI

I have a site that has a bunch of URLs indexed in Google that look like this -
http://www.domain-namr.com/?option=filter&option2=&option3=
I am trying to redirect all of these to a new URL in HTACCESS with this code -
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^option=filter$
RewriteRule ^(.*)$ /new-url.html [R=301,L]
Of course it is not working. On all of the pages that have a filename I can use in the request uri condition, the redirects work. What am I missing?
It is not working due to the wrong regex pattern in your rule.
Try this rule as your very first rule in root .htaccess:
RewriteCond %{QUERY_STRING} ^option=filter(&|$) [NC]
RewriteRule ^$ /new-url.html? [R=301,L]

htaccess redirect one folder to another on same domain if request_uri condition false

I'd like to redirect the visitors if they visit the old not working URL example.com/english/* to example.com/german/* but not when they're visiting example.com/
This code in .htaccess is not working:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/german/(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^/english/(.*)$ /german/$1 [R=302,NC,L]
I searched throrough the apache docs and google but nothing does the same.
Where's the problem?
Replace your code with this:
RewriteEngine on
RewriteRule ^english/(.*)$ /german/$1 [R=302,NC,L]
RewriteRule doesn't match leading slash in .htaccess.

Htaccess, How do I redirect this dynamic URL

I have a strange redirect situation that I cannot get to work.
www.example.com/?NR
www.example.com/?PV
I need to get these two URL's rewritten and redirected to the home page but I can't get it to work. Here is what I have tried:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^NR$ [NC]
RewriteRule www.example.com/ http://www.example.com [R=301,L]
RewriteCond %{QUERY_STRING} ^PV$ [NC]
RewriteRule www.example.com/ http://www.example.com [R=301,L]
We bought this site and I have no reference to what those pages might actually be, they just need to be redirected and rewritten properly.
This one rule should work for both of your requirements:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(NR|PC)$ [NC]
RewriteRule ^/?$ /? [R=301,L]
Remember that RewriteRule only matches REQUEST_URI which is URI part without domain name and query string.
/ in target URI is for your home URL and ? in the end is to strip any existing query string.
Reference: Apache mod_rewrite Introduction