HTACCESS rewrite if multiple cookies found - wordpress

I'm scratching my head trying to do make a htaccess redirect in case of a cookie is found.
I have several directories /fr-be/, /nl-be/, /en/, ...
When a user visits a page in those directories a cookie named "lang" is dropped with the corresponding directory name so 'fr-be', 'nl-be', 'en', ... as value.
If the user manually type the url : 'https://testme.com' and if the cookie is found it should be redirected to the corresponding dir.
If there is no cookie found, it should be redirected to a default dir '/en/'
If there is something else than '/en/' like https://testme.com/en/test-page/ it should not be redirect.
It is a wordpress that's why there is a ". /index.php [L]" at the end.
I can't use php redirect because the website is on a static (strattic) server
Thanks for you help !
Here is my code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_COOKIE} lang=fr-be
RewriteRule ^/$ /fr-be/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_COOKIE} lang=nl-be
RewriteRule ^/$ /nl-be/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_COOKIE} lang=en
RewriteRule ^/$ /en/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/$ /en/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Related

301 Redirect to subfolder but with input

I want my wordpress site redirections to remember the url but place it in a subfolder.
So http://www.example.com/test123 has to redirect to http://www.example.com/voetbal/test123
And http://www.example.com/0239 to http://www.example.com/voetbal/0239
My htaccess looks like this.
RewriteEngine On
RewriteBase /voetbal/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /voetbal/index.php [R=301, L]
this makes my site go from http://www.example.com to http://www.example.com/voetbal/
but only the root.
How can i make my htaccess to remember peoples input and place it after the /voetbal/
Try :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /voetbal%{REQUEST_URI} [L,R=301]

RewriteRule Apache Directives in .htaccess not working

My .htaccess file for my PHP wordpress app is in clone-directory/php/ and I could say that it was recognized by the OpenShift server. But some of the RewriteRules and RewriteCond doesn't seem to work. Since the app is accessible by both http and https by crawlers, I'm trying to create separate robots.txt for ssl and http connections.
I've been researching hours and hours with several different codes but none of them work. Some of them are as follows ...
1
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^robots.txt$ robots-https.txt [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
2
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^robots.txt$ robots-https.txt [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
3
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^robots.txt$ robots-https.txt [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
4
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} ^on$ [NC]
RewriteRule ^robots.txt$ robots-https.txt [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
5
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^robots.txt$ robots-https.txt [L]
When I visit https://reversiblean-reversiblean.rhcloud.com/robots.txt, the url won't redirect. URL is always the same.
Keep it like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} https [OR]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^robots\.txt$ /robots-https.txt [L,R,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Make sure the robots.txt and the second robots.txt exist in the repo's php directory. Unlesss OpenShift will point it to a default robots.txt, though I'm not sure where that file is stored. I was expecting for the url to change, but only the contents of the robots.txt would change according to the http protocol.
I can't believe I've spent almost half of my day on this. : )
In case anyone else was having trouble or wondering about Google's duplicate content issue, here's how my .htaccess looks now ...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule ^robots.txt$ robots-ssl.txt [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Exclude wordpress from .htaccess rules

I have a testing web where there is a testing directory lets say 'test' which contains wordpress directory lets say 'mywordpress', test and his content - excluding the mywordpress - is handled by its own .htaccess - I need the .htaccess to do nothing with the url that goes mydomain.com/test/mywordpressXX (XX can be "/" or "/anything...."
Ive tried something but this .htaccess still works with test content, but throws Internal 500 error when I am trying to reach WP admin:
RewriteEngine On
RewriteBase /test/
DirectorySlash On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)$ $1/ [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/mywordpress/?$
RewriteRule ^([^/?]+)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/mywordpress/?$
RewriteRule ^([^/?]*)/([^/?]*)$ $1.php?$1=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/mywordpress/?$
RewriteRule ^([^/?]*)/([^/?]*)/([^/?]*)$ $1.php?$2=$3
What am I doing wrong? Or how is it done correctly ?
Thanks.
Try this .htaccess:
DirectorySlash On
RewriteEngine On
RewriteBase /test/
RewriteRule ^mywordpress(/|$) - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)$ $1/ [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ $1.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]*)/?$ $1.php?$1=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]*)/?$ $1.php?$2=$3 [L,QSA]
Oh ! Silly me ! I forget about the RewriteBase /test/ you just have to make the exclude rule this way:
RewriteCond %{REQUEST_URI} !mywordpress/?$
There was an additional slash:
RewriteCond %{REQUEST_URI} !/mywordpress/?$

wordpress change to friendly URL with variable using htaccess

I have a wordpress site with a page template and 1 variable call uid (user id) , and I wanna change it to friendly url.
from
http://localhost/forum/edit-profile/?uid=1
to
http://localhost/forum/edit-profile/1
or
http://localhost/forum/edit-profile/uid/1
P/S: wordpress already has a .htaccess file , i just wanna add some additional codes to this.
I tried these solutions, but they are not working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /forumengine/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forumengine/index.php [L]
RewrieCond %{QUERY_STRING} !no-redir [NC]
RewrieCond %{QUERY_STRING} uid=([^&]+) [NC]
RewriteRule ^(.*/edit-profile)/$ $1/%1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*/edit-profile)/(\d+)$ $1/?uid=$2&no-redir [NC,QSA,L]
</IfModule>
Add this to your .htaccess rules
RewrieCond %{QUERY_STRING} !no-redir [NC]
RewrieCond %{QUERY_STRING} uid=([^&]+) [NC]
RewriteRule ^(.*/edit-profile)/$ $1/%1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*/edit-profile)/(\d+)$ $1/?uid=$2&no-redir [NC,QSA,L]
This would redirect /forum/edit-profile/?uid=1 to /forum/edit-profile/1 and back.
EDIT : (with wordpress rules they should go like)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /forumengine/
RewriteRule ^index\.php$ - [L]
RewrieCond %{QUERY_STRING} !no-redir [NC]
RewrieCond %{QUERY_STRING} uid=([^&]+) [NC]
RewriteRule ^(.*/edit-profile)/$ $1/%1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*/edit-profile)/(\d+)$ $1/?uid=$2&no-redir [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forumengine/index.php [L]
</IfModule>

How to redirect to sitemap.php from htaccess

I am using htaccess coppied from wordpress.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
It works fine.
I would like to add a script to redirect to sitemap.php file if user requests sitemap.xml.
I tried adding RewriteCond %{REQUEST_FILENAME} !-^sitemap.xml$ but didn't work.
Put the rule BEFORE that set of rules:
RewriteRule sitemap\.xml sitemap.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Resources