Create a dynamic RewriteRule - apache

Is is possible to create a RewriteRule that conatins a dynamic parameter (partial url)?
Here is the existing Rewrite:
RewriteCond %{SERVER_NAME} ^dashdiscovery-dev.site.com
RewriteRule ^/?auth/?$ https://osso-stg.site.com/opensso/idpssoinit?realm=/sitenet&iPSPCookie=yes&RelayState=app=tableau,appURI=/views&NameIDFormat=urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress&metaAlias=/sitenet/externalidpv2&spEntityID=server.site.com&binding=urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST [L,R]
What I need to to get the full address that is requested and put it in the Rewrite appURI= parameter dynamically.
Something like this:
RewriteCond %{SERVER_NAME} ^dashdiscovery-dev.site.com(/view/page7)
RewriteRule ^/?auth/?$ https://osso-stg.site.com/opensso/idpssoinit?realm=/sitenet&iPSPCookie=yes&RelayState=app=tableau,appURI=(**/view/page7**)&NameIDFormat=urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress&metaAlias=/sitenet/externalidpv2&spEntityID=server.site.com&binding=urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST [L,R]
But I also need it to redirect to just /views is there isn't any additional parameter after the site name RewriteCond %{SERVER_NAME} ^dashdiscovery-dev.site.com
Edit to provide more info about the expected urls:
dashdiscovery-dev.site.com
would need to have appURI=/views in the Rewrite
dashdiscovery-dev.site.com/views/ResourceManagerDashboardv10-3-15-13_bkup/4DemandvsBooking
would need to have appURI=/views/ResourceManagerDashboardv10-3-15-13_bkup/4DemandvsBooking in the Rewrite
dashdiscovery.site.com/views/OpsPipeline/PipelineDash
would need to have appURI=/views/OpsPipeline/PipelineDash in the Rewrite
not all URLs will be only 2 levels past /views, but they should all have /views

For dashdiscovery-dev.site.com:
RewriteCond %{HTTP_HOST} ^dashdiscovery-dev\.site\.com$ [NC]
RewriteRule ^/?$ https://osso-stg.site.com/opensso/idpssoinit?realm=/sitenet&iPSPCookie=yes&RelayState=app=tableau,appURI=/views&NameIDFormat=urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress&metaAlias=/sitenet/externalidpv2&spEntityID=server.site.com&binding=urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST [L,R]
For everything else:
RewriteCond %{HTTP_HOST} ^dashdiscovery-dev\.site\.com$ [NC]
RewriteRule ^/?(views/.*)$ https://osso-stg.site.com/opensso/idpssoinit?realm=/sitenet&iPSPCookie=yes&RelayState=app=tableau,appURI=/$1&NameIDFormat=urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress&metaAlias=/sitenet/externalidpv2&spEntityID=server.site.com&binding=urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST [L,R]
The $1 in the rule's target backreferences the (views/.*) regex match.

Related

Redirect url with ids range to another url using htaccess

I try to redirect user from Joomla plugins links that have specific IDs to the default admin page as following:
When user login in Joomla backend, he can reach this page of plugins:
https://www.example.com/administrator/index.php?option=com_plugins
Then if he wants to open a plugin with the id like 422 to edit it, he's to click on this link:
https://www.example.com/administrator/index.php?option=com_plugins&task=plugin.edit&extension_id=422
But instead of opening the plugin, I want the user to get redirected to this page:
https://www.example.com/administrator/index.php
To achieve this, I created a .htaccess in the folder administrator and placed the code at the end. So, I set a range of IDs of plugins that user cannot edit, but gets redirected.
Please find the all content of .htaccess file as following:
# Canonical https/www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# Redirect plug id from 350 to 423:
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{QUERY_STRING} (^|&)option\=com_plugins($|&)
RewriteCond %{QUERY_STRING} (^|&)extension_id=\b(3[5-8][0-9]|39[0-9]|4[01][0-9]|42[0-3])\b($|&)
RewriteRule ^administrator/index\.php$ https://www.example.com/administrator/index.php? [L,R=302]
# Redirect plug id from 425 to 10864:
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{QUERY_STRING} (^|&)option\=com_plugins($|&)
RewriteCond %{QUERY_STRING} (^|&)extension_id=\b(42[5-9]|4[3-9][0-9]|[5-9][0-9]{2}|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|10[0-7][0-9]{2}|108[0-5][0-9]|1086[0-4])\b($|&)
RewriteRule ^administrator/index\.php$ https://www.example.com/administrator/index.php? [L,R=302]
But does not work.
I create a .htaccess in the folder administrator and placed the code at the end.
# Redirect plug id from 350 to 423:
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{QUERY_STRING} (^|&)option\=com_plugins($|&)
RewriteCond %{QUERY_STRING} (^|&)extension_id=\b(3[5-8][0-9]|39[0-9]|4[01][0-9]|42[0-3])\b($|&)
RewriteRule ^administrator/index\.php$ https://www.example.com/administrator/index.php? [L,R=302]
If the .htaccess file is inside the /administrator subdirectory then you need to remove administrator/ from the start of the RewriteRule pattern (1st argument), otherwise the rule will never match.
In .htaccess, the RewriteRule pattern matches against a relative URL-path to the directory that contains the .htaccess file.
In other words, it should look like this:
:
RewriteRule ^index\.php$ https://www.example.com/administrator/index.php [QSD,R=302,L]
Also, on Apache 2.4 you can use the QSD (Query String Discard) flag instead of appending an empty query string to remove the original query string.
The preceding conditions that match the query string and plugin id are OK and should match the requested URL. (Although the word boundary \b elements are unnecessary.)
Depending on what other directives you have, this rule should be near the top of the .htaccess file, not "at the end". Since you have used an absolute substitution string it would be more optimal to include these rules before your general canonical redirects (although this does assume you are not implementing HSTS).
You are also missing the RewriteEngine On directive from the rules in question.
So, it should look like this instead:
RewriteEngine On
# Redirect plug id from 350 to 423:
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{QUERY_STRING} (^|&)option\=com_plugins($|&)
RewriteCond %{QUERY_STRING} (^|&)extension_id=(3[5-8][0-9]|39[0-9]|4[01][0-9]|42[0-3])($|&)
RewriteRule ^index\.php$ https://www.example.com/administrator/index.php? [QSD,R=302,L]
# Redirect plug id from 425 to 10864:
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{QUERY_STRING} (^|&)option\=com_plugins($|&)
RewriteCond %{QUERY_STRING} (^|&)extension_id=(42[5-9]|4[3-9][0-9]|[5-9][0-9]{2}|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|10[0-7][0-9]{2}|108[0-5][0-9]|1086[0-4])($|&)
RewriteRule ^index\.php$ https://www.example.com/administrator/index.php [QSD,R=302,L]
# Canonical https/www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Additional notes:
I assume you have not implemented HSTS.
I reversed the order of your two canonical redirects to reduce the number of redirects when requesting http://example.com/ (HTTP + non-www). But this does assume #1 above.
Optimised the regex on the canonical redirects... no need to traverse and capture the entire URL-path when using the REQUEST_URI server variable.
Removed the word boundary \b from the regex as this would seem unnecessary here.

Apache HTTP_URI redirect everything but sertain urls that query_string

We would like to redirect all trafic to our new page. We would still like that ceratin urls are still accessible on our old page, because these are used by our partners and are not yet ready to migrate.
The problem i'm having is with QUERY_STRING parameters.
My rules so far:
RewriteCond %{REQUEST_URI} !^/endfile.php
RewriteCond %{REQUEST_URI} !^/sites/default/files/pdffolder/
RewriteCond %{REQUEST_URI} !^/insure/agency
# RewriteCond %{REQUEST_URI} !^/content/insurance1?szs=000029&wssl=1
# RewriteCond %{REQUEST_URI} !^/text/insurance2?wssl=1&zst=enter
RewriteRule (.*) https://www.new-domain.eu/ [R=301,L]
The code works for the fist 3. I cant get it to work for the 4th and 5th rule. I currently have the commented out.
I tried using QUERY_STRING rules, without success. Is htere any way to use AND in rewrite condition?
Any ideas?
You cannot match query string using REQUEST_URI. Use THE_REQUEST to match both URI and query:
RewriteCond %{REQUEST_URI} !^/endfile\.php [NC]
RewriteCond %{REQUEST_URI} !^/sites/default/files/pdffolder/ [NC]
RewriteCond %{REQUEST_URI} !^/insure/agency [NC]
RewriteCond %{THE_REQUEST} !\s/+content/insurance1\?szs=000029&wssl=1[&\s] [NC]
RewriteCond %{THE_REQUEST} !\s/+text/insurance2\?wssl=1&zst=enter[&\s] [NC]
RewriteRule ^ https://www.new-domain.eu/? [R=301,L]
Also note use of trailing ? in target to strip off any existing query string.

Simple Apache mod_rewrite remapping

I have to remap a few ID's to URL strings (301 redirect) and I have to do it with mod_rewrite:
/page.php?id=15 to /pagexy
/page.php?id=10 to /pageyz
The rule:
RewriteRule ^page.php?id=15$ /pagexy [L,R=301]
doesn't work. What am I doing wrong?
You need to inspect the query string separately. The following should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=15\b [NC]
RewriteRule ^/page.php$ /pagexy? [L,R=301]
RewriteCond %{QUERY_STRING} id=10\b [NC]
RewriteRule ^/page.php$ /pageyz? [L,R=301]
Given you are tying ids to pages, it may also pay to look at using a RewriteMap

mod_rewrite - how do I match a URL, but not it's subdirectories?

I have an ecommerce site, with product URLs in the following format: site.com/product/long-product-title-here?sku=skugoeshere and want it in this format: site.com/product/long-product-title-here/skugoeshere
One reason for the query is that each page could have multiple variations of the same product.
I'm trying to use mod_rewrite to make it look nicer using the following:
# RewriteCond %{QUERY_STRING} ^sku=(.+)$
# RewriteCond %{REQUEST_URI} product/([^/]+)$
# RewriteRule /([^/]+)$ /$1/%1 [R=301,L]
But this goes into a redirect loop of the form site.com/product/title/title/title...
How do I make the pattern only match the original URL?
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([^/]+/[^/]+)\?sku=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} !(?:^|&)sku=[^&]+
RewriteRule ^([^/]+/[^/]+)/([^/]+)/?$ /$1?sku=$2 [L,QSA]
skip the leading slash here
RewriteRule /([^/]+)$ $1/%1 [R=301,L]
^ no slash here

Strip specific parameteters when redirecting with Mod-Rewrite

I have a pretty complex RewriteRule where I need to check if certain parameters are present in QueryString and then redirect to the same URL but with those parameters stripped.
How can I remove some parameters and preserve the rest?
RewriteCond %{QUERY_STRING} color=red
RewriteCond %{QUERY_STRING} status=contiue
RewriteRule ^(.*)$ /$1? [R=301,L]
url is like:
"http://example.com/site.php?setup=done&color=red&weight=100&status=continue"
(parameters order and quantity is not predictable/hardcoded)
Try these rules:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)(color=red|status=continue)($|&)(.*)
RewriteRule .* $0?%1%5 [N,E=REMOVED:true]
RewriteCond %{ENV:REMOVED} true
RewriteRule ^ %{REQUEST_URI} [L,R=301]
Another way would be to use PHP to check what parameters are given and remove them there.