How to escape/encode characters in .htaccess RewriteRule
I want the Accept-Language
header to be available in JavaScript, therefore in my .htaccess
I set the following rule:
RewriteRule .* - [CO=HTTP-Accept-Language:%{HTTP:Accept-Language}:%{HTTP_HOST}:0:/]
This works, but not as anticipated.
The cookie is set as following:
Set-Cookie: HTTP-Accept-Language=en-US,en;q=0.9; path=/; domain=XXXXXX.XX
Since the cookie value contains ;
character, JavaScript's document.cookie
trims this value to
en-US,en
How can I force RewriteRule
to escape\encode deliminator characters (;
and space) to ensure that the entire cookie value is properly recorded?
do you know?
how many words do you know
See also questions close to this topic
-
how to change prettier format for react native
my code formatting prettier didn't works well for react native, i don't understand where to config it but it works well with flutter
from this code
import { View, Text } from 'react-native' import React from 'react' export default function App() { return ( <View> <Text>Apps</Text> </View> ) }
it's formatted to this
import { View, Text } from 'react-native' import React from 'react' export default function App() { return ( < View > < Text > Apps < /Text> < /View> ) }
-
MarkLogic server-side JavaScript and XQuery
I am just starting using NoSQL MarkLogic DB and trying to choose for me the best query language to learn and use in future. On server side MarkLogic provides the possibility to use JavaScript API or XQuery API.
I want to receive an advice. Which language is better to concentrate on and learn? JavaScript or XQuery?
- Popover in chrome extension using js
-
Having issues with multi virtual hosts with ssl
In the first run my first virtual host was successfully running well with SSL however when I tried to do it on second one it is not working anymore First I edit apache
httpd-xampp.conf
and added this, So basically based on this article: Using Multiple SSL Certificates in Apache with One IP Address<VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/proteksyon" ServerAdmin admin@proteksyon.ph ServerName proteksyon.ph ServerAlias *.proteksyon.ph SSLEngine On SSLCertificateFile "crt/proteksyon.ph/server.crt" SSLCertificateKeyFile "crt/proteksyon.ph/server.key" <Directory "C:/xampp/htdocs/proteksyon"> AllowOverride All Order Allow,Deny Allow from all Require all granted </Directory> </VirtualHost> <VirtualHost *:443> DocumentRoot "C:/xampp/htdocs/proteksyon" ServerAdmin admin@proteksyon.ph ServerName proteksyon.ph ServerAlias *.proteksyon.ph SSLEngine On SSLCertificateFile "crt/proteksyon.ph/server.crt" SSLCertificateKeyFile "crt/proteksyon.ph/server.key" <Directory "C:/xampp/htdocs/proteksyon"> AllowOverride All Order Allow,Deny Allow from all Require all granted </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/clique" ServerAdmin support@clique.me ServerName clique.me ServerAlias clique.me SSLEngine On SSLCertificateFile "crt/clique.me/server.crt" SSLCertificateKeyFile "crt/clique.me/server.key" <Directory "C:/xampp/htdocs/clique"> AllowOverride All Order Allow,Deny Allow from all Require all granted </Directory> </VirtualHost> <VirtualHost *:443> DocumentRoot "C:/xampp/htdocs/clique" ServerAdmin support@clique.me ServerName clique.me ServerAlias clique.me SSLEngine On SSLCertificateFile "crt/clique.me/server.crt" SSLCertificateKeyFile "crt/clique.me/server.key" <Directory "C:/xampp/htdocs/clique"> AllowOverride All Order Allow,Deny Allow from all Require all granted </Directory> </VirtualHost>
And this is the inputs for my
hosts
file:127.0.0.1 clique.me 127.0.0.1 proteksyon.ph
When accessing the
proteksyon.ph
everything is good and SSL is working fine it access the file of proteksyon and htdocs. When it comes to accessing/browsing the clique.me it throw this kind of error: -
HTTP authentication with PHP for specific pages
I have been trying to fix one issue for the last two weeks but didn't find any solution to my problem. So, I am trying to add HTTP authentication with PHP 7.4.
The main issue
I have to add restrictions for specific URLs ( For example, https://example.com/photos ) with PHP, and I am using the below code. But the problem is I have to show logged-in users on other pages, too. For example, if I didn't log in, then anything to show, but if I am logged in need to show username and password on all pages, but again I have to restrict only specific pages.
PHP Code:
Header("WWW-Authenticate: Basic realm='Members Only' valid-user"); http_response_code(401); echo "<script> window.location.replace( 'https://example.com/sign-up' ) </script>"; exit;
.htaccess Code:
AuthUserfile PATH_TO_THE_FILE/.htpasswd AuthName "Members Only" AuthType Basic AuthBasicProvider file
I hope someone will help me soon.
-
htaccess how to prioritize RewriteCond
We recently migrate our new website and we need to adjust htaccess to avoid 404 error with the search engine,
The website was a Multilanguage site we decided to use session and cookie to manage multi language instead of string.
We put together htaccess to make the changes
I have my htaccess code below
RewriteEngine On RewriteRule ^([^/]+)/fr/?(.*)$ /$1/$2 [R=301,NE,L] RewriteRule ^([^/]+)/en/?(.*)$ /$1/$2 [R=301,NE,L] RewriteRule ^([^/]+)/es/?(.*)$ /$1/$2 [R=301,NE,L] RewriteRule ^([^/]+)/pt/?(.*)$ /$1/$2 [R=301,NE,L] RewriteRule ^([^/]+)/ar/?(.*)$ /$1/$2 [R=301,NE,L] RewriteRule ^/?ar(.*)$ /$1 [L,R=301] RewriteRule ^/?pt(.*)$ /$1 [L,R=301] RewriteRule ^/?es(.*)$ /$1 [L,R=301] RewriteRule ^/?fr(.*)$ /$1 [L,R=301] RewriteRule ^/?en(.*)$ /$1 [L,R=301] RewriteRule ^/?location(.*)/(.*).html$ /newlocation$1/$2.html$3 [R=301,NE,L] RewriteRule ^/?ads/location(.*)/(.*).html$ /ads/newlocation$1/$2.html$3 [R=301,NE,L] RewriteRule ^/?page(.*)/(.*).html$ /newpage$1/$2.html$3 [R=301,NE,L] RewriteRule ^/?ads/page(.*)/(.*).html$ /ads/newpage$1/$2.html$3 [R=301,NE,L]
this code above works great ! but when I add this code below,
RewriteCond %{REQUEST_URI} !^/ads/ RewriteRule ^([^/]+)\.html?(.*)$ /ads/$1.html$2 [L,QSA]
Nothing is working anymore. Is there anything wrong with this?
-
Apache 2.4 never runs RewriteMap program
Since JavaScript cannot access HTTP header
Accept-Language
, I decided to pass this information to a custom cookie using .htaccess as follows:RewriteRule .* - [CO=HTTP-Accept-Language:%{HTTP:Accept-Language}:%{HTTP_HOST}:1:/]
However, I quickly realized that cookies values are not allowed to use some "special" characters, including semicolon, which is used within
Accept-Language
header.Therefore, I decided to use a simple RewriteMap program written in Perl to replace semicolons with underscore characters.
#!/usr/bin/perl $| = 1; while (<STDIN>) { s/;/_/g; print $_; }
In my httpd.conf I defined the RewriteMap with
LogLevel trace8 <IfModule rewrite_module> RewriteEngine on Mutex default RewriteMap lc int:tolower RewriteMap uc int:toupper RewriteMap escape int:escape RewriteMap myprg "prg:/etc/httpd/rewrite/myprg.pl" </IfModule>
and to test these RewriteMap functions, I modified my .htaccess as follows:
RewriteEngine On RewriteBase / RewriteRule .* - [CO=Test1:${uc:%{HTTP:Accept-Language}}:%{HTTP_HOST}:1:/] RewriteRule .* - [CO=Test2:${myprg:%{HTTP:Accept-Language}}:%{HTTP_HOST}:1:/]
Unfortunately, while uc RewriteMap function works fine, the external myprg doesn't.
With
LogLevel trace8
in place, I expected some useful information in log file. But all I found about this external script was this:map lookup OK: map=myprg key=en-US,en;q=0.9 -> val=
No error, just information that val is empty. Based on this finding, I modified my Perl script to include some internal logging:
#!/usr/bin/perl use Cwd qw(abs_path); use File::Spec; use POSIX; $| = 1; my (undef, $directory, $filename) = File::Spec->splitpath(abs_path($0)); open my $file, '>>:encoding(UTF-8)', $directory . ($filename =~ s/(?:\.[^.]*)$/.log/r); print {$file} (strftime "%F %T", localtime time) . "\tRewriteMap script $filename started\n"; close $file; while (<STDIN>) { s/;/?/g; print $_; }
After each change, I obviously also restarted Apache server by
systemctl restart httpd
Then I realized that my Apache 2.4.6 (CentOS) never run the external RewriteMap script myprg.
I spent hours researching a possible problem on my side, but I didn't find anything wrong in my configuration files. What am I doing wrong? Why do I see no error, no warning, despite the script is never called?
-
htaccess ErrorDocument not working with when removing php extension
.htaccess
# Security Headers <IfModule mod_headers.c> Header set X-XSS-Protection "1; mode=block" Header set X-Frame-Options "SAMEORIGIN" Header set X-Content-Type-Options "nosniff" Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains" # Header set Content-Security-Policy ... Header set Referrer-Policy "same-origin" Header set Feature-Policy "geolocation 'self'; vibrate 'none'" </IfModule> # Index.php default RewriteEngine On DirectoryIndex index.php # Remove .php extension RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] # Force HTTPS RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Error document handling ErrorDocument 404 https://landon.pw/404 ErrorDocument 403 https://landon.pw/404 Options -Indexes
If I try landon.pw/a/, it will redirect to the 404. If I try landon.pw/a, it won't.
I believe the issue is because the way I am removing the php extension. When they go to /a, it is trying to serve a.php, which I guess is why it won't redirect files to the 404. I just don't know how to circumvent this problem.
-
IIS Web.config Rewrite rules - Redirect rules with - or + chars
https://someserver.com/path01/2011/abc+and+def/88722 https://someserver.com/path01/2011/abc-and-def/88722
I have several URLs that I need to create a redirect rule. I need to remove "2011" from the url and change the URL... I got most of it working but I can't figure out how the "-" or "+" needs to be written and how to combine into one rule
<rule name="Redirection 1" stopProcessing="true"> <match url="test-catalog/([0-9]*)//abc-and-def/([0-9]*)"/> <action type="Redirect" url="https://someserver.com/path01/overview/{R:2}#/abc-and-def"/> </rule> <rule name="Redirection 2" stopProcessing="true"> <match url="test-catalog/([0-9]*)//abc+and+def/([0-9]*)"/> <action type="Redirect" url="https://someserver.com/path01/overview/{R:2}#/abc-and-def"/> </rule>
I tried to use this use rule but it's not working !!
<rule name="Redirection rule " stopProcessing="true"> <match url="test-catalog/([0-9]*)//abc([-+]*)and([-+]*)def/([0-9]*)"/> <action type="Redirect" url="https://someserver.com/path01/overview/{R:4}#/abc-and-def"/> </rule>
Any ideas on how to solve this issue?