I am currently using Caddy as a reverse proxy.
rewrite / /index.html
https://localhost/ --> https://localhost/index.html
https://localhost/?p1=v1&p2=v2 --> https://localhost/index.html?p1=v1&p2=v2
Now I want to additionally change the path /static to index.html. However, a URL parameter static=true should then be added.
https://localhost/static --> https://localhost/index.html?static=true
This additional URL parameter should not influence the existing parameters.
https://localhost/static?p1=v1&p2=v2 --> https://localhost/index.html?static=true&p1=v1&p2=v2
How do I have to adjust my caddyfile to make this work as desired? I just tried the following, but unfortunately it didn't work.
rewrite /static /index.html?static=true
Related
I try to rewrite some of my URLs with a .htaccess file but it didn't work as expected.
This is the rewrite rule in my .htaccess file :
RewriteRule ^(index|administration)/([A-Za-z0-9-]+)(\.php)?$ index.php?c=$1&t=$2 [QSA]
When I go on www.example.com/index/main, I get a 404 error code.
So I try to change my rewrite rule to
RewriteRule ^index.php$ index.php?c=index&t=main [QSA]
Then I go to www.example.com/index.php and the webpage displays perfectly with all the datas in $_GET (c = index and t = main).
So I don't know why my first rule is not working. Let me see if you have any idea.
Is it possible that my server wants to enter the index folder, then the main folder for my first rule without taking care of my .htaccess (www.example.com/index/main) ?
You need to ensure that MultiViews (part of mod_negotiation) is disabled for this to work correctly. So, add the following at top of your .htaccess file:
Options -MultiViews
If MultiViews is enabled (it's disabled by default, but some hosts do sometimes enable this in the server config) then when you request /index/main where /index.php already exists as a physical file then mod_negotiation will make an internal request for index.php before mod_rewrite is able to process the request. (If index.html also exists, then this might be found first.)
(MultiViews essentially enables extensionless URLs by mocking up type maps and searching for files in the directory - with the same basename - that would return a response with an appropriate mime-type.)
If this happens then your mod-rewrite directive is essentially ignored (the pattern does not match, since it would need to check for index.php) and index.php is called without the URL parameters that your mod_rewrite directive would otherwise append.
it perfectly works by disabling the MultiViews Option in my .htaccess
This would ordinarily imply its your script (ie. index.php) that is triggering the 404 (perhaps due to missing URL parameters?), rather than Apache itself?
However, if you were seeing an Apache generated 404 then it would suggest either:
You also have an index.html file, which is found before index.php. .html files do not ordinarily accept path-info (ie. /main) so would trigger a 404.
OR, AcceptPathInfo Off is explicitly set elsewhere in the config, which would trigger a 404 when the request is internally rewritten to /index.php/main (by mod_negotiation).
I need to construct a RewriteRule to have the docroot's index.html file handle all requests made to a folder named: doc.
Here's what I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^doc/(.*) index.html [DPI,NS]
</IfModule>
I've added the [DPI,NS] flags to try and force this rule to only do this once, but, I think I'm missing something really obvious because it goes into a nested request loop, forever adding subsequent requests to the original request path -even without the flags.
When I visit: apache.dev:/doc/hello.htm, here's a snippet of the subsequent request log:
/doc/lib/lib/gui/lib/lib/gui/gui/lib/css/base.css
-which is weird, because ALL those 3-letter folder-names are directly in the docroot - and have no sub-directories in them.
When I specify the target path absolutely (starting with i.e: http://), it works, however, I get a 302 (redirect) - which is not what I want at all.
When I specify the path relatively to a sub-folder where I put this index.html file, then it works perfectly.
Question:
How do I force this to behave the way I need to (as with relative path) referring it to the docroot's "index.html" file - without redirect?
Any answer/comment will be appreciated, thanks.
The reason why it generates an infinite request-loop in growing the path is because of the way you reference a path in the handler, if the handler is client-side.
If you have: <script src="some/file.js">, then the request will look in the current document.href path's directory for some/file.js
If you refer it like: <script src="/some/file.js"> (note, it starts with a /) -then it will actually look in the docroot; and not append subsequent requests to the current href dir, -which works just fine.
I have a URL as follows: www.site.com/catalog/?type=6 which I need to rewrite to www.site.com/white-wines/white-cases.
What is important is that it is not a redirect, for example the URL should remain as www.site.com/white-wines/white-cases when visited, but show content from www.site.com/catalog/?type=6.
The current solution that I have performs a redirect and I'm struggling to work this out.
RewriteRule ^white-wines/white-cases$ http://www.site.co.uk/catalog/?type=6 [L]
Setting full address in the target implies a redirect if the target is different then the current hostname, not rewrite. Try root-relative path.
RewriteRule ^white-wines/white-cases$ /catalog/?type=6 [L]
You can't do a re-write to a different hostname.
Absolute URL
If an absolute URL is specified, mod_rewrite checks to
see whether the hostname matches the current host. If it does, the
scheme and hostname are stripped out and the resulting path is treated
as a URL-path. Otherwise, an external redirect is performed for the
given URL. To force an external redirect back to the current host, see
the [R] flag below.
Source: Apache Module mod_rewrite
I'm stuck with htaccess redirect on this case:
I have myapp.com domain where my main website and service runs on. My customers logs into their accounts on myapp.com and use. Now, I am going to provide one of the features on a separate domain, let's assume "goto.com". However, I don't want to build a separate app on goto.com. I just want to redirect all coming requests to goto.com to a php script under myapp.com but this redirection should be in the backend (masked), not a 301 redirection.
Let me clear up:
myapp.com - /var/www/vhosts/myapp.com/httpdocs/index.php
goto.com --> masked redirection --> myapp.com/goto.php?$1
How can I do this with htaccess? Any suggestions?
Just found that it can be done with redirect [P] (proxy) method: RewriteRule ^(.*)$ http://myapp.com/public_view/$1 [P] What do you think? Is this a good and stable method?
That method is fine, it utilizes the mod_proxy module. The only thing I can suggest is adding the L flag as well in case you have other rules in your htaccess file.
A limitation with using the P flag in an htaccess file is that if a page or request to http://myapp.com/ redirects, then you're URL address bar will say http://myapp.com/ instead of your other domain. The only way around this is to use ProxyPassReverse but it doesn't work in an htaccess file. You'd need access to vhost config:
ProxyPass / http://myapp.com/public_view/
ProxyPassReverse / http://myapp.com/public_view/
Additionally, if http://myapp.com/ sets cookies, you'll need to use the ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath directives to rewrite those.
I have to redirect a url to another url with all its parameters using htaccess.
My incoming url will be something like this:
www.mydomain.com/book-it.jsp?id=3&var1=values&var2=value2...
and I want to get it in PHP (book-it.php) as something like this (with all its parameters):
www.mydomain.com/book-it.php?id=3&var1=values&var2=value2...
I was using JSP and from now on moving to PHP and we need to use same URL since this url is already published with my application and I can't change that now. I need to get that url and parameters to another file.
You have 2 main approaches:
1) Using Redirect directive:
Redirect 301 /book-it.jsp http://www.mydomain.com/book-it.php
2) Using mod_rewrite module (this needs to be placed in .htaccess in website root folder. If placed in Apache config file (inside <VirtualHost>, for example) the rules need to be modified slightly):
RewriteEngine On
RewriteBase /
RewriteRule ^book-it\.jsp$ http://www.mydomain.com/book-it.php [QSA,NC,R=301,L]
Both of them will preserve query string.