Home > Net >  Rewritemap to url with spaces
Rewritemap to url with spaces

Time:07-19

I'm trying to:

  • give access to file in folder if query string value exist in map file
  • disable access if query string value is empty or no query string at all

With the following it works except for files with spaces: london bridge footage.mp4

/etc/httpd/extra/httpd-ssl.conf

<VirtualHost *:443>

... truncated ssl certificate ...

RewriteEngine On

# Rewritemap:
RewriteMap filehash txt:file_hash.txt

# Query string equal to key=digits lookup in map
# redirect to /404 on fail else redirect to match in map
RewriteCond %{QUERY_STRING} ^(key=)([0-9]*)
RewriteRule ^/test/video/(.*)$ ${filehash:%2|/404}? [PT,NE,L]

# Query string empty: key=
RewriteCond %{QUERY_STRING} !^key= $
RewriteRule ^/test/video/(.*)$ - [R=404,L]

# No query string
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/test/video/(.*)$ - [R=404,L]

</VirtualHost>

file_hash.txt

1001 /test/video/london_bridge_footage.mp4
1002 /test/video/london bridge footage.mp4
1003 /test/video/london bridge footage.mp4
1004 /test/video/london\ bridge\ footage.mp4

EDIT:

added more information about my configuration, i have Codeigniter 4 running in up folder /test/ who use rewrite rules

/etc/httpd/httpd.conf

<Directory "/var/www/test">
   LogLevel alert rewrite:trace6
   DirectoryIndex index.php
   AllowOverride All
   Require all granted
</Directory>

/var/www/test/.htaccess

# Disable directory browsing
Options All -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
    Options  FollowSymlinks
    RewriteEngine On

    # If you installed CodeIgniter in a subfolder, you will need to
    # change the following line to match the subfolder you need.
    # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
    RewriteBase /test

    # Rewrite CSS / JS "theme.1603357209.css -> theme.css"
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(. )\.\d (\.(?:js|css))$ $1$2

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (. )/$
    RewriteRule ^ %1 [L,R=301]

    # Rewrite "www.example.com -> example.com"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(. )$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to the front controller, index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

    # Ensure Authorization header is passed along
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

</IfModule>

# Add cache control header one year for CSS and JS files
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType text/css                  "access plus 1 year"
    ExpiresByType application/javascript    "access plus 1 year"
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 index.php
</IfModule>

EDIT 2:

rewritelog for a working file:


[Mon Jul 18 22:28:44.436405 2022] [core:notice] [pid 2002:tid 140071990364096] AH00094: Command line: '/usr/sbin/httpd'
[Mon Jul 18 22:28:49.518993 2022] [rewrite:trace3] [pid 26321:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42860] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40139e0/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/video/london_bridge_footage.mp4 -> video/london_bridge_footage.mp4
[Mon Jul 18 22:28:49.519018 2022] [rewrite:trace3] [pid 26321:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42860] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40139e0/initial] [perdir /var/www/test/] applying pattern '^(. )\\.\\d (\\.(?:js|css))$' to uri 'video/london_bridge_footage.mp4'
[Mon Jul 18 22:28:49.519025 2022] [rewrite:trace3] [pid 26321:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42860] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40139e0/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/video/london_bridge_footage.mp4 -> video/london_bridge_footage.mp4
[Mon Jul 18 22:28:49.519045 2022] [rewrite:trace3] [pid 26321:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42860] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40139e0/initial] [perdir /var/www/test/] applying pattern '^' to uri 'video/london_bridge_footage.mp4'
[Mon Jul 18 22:28:49.519060 2022] [rewrite:trace3] [pid 26321:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42860] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40139e0/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/video/london_bridge_footage.mp4 -> video/london_bridge_footage.mp4
[Mon Jul 18 22:28:49.520125 2022] [rewrite:trace3] [pid 26321:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42860] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40139e0/initial] [perdir /var/www/test/] applying pattern '^' to uri 'video/london_bridge_footage.mp4'
[Mon Jul 18 22:28:49.520143 2022] [rewrite:trace3] [pid 26321:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42860] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40139e0/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/video/london_bridge_footage.mp4 -> video/london_bridge_footage.mp4
[Mon Jul 18 22:28:49.520146 2022] [rewrite:trace3] [pid 26321:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42860] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40139e0/initial] [perdir /var/www/test/] applying pattern '^([\\s\\S]*)$' to uri 'video/london_bridge_footage.mp4'
[Mon Jul 18 22:28:49.520161 2022] [rewrite:trace3] [pid 26321:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42860] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40139e0/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/video/london_bridge_footage.mp4 -> video/london_bridge_footage.mp4
[Mon Jul 18 22:28:49.520165 2022] [rewrite:trace3] [pid 26321:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42860] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40139e0/initial] [perdir /var/www/test/] applying pattern '.*' to uri 'video/london_bridge_footage.mp4'
[Mon Jul 18 22:28:49.520170 2022] [rewrite:trace1] [pid 26321:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42860] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40139e0/initial] [perdir /var/www/test/] pass through /var/www/test/video/london_bridge_footage.mp4

A file that fail

[Mon Jul 18 22:32:19.837894 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/video/london bridge footage.mp4 -> video/london bridge footage.mp4
[Mon Jul 18 22:32:19.837918 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] applying pattern '^(. )\\.\\d (\\.(?:js|css))$' to uri 'video/london bridge footage.mp4'
[Mon Jul 18 22:32:19.837924 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/video/london bridge footage.mp4 -> video/london bridge footage.mp4
[Mon Jul 18 22:32:19.837927 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] applying pattern '^' to uri 'video/london bridge footage.mp4'
[Mon Jul 18 22:32:19.837942 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/video/london bridge footage.mp4 -> video/london bridge footage.mp4
[Mon Jul 18 22:32:19.837945 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] applying pattern '^' to uri 'video/london bridge footage.mp4'
[Mon Jul 18 22:32:19.837948 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/video/london bridge footage.mp4 -> video/london bridge footage.mp4
[Mon Jul 18 22:32:19.837951 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] applying pattern '^([\\s\\S]*)$' to uri 'video/london bridge footage.mp4'
[Mon Jul 18 22:32:19.837956 2022] [rewrite:trace2] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] rewrite 'video/london bridge footage.mp4' -> 'index.php/video/london bridge footage.mp4'
[Mon Jul 18 22:32:19.837966 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] add per-dir prefix: index.php/video/london bridge footage.mp4 -> /var/www/test/index.php/video/london bridge footage.mp4
[Mon Jul 18 22:32:19.837970 2022] [rewrite:trace2] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] trying to replace prefix /var/www/test/ with /test
[Mon Jul 18 22:32:19.837973 2022] [rewrite:trace1] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f40128a0/initial] [perdir /var/www/test/] internal redirect with /test/index.php/video/london bridge footage.mp4 [INTERNAL REDIRECT]
[Mon Jul 18 22:32:19.838010 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] add path info postfix: /var/www/test/index.php -> /var/www/test/index.php/video/london bridge footage.mp4
[Mon Jul 18 22:32:19.838015 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/index.php/video/london bridge footage.mp4 -> index.php/video/london bridge footage.mp4
[Mon Jul 18 22:32:19.838018 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] applying pattern '^(. )\\.\\d (\\.(?:js|css))$' to uri 'index.php/video/london bridge footage.mp4'
[Mon Jul 18 22:32:19.838022 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] add path info postfix: /var/www/test/index.php -> /var/www/test/index.php/video/london bridge footage.mp4
[Mon Jul 18 22:32:19.838025 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/index.php/video/london bridge footage.mp4 -> index.php/video/london bridge footage.mp4
[Mon Jul 18 22:32:19.838027 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] applying pattern '^' to uri 'index.php/video/london bridge footage.mp4'
[Mon Jul 18 22:32:19.838046 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] add path info postfix: /var/www/test/index.php -> /var/www/test/index.php/video/london bridge footage.mp4
[Mon Jul 18 22:32:19.838049 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/index.php/video/london bridge footage.mp4 -> index.php/video/london bridge footage.mp4
[Mon Jul 18 22:32:19.838054 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] applying pattern '^' to uri 'index.php/video/london bridge footage.mp4'
[Mon Jul 18 22:32:19.838057 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] add path info postfix: /var/www/test/index.php -> /var/www/test/index.php/video/london bridge footage.mp4
[Mon Jul 18 22:32:19.838062 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/index.php/video/london bridge footage.mp4 -> index.php/video/london bridge footage.mp4
[Mon Jul 18 22:32:19.838065 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] applying pattern '^([\\s\\S]*)$' to uri 'index.php/video/london bridge footage.mp4'
[Mon Jul 18 22:32:19.838070 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] add path info postfix: /var/www/test/index.php -> /var/www/test/index.php/video/london bridge footage.mp4
[Mon Jul 18 22:32:19.838074 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/index.php/video/london bridge footage.mp4 -> index.php/video/london bridge footage.mp4
[Mon Jul 18 22:32:19.838076 2022] [rewrite:trace3] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] applying pattern '.*' to uri 'index.php/video/london bridge footage.mp4'
[Mon Jul 18 22:32:19.838081 2022] [rewrite:trace1] [pid 26391:tid 140071799514688] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400cc00/initial/redir#1] [perdir /var/www/test/] pass through /var/www/test/index.php
[Mon Jul 18 22:32:20.061135 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/assets/dist/js/theme.1658174761.js -> assets/dist/js/theme.1658174761.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061153 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] applying pattern '^(. )\\.\\d (\\.(?:js|css))$' to uri 'assets/dist/js/theme.1658174761.js', referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061170 2022] [rewrite:trace2] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] rewrite 'assets/dist/js/theme.1658174761.js' -> 'assets/dist/js/theme.js', referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061174 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] add per-dir prefix: assets/dist/js/theme.js -> /var/www/test/assets/dist/js/theme.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061177 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/assets/dist/js/theme.js -> assets/dist/js/theme.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061180 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] applying pattern '^' to uri 'assets/dist/js/theme.js', referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061197 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/assets/dist/js/theme.js -> assets/dist/js/theme.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061201 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] applying pattern '^' to uri 'assets/dist/js/theme.js', referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061205 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/assets/dist/js/theme.js -> assets/dist/js/theme.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061208 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] applying pattern '^([\\s\\S]*)$' to uri 'assets/dist/js/theme.js', referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061213 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/assets/dist/js/theme.js -> assets/dist/js/theme.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061217 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] applying pattern '.*' to uri 'assets/dist/js/theme.js', referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061225 2022] [rewrite:trace2] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] trying to replace prefix /var/www/test/ with /test, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061228 2022] [rewrite:trace1] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f4017a10/initial] [perdir /var/www/test/] internal redirect with /test/assets/dist/js/theme.js [INTERNAL REDIRECT], referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061246 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400b7d0/initial/redir#1] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/assets/dist/js/theme.js -> assets/dist/js/theme.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061251 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400b7d0/initial/redir#1] [perdir /var/www/test/] applying pattern '^(. )\\.\\d (\\.(?:js|css))$' to uri 'assets/dist/js/theme.js', referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061255 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400b7d0/initial/redir#1] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/assets/dist/js/theme.js -> assets/dist/js/theme.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061258 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400b7d0/initial/redir#1] [perdir /var/www/test/] applying pattern '^' to uri 'assets/dist/js/theme.js', referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061271 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400b7d0/initial/redir#1] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/assets/dist/js/theme.js -> assets/dist/js/theme.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061275 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400b7d0/initial/redir#1] [perdir /var/www/test/] applying pattern '^' to uri 'assets/dist/js/theme.js', referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061278 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400b7d0/initial/redir#1] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/assets/dist/js/theme.js -> assets/dist/js/theme.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061281 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400b7d0/initial/redir#1] [perdir /var/www/test/] applying pattern '^([\\s\\S]*)$' to uri 'assets/dist/js/theme.js', referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061289 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400b7d0/initial/redir#1] [perdir /var/www/test/] strip per-dir prefix: /var/www/test/assets/dist/js/theme.js -> assets/dist/js/theme.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061293 2022] [rewrite:trace3] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400b7d0/initial/redir#1] [perdir /var/www/test/] applying pattern '.*' to uri 'assets/dist/js/theme.js', referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003
[Mon Jul 18 22:32:20.061299 2022] [rewrite:trace1] [pid 26391:tid 140071791121984] mod_rewrite.c(486): [client 192.168.1.254:42862] 192.168.1.254 - - [my-domain.org/sid#5561d7dc23b8][rid#7f64f400b7d0/initial/redir#1] [perdir /var/www/test/] pass through /var/www/test/assets/dist/js/theme.js, referer: https://my-domain.org/test/video/london bridge footage.mp4?key=1003

Maybe related to this here ?

EDIT 3

Following Anubhava advices i copied his .htaccess but it did not work, files was reachable with or without query string, i have edited as follow this two lines:

# with   instead of * ?key= return the file
RewriteCond %{QUERY_STRING} ^key=(\d*) [NC]
# Without ? internal server error
RewriteRule ^video/ ${filehash:%1}? [L,NC]

What is working:

  • map works fine with all type of files, with or without spaces if a query string ?key=digits is provided.
  • if query string is ?key= or ?key=0000 no file is returned
  • if not matching map the front controller return a 404

What is not working:

  • the file is downloadable if i use any uri between file extension and query string equal sign: https://my-site.org/test/video/london bridge footage.mp4?key

I have tested plenty of variations with no luck the last past few hours.

Httpd is 2.4.54

Help appreciated

EDIT 4:

it is now working, thank you so much to Anubhava for his help, the solution was inside another post that was solved by Anubhava

# Query string equal to key=digits lookup in map 
RewriteCond %{QUERY_STRING} ^key=(\d*) [NC]
RewriteRule ^video/ ${filehash:%1}? [L,NC,NE]

# for video URL if key=<digits> is not present then 404
RewriteCond %{QUERY_STRING} !(^|&)key=(\d ) [NC]
# prevent looping from internal redirects
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^video/ /404/ [L,NC]

EDIT 5

In the same time Anubhava provided another working solution see accepted answer.

CodePudding user response:

I suggest you remove every rule from vhost except RewriteMap and keep all the rules in one place i.e. test/.htaccess as it already exists in your file system.

/etc/httpd/extra/httpd-ssl.conf:

<VirtualHost *:443>

... truncated ssl certificate ...

RewriteEngine On
# Rewritemap:
RewriteMap filehash txt:file_hash.txt
</VirtualHost>

/test/.htaccess:

# Disable directory browsing
Options All -Indexes
# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------
# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
Options  FollowSymlinks
RewriteEngine On

# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
RewriteBase /test/

# Rewrite CSS / JS "theme.1603357209.css -> theme.css"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(. )\.\d (\.(?:js|css))$ $1$2 [L,NC]

# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (. )/$
RewriteRule ^ %1 [L,R=301,NE]

# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(. )$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]


# for video URL if key=<digits> is not present then 404
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} !(^|&)key=(\d ) [NC]
RewriteRule ^video/ /404/ [L,NC]

# Query string equal to key=digits lookup in map
# redirect to /404 on fail else redirect to match in map
RewriteCond %{QUERY_STRING} ^key=(\d ) [NC]
RewriteRule ^video/ ${filehash:%1|/404/}? [L,NC]

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php%{REQUEST_URI} [L]

# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Add cache control header one year for CSS and JS files
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType text/css                  "access plus 1 year"
    ExpiresByType application/javascript    "access plus 1 year"
</IfModule>

Also you should not use R=404 rules because index.php is acting as front controller that handled all non-files, non-directory requests anyway.

  • Related