htaccess RewriteCond using cookie value - .htaccess

I am using below code to redirect the site but its not working. Please suggest me how can I fix the issue.
RewriteEngine On
RewriteCond %{HTTP_COOKIE} site_mobile=desktop; [NC]
RewriteRule ^ http://www.google.com [NC,L]
thanks in advance

I think trailing ; may not be always there. Try this regex:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} (^|\s)site_mobile=desktop(;|$) [NC]
RewriteRule ^ http://www.google.com [R,L]
If that doesn't redirect try this condition (for testing):
RewriteCond %{HTTP_COOKIE} desktop [NC]

Related

htaccess 301 redirect root to subdirectory

Trying to get
www.example.com
to go directly to
www.example.com/forum
How can I do this with this configuration? Thanks in advance.
Edit:
Right now I have added below content in .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteRule ^(.*)$ /forum/$1 [L,R=301]
I want result like
https://www.example.com/topic/topic-url/
to
https://www.example.com/forum/topic/topic-url/
With your shown samples, could you please try following. Please clear your browser cache before testing your URLs. Your tried rules will be creating an infinite loop hence it may not be working, I have added a condition if uri doesn't start from forum then only proceed with redirection.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_URI} !^/forum [NC]
RewriteRule ^(.*)$ /forum/$1 [L,R=301]

.htaccess SEO friendly URL with queryString

I have a page like:
page.php?mode=dashboard&hl=en
and i want to use like:
site.com/dashboard?hl=en
how must i use RewriteRule? Thanks...
EDIT: I think this is not possible with .htaccess. Need to use javascript.
wish it helps you
# Redirect /Category.php?Category_Id=10&Category_Title=some-text
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^Category_Id=(\d+)&Category_Title=([\w-]+)$
RewriteRule ^Category\.php$ /Category/%1/%2? [R=301,L]
You may be able to use this code in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /page\.php\?mode=([^\s&]+)&(hl=[^\s&]+) [NC]
RewriteRule ^ /%1?%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w-]+)/?$ page.php?mode=$1 [L,QSA]

RewriteCond %{QUERY_STRING} ^page=3$.How to work only page=3? I do not want to redirect page=33

I want to redirect only /?page=3 to /.
/?page=33 is not redirect.
The following code also redirects /?page=33
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=3$
RewriteRule / [R=301,L]
You can use:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=3$
RewriteRule ^ /? [R=301,L]
With ? to prevent the automatic addition of ?page=3
Try it like this, clear your browser cache first after that if it works change R=302 to 301
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=3
RewriteRule ^ / [R=302,L]

Multiple parameters URL redirection to another preety URL

I want to redirect via htaccess:
test.mydomain.com/articles_main.php?post=POSTTILE&name=&email=
To:
mydomain.com/POSTTILE
Any help is greatly appreciated, I have struggled with this for a long time, please help.
EDIT:
I have tested many things but what remained commented is this:
RewriteEngine On
RewriteBase
RewriteCond %{THE_REQUEST} \s/articles_main.\.php\?post=([^&]*)&name=([^&]*)&email=([^&]*) [NC]
RewriteRule ^articles_main\.php\?post=([A-Za-z0-9-]+)&name=&email=$ http://mydomain.com/$1 [L,R=301]
RewriteRule ^articles_main\.php\?post=([A-Za-z0-9]+)&?$ ^/$1/?$ [R=301]
Your regex is not correct. Use this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.mydomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/articles_main\.php\?post=([^&]+)&name=([^&]*)&email=([^&\s]*) [NC]
RewriteRule ^ http://mydomain.com/$1? [L,R=301]
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^([A-Za-z0-9]+)/?$ articles_main\.php?post=$1name=&email= [L,QSA]

RewriteRule at htaccess wrong redirect

ive this rules at htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^site.com [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^warcraft$ www.site.com/forumdisplay.php?f=480 [R=301,NE,NC,L]
issue happend when redirect warcraft its redirect to
http://www.site.com/home/site/public_html/www.site.com/forumdisplay.php?f=480
any tip ?
I suppose you want to redirect it to http://www.site.com/forumdisplay.php?f=480
If so put it in your .htaccess file:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^warcraft$ http://www.site.com/forumdisplay.php?f=480 [R=301,NE,NC,L]
You must provide the full URL including protocol in your rule, in this case http://www.site.com/.....

Resources