Apache RewriteRule pass entire URL as a parameter - apache

Currently my Apache RewriteRule only passes the path of the original URL as a query parameter to the rewritten URL.
How do I send the entire URL (including scheme and authority etc) as a parameter to the rewritten URL?
I know %{REQUEST_URI} only passes the path, and I can’t see any Apache environment variables that do pass the entire URL.
I’m something of a beginner to Apache configuration so excuse any ignorance on my part,
thanks!
Here’s my current config:
#
# Enable Rewrite
#
RewriteEngine On
#
# Condition for rewriting the URL. Check the URL's path is a valid REST URI path
#
RewriteCond %{REQUEST_URI} ^(?:[^?#]*)(?:/v[0-9]+(?:\.[0-9]+)*)(?:/[a-z]+)(?:/[a-z]+)(?:/?[^/?#]*)(?:[^?#]*)$
#
# Rewrite the URL, sending the REST path as a parameter to the specified version.
#
RewriteRule ^(?:[^?#]*)(?:/(v[0-9]+(?:\.[0-9]+)*))((?:/[a-z]+)(?:/[a-z]+)(?:/?[^/?#]*)(?:[^?#]*))$ https://localhost/rest/$1/index.php?request_uri_path=https://localhost/rest/$1/$2 [QSA,NC,L,R=307]
Currently I’ve hardcoded the url into the query parameters so the URL
https://localhost/rest/v1/users/show/12345
becomes:
https://localhost/rest/v1/index.php?request_uri_path=https://localhost/rest/v1/users/show/12345

You can use this rule to get full URL as query parameter:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ index.php?request_uri_path=http%1://%{HTTP_HOST}%{REQUEST_URI} [L,QSA]
If you're on Apache 2.4 then you can make use of %{REQUEST_SCHEME} variable:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php?request_uri_path=%{REQUEST_SCHEME}://%{HTTP_HOST}%{REQUEST_URI} [L,QSA]

Related

Dynamically pointing multiple domains to a single domain's subfolder with .htaccess

In this example, I manage domain.com. Inside it, I have a store template in php that loads the selected store dinamically:
domain.com/store1
domain.com/store2
domain.com/store3
The nice urls are being generated by the .htaccess below. What's really being loaded in the back, is this:
domain.com/store/index.php?store=store1
domain.com/store/index.php?store=store2
domain.com/store/index.php?store=store3
Each store has internal links, such as these:
domain.com/store1/catalogue
domain.com/store1/catalogue/instruments
domain.com/store1/catalogue/instruments/guitars
domain.com/store1/electric-guitar/1337
It works exactly as expected. These are the .htaccess rules to make that work in domain.com. The store query string (store1, store2, store3) in each RewriteRule determines which store is being loaded:
# permalinks
RewriteEngine on
# errors
ErrorDocument 404 /error.php?error=404
# ignore existing directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
#################################################
# store
RewriteRule ^([0-9a-zA-Z-]{2,50})/?$ store/index.php?store=$1 [QSA,L]
# store: catalogue
RewriteRule ^([0-9a-zA-Z-]{2,50})/catalogue/?$ store/catalogue.php?store=$1 [QSA,L]
RewriteRule ^([0-9a-zA-Z-]{2,50})/catalogue/([0-9a-zA-Z-]{1,75})/?$ store/catalogue.php?store=$1&cat=$2 [QSA,L]
RewriteRule ^([0-9a-zA-Z-]{2,50})/catalogue/([0-9a-zA-Z-]{1,75})/([0-9a-zA-Z-]{1,75})/?$ store/catalogue.php?store=$1&cat=$2&subcat=$3 [QSA,L]
# store: products
RewriteRule ^([0-9a-zA-Z-]{2,50})/([0-9a-zA-Z-]{1,150})/([0-9]+)$ store/product.php?store=$1&slug=$2&id_product=$3 [QSA,L]
Now, here comes the tricky part. Some of the clients, would like to use their own domains, so that store1.com/* loads the same content of domain.com/store1/* (without using a redirect).
Example:
store1.com => domain.com/store1/
store1.com/catalogue => domain.com/store1/catalogue
store1.com/catalogue/instruments => domain.com/store1/catalogue/instruments
store1.com/catalogue/instruments/guitars => domain.com/store1/catalogue/instruments/guitars
store1.com/electric-guitar/1337 => domain.com/store1/electric-guitar/1337
You get the idea.
All the domains (domain.com, store1.com, store2.com, store3.com) are configured in the same Apache Web Server environment (using virtual hosts).
The question is: Can this be implemented in a .htaccess file in each one of the store's domain root path dynamically or inside the virtualhost .conf file? If so, how?
Make sure all your custom "store" domains (eg. store1.com, store2.com, etc.) are defined as ServerAlias in the domain.com vHost and so resolve to the same place as domain.com.
You can then rewrite requests for the custom store domain to prefix the URL-path with the store-id (the domain name) and then use your existing rules unaltered.
For example, a request for store1.com/catalogue/instruments is internally rewritten to /store1/catalogue/instruments and then processed by the existing rules as usual.
The following directives should go before the existing # store rules:
# Internally rewrite the request when a custom domain is used
# - the URL-path is prefixed with the domain name
RewriteCond %{HTTP_HOST} !^(www\.)?domain\. [NC]
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteCond %{REQUEST_URI}#%1 !^/([^/]+)/.*#\1
RewriteRule (.*) %1/$1
And that's basically it, although you may also decide to implement a canonical redirect - see below.
Explanation of the above directives:
The first condition simply excludes requests for the main domain.com (which should already have the relevant store-id prefixed to the URL-path).
%{REQUEST_URI} !\.\w{2,4}$ - The second condition avoids rewriting requests for static resources (images, JS, CSS, etc.). Specifically, it excludes any request that ends in - what looks like - a file extension.
%{HTTP_HOST} ^([^.]+) - The third condition captures the requested domain name before the TLD which is then accessible using the %1 backreference later and used to prefix the URL-path. This assumes there is no www subdomain (as stated in comments). eg. Given a request for store1.com or store2.co.uk, store1 or store2 respectively are captured.
%{REQUEST_URI}#%1 !^/([^/]+)/.*#\1 - The fourth condition checks that the URL-path is not already prefixed with the domain name (captured above). This is primarily to ensure that rewritten requests are not rewritten again, causing a rewrite loop. This is achieved using an internal backreference (\1) that compares the first path-segment in the URL-path against the previously captured domain name (%1).
(.*) %1/$1 - Finally, the request is internally rewritten, prefixing the domain name to the requested URL-path.
Ignore existing files (may not be required)
# ignore existing directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
You've not stated how you are referencing your static resources (images, JS, CSS, etc.), but you may need to modify this rule to also match requests for existing files. (Although the condition I added to the rule above may already be sufficient to exclude these requests.) For example:
# ignore existing directories or files
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
Canonical redirect (optional)
You should also consider redirecting any direct requests of the form store1.com/store1/catalogue/instruments back to the canonical URL store1.com/catalogue/instruments - should these URLs ever be exposed/discovered. A request of the form store1.com/store2/catalogue/instruments will naturally result in a 404, so is not an issue.
For example, the following would go immediately after the ErrorDocument directive in your existing rules:
# Redirect to remove the "/store1" URL-prefix when the "store1.com" domain is requested.
# - Only applies to direct requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^domain\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteCond %{REQUEST_URI}#%1 ^/([^/]+)/.*#\1
RewriteRule ^(?:[^/]+)(/.*) $1 [R=301,L]
Test first with a 302 (temporary) redirect to avoid potential caching issues.
This is basically the reverse of the above rewrite, but applies to direct requests only. The check against the REDIRECT_STATUS env var ensures that only direct requests from the client and not rewritten requests by the later rewrite are processed.
Summary
With the two rule blocks in place...
# permalinks
RewriteEngine on
# errors
ErrorDocument 404 /error.php?error=404
# Redirect to remove the "/store1" URL-prefix when the "store1.com" domain is requested.
# - Only applies to direct requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^domain\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteCond %{REQUEST_URI}#%1 ^/([^/]+)/.*#\1
RewriteRule ^(?:[^/]+)(/.*) $1 [R=301,L]
# ignore existing directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Internally rewrite the request when a custom domain is used
# - the URL-path is prefixed with the domain name
RewriteCond %{HTTP_HOST} !^(www\.)?domain\. [NC]
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteCond %{REQUEST_URI}#%1 !^/([^/]+)/.*#\1
RewriteRule (.*) %1/$1
#################################################
# store
:
: existing directives follow
:

Mod Rewirte for CalDav and CardDav urls in Owncloud

I want to shorten my owncloud caldav and carddav urls.
currently the urls are :
https://site.mysite.com/owncloud/remote.php/caldav/principals/edison/
https://site.mysite.com/owncloud/remote.php/carddav/addressbooks/edison/contacts
Note : The name edison and what ever follows after that is dynamic it will change depending on the users.
I want it to be
https://site.mysite.com/caldav/principals/edison/
https://site.mysite.com/carddav/addressbooks/edison/contacts
.htaccess file contains
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !\.sso/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Eg:
If the user request for https://site.mysite.com/caldav/principals/edison/ . Then it should redirect to https://site.mysite.com/owncloud/remote.php/caldav/principals/edison/
How can it be done using mod_rewrite ?
mod_rewrite Prepend Path to Requested URL with Dynamic Portion
Starting at server root, enter the requested URL path in the RewriteRule "pattern" parameter, and the desired path in the "substitution" parameter. In the form:
RewriteRule pattern substitution [flags]
In this case:
RewriteRule ^caldav/principals/edison/$ owncloud/remote.php/caldav/principals/edison/ [L]
If a portion of the URL (between slashes) varies and you don't want to (or can't) write a rule for every situation then use the regular expression ([^/]+) to capture the dynamic portion and inject it into your substituted path using the RE capture variable $1:
RewriteRule ^caldav/principals/([^/]+)/$ owncloud/remote.php/caldav/principals/$1/ [L]
The first set of parenthesis is $1, the second set is $2, etc. And capturing parenthesis can be nested.
Put the more specific rules higher in the rules list, and more general rules lower in the list. So I suggest putting this rule first, right after RewriteBase /.

mod_rewrite condition for existing folder

I'm trying to set-up an .htaccess file that will pass every request URL as GET into a file called index.php. The only exception is, when the request URL points to a directory res.
Examples:
/wiki/cool-stuff => index.php?uri=/wiki/cool-stuff
/wiki/cool-stuff?news=on => index.php?uri=/wiki/cool-stuff&news=on
/res/cool-photo.jpg => res/cool-photo.jpg
Two problems:
The GET variable passed to /wiki/cool-stuff in the second example is not passed to index.php
Accessing /res (not /res/!!) suddenly shows me /res/?uri=res in my browser and index.php with uri=res/. Accessing /res/ instead, shows me index.php with uri=res/ and the URL in the browser stays (which is okay).
The .htaccess:
RewriteEngine on
RewriteBase /subthing/
RewriteCond %{REQUEST_URI} !/res/(.+)$
RewriteCond %{REQUEST_URI} !index.php
RewriteRule (.*) index.php?uri=$1
How can I achieve the desired behaviour?
Try using the Query-String-Append flag, QSA
Make the trailing slash optional - in Regex, this is achieved by adding ?.
New .htaccess:
RewriteEngine on
RewriteBase /subthing/
RewriteCond %{REQUEST_URI} !/res(/.*)?$
RewriteCond %{REQUEST_URI} !index.php
RewriteRule (.*) index.php?uri=$1 [QSA]
Note that I have tweaked the Regex on the /res folder to cause /resabc to be redirected (if the slash was the only optional piece, anything beginning with res would match.
Apache Mod_Rewrite Documentation

Apache rewrite to 404 except for matched query

I'm trying to handle logically simple operation, but apache rewrite does not handle my website's query string requests. I heard it does not do it eventually. Still cannot understand its regex syntax.
RewriteEngine on
RewriteBase /
#RewriteCond %{QUERY_STRING} ^\?category=([a-z])$ // WHY DO I NEED THIS?
RewriteRule ^!(?category=[a-z]+|?do=[a-z]+)$ [R=404,L,NC]
Goal is to send the client to 404 for any query NOT matching /?category=[a-z]+ and /?do=[a-z]+. It would be much better if it's possible to load patterns from the text file.
The Apache documentation is poor and Google does not help as well.
Don't include the query string inside RewriteRule. It is matched in RewriteCond.
RewriteEngine on
RewriteBase /
# For js & css
# Normally this would be done with:
# RewriteCond %{REQUEST_FILENAME} !-f
# But since you might have files here you're trying to put a 404 on,
# we'll do it differently, matching css & js files
RewriteCond %{REQUEST_FILENAME} !^(.*)\.(css|js)$
#Conditions check if either query param is missing (negated with !)
RewriteCond %{QUERY_STRING} !category=([a-z]+)
RewriteCond %{QUERY_STRING} !do=([a-z]+)
# Rewrite anything not matched by the querystring conditions above...
# Edit -- was missing the rewrite target ( - )
RewriteRule ^(.*)$ - [R=404,L]

.htaccess Issues, Virtual Subdomains & Codeigniter

basically my problem is this. I have set up a system that accepts wildcard subdomains, and rewrites them to a single CodeIgniter installation. There is a controller called dispatcher that takes the arguments that it requires, as well as being passed the subdomain being used in the URL.
My problem is this: I need to create RewriteRules that conform with CodeIgniter's URL structure. Currently I have this:
RewriteEngine On
# Extract the subdomain part of domain.com
RewriteCond %{HTTP_HOST} ^([^\.]+)\.ev-apps\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Check that the subdomain part is not www and ftp and mail
RewriteCond %1 !^(www|ftp|mail)$ [NC]
# Redirect all requests to a php script passing as argument the subdomain
RewriteRule ^/(.*)/(.*)$ /dispatcher/run_app/$1/$2/%1 [L,QSA] # First is app name, then site, then action
RewriteRule ^/(.*)/(.*)/(.*)$ /dispatcher/run_app/$1/$2/$3/%1 [L,QSA] # First is app name, then site, then action, then any passed data.
It works fine for URLs that follow this format "http://example.ev-apps.com/app/method/1", but not for "http://example.ev-apps.com/app/method". It seems the precedence is all wrong, but ideally I want a solution that will work for any number of segments, for example: "http://example.ev-apps.com/app/method/1/2/3/4/5/6/7/8/9, etc.
Any help would be greatly appreciated.
I solved this issue by using the following directive:
RewriteRule ^(.*)$ /dispatcher/run_app/%1/$1 [L,QSA]
And then using the following code in my dispatcher action:
$args = func_get_args();
$site_subdomain = $args[0];
$app_name = $args[1];
$action = $args[2];
$input = (isset($args[3]) ? $args[3] : '');
Thanks for reading, if you did!