How to rewrite a directory to a cookie value in .htaccess? - .htaccess

$val = substr(str_shuffle(str_repeat("abcdefghijklmnopqrstuvwxyz0123456789", 12)), 0, 12);
setcookie('sub' , $val , time() + (86400) , '/');
example.com/folder/file.php should be rewritten to the value of $_COOKIE['sub'] e.g. example.com/6df1fcb7aba6/file.php
How can I do that?
I tried this:
RewriteCond %{HTTP_COOKIE} !^.*sub.*$ [NC]
RewriteRule %{HTTP_COOKIE} !^.*sub.*$/(.*) folder/$1
, but doesn't work

You can do something like the following in the root .htaccess file using mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} (?:^|;|\s)sub=([a-z0-9]{12})(?:$|;)
RewriteRule ^folder/(file\.php)$ %1/$1 [L]
The above states:
Given a request for /folder/file.php
And the cookie sub exists with a 12 char alphanumeric (lowercase) value.
Rewrite the request to /<sub-cookie-value>/file.php
The %1 backreference contains the value of the sub cookie, providing it meets the criteria of being a 12 char alphanumeric (lowercase) value.
The $1 backreference simply contains file.php as captured from the RewriteRule pattern.
RewriteCond %{HTTP_COOKIE} !^.*sub.*$ [NC]
RewriteRule %{HTTP_COOKIE} !^.*sub.*$/(.*) folder/$1
The RewriteRule pattern (first argument) is a regex that matches against the requested URL-path, so %{HTTP_COOKIE} (seen as a literal string) will never match, so the rule does nothing. However, the syntax of the 3rd argument (flags) is entirely invalid so is likely to result in a 500 Internal Server Error ("bad flags delimiter").
The RewriteCond directive is checking that the string sub does not occur anywhere in the Cookie HTTP request header, which is not what you want to do. You need to check that sub is present and extract the value.

Related

Remove trailing dot in URL domain

We have URLs of the form:
www.dev-studio.co.uk.
www.dev-studio.co.uk./a-sample-image
With the help of .htaccess rules, I am trying to remove the trailing dot (co.uk.) in the end of the domain name but I'm failing.
This is the rule I'm trying:
RewriteCond %{HTTP_HOST} ^([a-z0-9\.-]+)(\.co\.uk\.)(.*)$
RewriteRule ^ http://www.dev-studio.co.uk/%3 [L,R=302,NE]
But the %3 which should capture the 3rd group is returning empty.
The goal is to simple redirect www.dev-studio.co.uk./a-sample-image to www.dev-studio.co.uk/a-sample-image
I have tried all the other questions over here but the solutions are not working for me.
Any help would be appreciated.
RewriteCond %{HTTP_HOST} ^([a-z0-9\.-]+)(\.co\.uk\.)(.*)$
RewriteRule ^ http://www.example.co.uk/%3 [L,R=302,NE]
The HTTP_HOST server variable contains the hostname only (ie. the value of the Host HTTP request header), it does not contain the URL-path, so the %3 backreference is always empty.
You need to either capture the URL-path from the RewriteRule pattern. For example:
RewriteRule (.*) http://www.example.co.uk/$1 [R=302,L]
Or, use the REQUEST_URI server variable (which contains the full URL-path, including slash prefix) instead:
RewriteRule ^ http://www.example.co.uk%{REQUEST_URI} [R=302,L]
This should ultimately be a 301 (permanent) redirect, once you have confirmed it works OK.
Note that since you are redirecting to a specific domain, do you need a CondPattern that matches any .co.uk hostname? You could be specific:
RewriteCond %{HTTP_HOST} =www.example.co.uk.
RewriteRule ^ http://www.example.co.uk%{REQUEST_URI} [R=302,L]
The = prefix on the CondPattern changes it to a lexicographical string comparison (not a regex), so no need to escape the dots.
If you wanted an entirely generic solution to remove the trailing . (FQDN) from any requested host then you could do something like:
RewriteCond %{HTTP_HOST} (.+)\.$
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
Although you might want to combine this with your canonical redirects (eg. non-www to www / HTTP to HTTPS?) to avoid multiple redirects - although they are probably unlikely to occur all at once anyway, so probably not an issue.

Understanding .htaccess rewrite rule

I stumbled upon this .htaccess file and am trying to figure out what it does.
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !upload\.php$
RewriteRule ^ - [L,H=default-handler]
RewriteEngine On
Line above says , you activate mod_rewrite
RewriteCond %{REQUEST_URI} !/$
Line above is a condition to be applied with URI , URI is a part of path after host not including query string http://httpd.apache.org/docs/current/mod/mod_rewrite.html , for example http://example.com/whatever/index.php , /whatever/index.php is the URI part in this case , then there is !/$ with means ! not end $ with / so this condition will match any URI that not ends with /
RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !upload\.php$
Two line above also are conditions to don't match URIs that end with either index.php or upload.php
RewriteRule ^ - [L,H=default-handler]
Above line illustrates the rule that should applied according to those conditions and it includes Pattern , substitution and flags , you can read more here https://httpd.apache.org/docs/2.4/rewrite/intro.html and herein means any request passes conditions above will be handled by default handler , So the Pattern is ^ which is regular expression https://httpd.apache.org/docs/2.4/rewrite/intro.html means matches the beginning of the string and substitution is - which means do nothing then the flags [L,H=default-handler] , L means stop processing the rule set , H means forces the resulting request to be handled with the specified handler and it is default handler in your case .
see this about flags https://httpd.apache.org/docs/2.4/rewrite/flags.html and this to understand handler https://httpd.apache.org/docs/2.4/handler.html

add string at the end of URL using htaccess which has query string also

I want to change
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
That is if the URL contains index.php?members, then I add /#div at the end of url. I tried this
RewriteEngine On
RewriteCond %{REQUEST_URI} index.php?
RewriteRule (.*) /%1/%{QUERY_STRING}&123 [L,QSA,R=301]
but it returns
domain.com/members/maxmusterman.5&123?members/maxmusterman.5
Note here that &123 is attached after URI before starting parameters. I researched htaccess QSA flag but I could not find a way to add a custom string at the end of the query string. How can I do that. Here I have used &123 for test purpose, actual requirement is adding /#div
To redirect
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
.
You can use something like the following :
RewriteEngine on
RewriteCond %{QUERY_STRING} !loop=no
RewriteRule ^division1/index\.php$ %{REQUEST_URI}?%{QUERY_STRING}&loop=no#div [L,R,NE]
I added an additional perameter loop=no to the destination url to prevent infinite loop error .You can't avoid this as both your old url and the new url are identical and can cause redirect loop if you remove the RewriteCond and Query perameter.
NE (no escape ) flag is important whenever you are redirecting to a fragment otherwise mod-rewrite converts the # to its hex %23 .
solution #2
RewriteEngine on
RewriteCond %{THE_REQUEST} !.*loop=no [NC]
RewriteCond %{THE_REQUEST} /division1/index\.php\?(.+)\s [NC]
RewriteRule ^ /division1/index.php?%1&loop=no#div [NE,L,R]
Clear your browser cache before testing these redirects.

How can I change url parameter value using htaccess

I want to validate url parameters using htaccess, where a certain parameter needs to have a valid value. If the value for the parameter is invalid, I want to redirect to another page.
E.g.: www.mydomain.com/folder/page1?specialId=1a3b5c78
The value for the "specialId" parameter needs to be an 8 character string.
If the value is invalid (e.g.: 1a3b5c78x or abc) I want to redirect to www.mydomain.com/folder/page2
You can use in your htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} specialId [NC]
RewriteCond %{QUERY_STRING} !specialId=[A-Z]{8}(?:&|$) [NC]
RewriteRule page1/?$ /folder/page2? [R=301,NC,L]

htaccess mod rewrite changes http://www to http:/www

I want to replace calls like this:
www.mysite.com/sub/file.php?param1=x&param2=http://www.someurl.com
with:
www.mysite.com/sub/param1/param2
Param 1 is an integer number Param 2 is a url
I wrote this rewrite rule in htaccess:
RewriteCond %{REQUEST_URI} \/sub\/
RewriteRule sub\/([0-9]+)\/(.*)$ sub\/file.php?param1=$2&param2=$1 [L]
Unfortunately param2 (the URL) starts with http:/www.someurl.com instead of http://www.someurl.com (note the single slash).
Any idea what causes it? When I call the same file with same parameters in the format www.mysite.com/sub/file.php?param1=x&param2=http://www.someurl.com , param2 does appear OK so it must be something with the rewrite rule.
You need to grab the value from THE_REQUEST:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sub/[0-9]+/([^?\ ]+)
RewriteRule ^sub/([0-9]+)/ sub/file.php?param1=$1&param2=%1 [L]

Resources