How to mod_rewrite so all paths are proxied except base path, using .htaccess - apache

I'm trying to write some proxing rewrite rules using mod_rewrite for my site but cant get it to work. Please help out, mod_rewrite experts.
I need to proxy all sub-paths to an external server while preserving the URI, but I dont want to rewrite the base path /.
I do not know the sub path's name and I want it to be treated like a wildcard
/[/[/*...]]
www.domain.com/ -> www.domain.com/index.php
www.domain.com/xxx/ -> www.external-server.com/xxx/
www.domain.com/xxx/yyy/ -> www.external-server.com/xxx/yyy/
www.domain.com/xxx/yyy/zzz/ -> www.external-server.com/xxx/yyy/zzz/
I can easily proxy calls on all URI's like this:
RewriteEngine On
RewriteRule (.*) http://www.external-server.com/$1 [P,QSA]
Anyone got an idea about a working set of rules and cond's supporting my use case?
Thanks in advance!//
Edsh

If I'm understanding you correctly, something like this should work for you.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/
RewriteRule (.*) http://www.domain.com/index.php [L,P,QSA]
RewriteCond %{REQUESTURI} !^/
RewriteRule (.*) http://www.external-server.com/$1 [P,QSA]

Related

Apache re-write URL path that doesn't exist

We currently have a website with a URL structure as follows:
https://www.example.com/en_CA/homepage/page1.html
https://www.example.com/en_CA/homepage/page2.html
We need to shorten the URL to:
https://www.example.com/page1.html
https://www.example.com/page2.html
We have tried using the following rewrite rules and conditions:
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/ [NC]
RewriteRule ^/(.*)$ /en_CA/homepage/$1 [P]
The problem we have is that we get a 404 because the shorter URL doesn't exist. I think the solution needs to also involve AliasMatch to set up an alias for that URL but I'm not sure how to go about that.
I've tried:
AliasMatch ^/[^/]*/(.*) /en_CA/homepage/$1
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/ [NC]
RewriteRule ^/(.*)$ /en_CA/homepage/$1 [PT]
But this doesn't work.
The website is build using Adobe AEM so we need to ensure that AEM only ever receives the long URL.
Thanks
Russell
There is no need to use AliasMatch, I think, you want to access the url https://www.example.com/en_CA/homepage/page1.html from https://www.example.com/page1.html, and the same to the other one. Please let me know if I am wrong.
Try this, let me know if it works:
Please read the comments (text after # symbol) carefully
# Add this to your root .htaccess file i.e the public_html, htdocs, etc. or use RewriteBase /
RewriteEngine On # Do not use this two times in one .htaccess file, be sure you don't have any other directories other than /en_CA/homepage/ in your root dir, or use the RewriteRule ^(.*)$ /dirname/$1 [L] for every dir.
RewriteCond %{REQUEST_URI} !^/en_CA/homepage/
RewriteRule ^(.*)$ /en_CA/homepage/$1 [L]
I am sure the above will work.
Follow the same to other folders.
I will add something later to hide the folder containing it.

Apache Rewrite for Entire Subdomain

How would I go about rewriting :
proxmox.example.com
to
example.com/forward/?url=proxmox.example.com
I am trying to do this on Ubuntu with Apache
Please let me know what Apache plugins are used.
Will this rewrite go into my VirtualHosts file? (/etc/apache2/sites-available)
Thanks for the help!
You won't need a vhost for that. It can be put in the main Apache config.
Put this in your config:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(proxmox.(example.com))$
RewriteRule .* http://%2/forward/?url=%1 [R,L]
Use [P,L] instead of 'R' if you want to mask/hide the rewrite from the user. Otherwise stay with 'P'.
Also I used two nested brackets to retrieve example.com (value is stored in %2) and use that instead of a static 'example.com' in the second line. But if you prefer a 'clearer' (=easier to read) rule, use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(proxmox.example.com)$
RewriteRule .* http://example.com/forward/?url=%1 [R,L]
Hope it helped. Let us know if it did, and even more so, if it didn't so we can help furthermore :)

.htaccess redirect according to PHP parameters

I'm trying to build some redirections with my .htaccess file to deal with some old forum url. I want the redirections to be made according to the PHP parameters (the topic ID).
For instance, something like
RewriteEngine On
RedirectPermanent /forum/viewtopic.php?t=123 /page1.html
RedirectPermanent /forum/viewtopic.php?t=345 /page7.html
RedirectPermanent /forum/viewtopic.php?t=89 /page3.html
The old and new URL are not related to each other (no PHP parameter has to be kept or something). I want to decide manually in my .htaccess file what to do for each topic ID.
But I can't manage to do that so easily. I tried many things, but nothing works.
Is this possible ? Any idea ?
Thanks a lot !
Edit : additional question : I want to add a global redirection of all the folder /forum to the root of the site ("/"). I guess I can place it after the others, so if no other rule is trigered, this one will be trigered.
I'm trying some things like
RewriteRule ^forum /? [L,R=301]
But everything I have tried so far redirects me to the "page1.html" (my first rule). Any idea why ? Thanks a lot !
You can't match against the query string using mod_alias's Redirect, RedirectMatch, etc. You need to use mod_rewrite and match against the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^t=123$
RewriteRule ^forum/viewtopic\.php$ /page1.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=345$
RewriteRule ^forum/viewtopic\.php$ /page7.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=89$
RewriteRule ^forum/viewtopic\.php$ /page3.html? [L,R=301]
NOte that RewriteEngine is a mod_rewrite directive, not mod_alias. So it has no affect at all on the RedirectPermanent directives.

Apache URL Rewriting for multiple dynamic folder levels

Wondering if somebody can help me write some RewriteRule's for my website.
Take a look at the following URLs and see how I need to rewrite them.
http://www.example.com/essays-and-reports/
does not need to be re-written, it is a physical folder on the web server.
http://www.example.com/essays-and-reports/business/
needs to rewrite to (root)/first_level_template.php
http://www.example.com/essays-and-reports/dynamic_name2
needs to rewrite to (root)/first_level_template.php
http://www.example.com/essays-and-reports/business/financial-reports/
needs to rewrite to (root)/second_level_template.php
http://www.example.com/essays-and-reports/blah/financial-reports/C_B_2413_Report_on_savings.php
needs to rewrite to (root)/final_level_template.php
Note the rules must work regardless of a trailing slash. To sum-up, there are three levels which I need to re-write to their relevant template. None of above exists physically on the server including the PHP file for final level. The only thing that exists is the essays-and-reports folder which is main folder for the website.
I tried something like this but I get compile errors in the log.
RewriteRule ^([^/]+)/([^/]+)/?$ /second-level-template\.php [L]
If you could help me write the rules I need - I appreciate it greatly.
EDIT:
This code kind of works but it also rewrites the essays-and-reports folder which I don't want...
Options +FollowSymLinks
RewriteEngine On
RewriteBase /essays-and-dissertations/
RewriteRule ^[^/]+/[^/]+/[^/]+/?$ /final_level_template.php [L]
RewriteRule ^[^/]+/[^/]+/?$ /second_level_template.php [L]
RewriteRule ^[^/]+/?$ /first_level_template.php [L]
You could do this with 3 rules:
RewriteRule ^essays-and-reports/[^/]+/?$ /first_level_template.php [L]
RewriteRule ^essays-and-reports/[^/]+/[^/]+/?$ /second_level_template.php [L]
RewriteRule ^essays-and-reports/[^/]+/[^/]+/[^/]+/?$ /final_level_template.php [L]
You don't need to escape the dot in your redirect target, since that's not a regular expression.

Simple mod_rewrite RewriteRule for messy legacy url's

Launching a new website for a new client. Their old site has about 50 products and unfortunately, the old product names do not match up to the new URL pattern
Old URL Examples:
example.com/products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription
example.com/products.aspx?category=Foo&product=ProductNameDescription&var1=1293.123
example.com/products.aspx?category=Bar&product=ProductCategoryProdNameRandomNumbers
(The old URL's are sometimes hitting 150+ characters.)
New URL's:
example.com/products/category/actual-product-name
There's no set, recognizable pattern to go from the old product name to the new one. There is for the category.
I've tried simple mod_alias Redirects, but understand that I need a RewriteRule instead. But I'm having problems. All I need is a 1-to-1 redirect for each of these 50 URL's. I thought I could do something like:
RewriteRule ^/products.aspx?category=Foo&product=ProductName
/products/category/new-product-name/ [R=301,NC]
But that isn't working. I know this should be simple, but I am stuck. Any ideas?
Use the pattern below for the rest of your redirect urls. Note that you escape special characters e.g. ? , . and space by adding a \ in front of them
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /products\.aspx\?category=Foo&product=SuperLongNoBreakProductNameIDDescription [NC]
RewriteRule ^ /products/category/new-product-name/ [R=301,NC]
Have a look at the RewriteMap directive of mod_rewrite.
You can specify in a text file something like:
products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription /products/category/new-product-name
And in your httpd.conf
RewriteMap productmap /path/to/map/file.txt
RewriteRule ^(.*) ${productmap:$1} [R=301,NC]
Tip: If it's a permanent redirect you want, make sure you set an appropriate Cache-Control and Expires header to instruct browsers to cache the 301.
You can try something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^category=Foo&product=ProductName$
RewriteRule ^products\.aspx$ /products/category/new-product-name/? [R=301,L]
Notes:
In per-dir (.htaccess) context, the per-dir prefix is stripped, so you can't start the RewriteRule pattern with ^/.
You have to use RewriteCond to match against the query string.
As stated in another answer, a RewriteMap solution might be suited to this situation, if you have access to httpd.conf / the vhost definition for this site. I'm not sure how that works with query strings though.
For something like this, it might be a better solution to rewrite all of these URLs to a server side script, and use the script to do the HTTP redirect for each URL.