I have a server that responds to www.server.com/api
and I would like to have that same server respond to api.server.com
currently, I set up api.server.com to be an alias of www.server.com in my vhosts file, but I need to write a rule to add the /api on all requests with the api.server.com domain.
I've tried this:
RewriteEngine on
RewriteCond %{SERVER_NAME} api.server.com
RewriteRule ^(.*)$ /api/$1 [L,R=301]
but either way I play around with this, I get 500 because of a loop condition.
According to your description you do not want to implement an external redirection. Instead try that:
RewriteEngine on
RewriteCond %{SERVER_NAME} ^api\.server\.com$
RewriteCond %{REQUEST_URI} !^/api
RewriteRule ^(.*)$ /api/$1 [L]
It will have all requests to https://api.server.com/... get processed by /api/... without rewriting the visible URL in the browser. Also it will not result in an endless rewriting loop for direct or internal requests to /api/....
If you operate a current version of the apache http server you can replace the old L flag with the END flag which will prevent an additional rewriting iteration, thus save time and performance.
Related
I have to rewrite the URL so I have used the below code in my htaccess which is working
RewriteRule ^products/part/pumps/?$ pumps.php [L,NC]
if someone tries to access the url link exmple.com/products/part/pumps/ then it's showing the pumps.php data.
Now my issue is if some try to access the page example.com/pumps.php then how can I redirect to this link exmple.com/products/part/pumps/
I have tried but getting a redirecting error
Redirect 301 /pumps.php /products/part/pumps/
I have many pages and i have to redirect them also. sharing two example here
RewriteRule ^power\.php$ /products/engine/power/ [R=301,L]
RewriteRule ^connecting\.php$ /products/connection/power/connecting/ [R=301,L]
Use the following before your existing rewrite:
# External redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^pumps\.php$ /products/part/pumps/ [R=301,L]
(But test first with a 302 redirect to avoid potential caching issues.)
By checking that the REDIRECT_STATUS environment variable is "empty" we can redirect only direct requests from the user and not the rewritten request by the later rewrite. After the request is successfully rewritten, REDIRECT_STATUS has the value 200 (as in 200 OK HTTP status).
The RewriteCond (condtion) directive must precede every RewriteRule directive that triggers an external redirect.
The Redirect directive (part of mod_alias, not mod_rewrite) is processed unconditionally and will end up redirecting the rewritten request, resulting in a redirect loop. You need to use mod_rewrite throughout.
Use the END flag instead of RewriteCond (requires Apache 2.4)
Alternatively, you can modify your existing rewrite to use the END flag (instead of L) to prevent a loop by the rewrite engine. The RewriteCond directive as mentioned above can then be omitted. But note that the END flag is only available on Apache 2.4+.
For example:
# External redirects
RewriteRule ^pumps\.php$ /products/part/pumps/ [R=301,L]
# Internal rewrites
RewriteRule ^products/part/pumps/?$ pumps.php [END,NC]
It is advisable to group all the external redirects together, before the internal rewrites.
Unfortunately, due to the varying nature of these rewrites it doesn't look like the rules can be reduced further.
Im trying to rewrite url from long to short but cant wrap my head around this.
My survey rewrite works wonderfully but after completing my survet php redirects to www.example.com/survey_thank_you.php?survey_id=1
but I would like to show url like www.example.com/thank_you
Im not even sure if this is possible.
Im new with .htaccess and i have tried almost everthing
.htaccess
Options +FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^survey_thank_you.php?survey_name=([0-9a-zA-Z]+)/?$ Thank_you [L,NC,QSA]
RewriteRule ^([0-9a-zA-Z]+)/?$ survey_form.php?survey_name=$1 [L,NC,QSA] #works like charm.
Any help or directions will be highly appreciated.
Solution:
Options +FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^survey_id=([0-9a-zA-Z]+)/?$
RewriteRule ^survey_thank_you\.php$ /%1/thank_you [R,L,QSD]
RewriteRule ^([0-9a-zA-Z]+)/thank_you$ survey_thank_you.php?survey_id=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([0-9a-zA-Z]+)/?$ survey_form.php?survey_name=$1 [L,NC,QSA]
but after completing my survet php redirects to www.example.com/survey_thank_you.php?survey_id=1
You need to "correct" the URL that PHP is redirecting you to after the survey. If the desired URL is /thank_you (or /Thank_you?) then PHP should be redirecting to that URL.
You then use mod_rewrite in .htaccess to internally rewrite /thank_you back into the URL that your application understands. ie. /survey_thank_you.php?survey_id=1. However, therein lies another problem, where does the 1 (survey_id) come from in the query string? Presumably you don't want to hardcode this? So this would need to passed in the requested URL. eg. /1/thank_you or perhaps /thank_you/1?
However, is this really necessary? The resulting "thank you" page is not a page that should be indexed or a page that is normally navigated to by the user, so implementing a user-friendly URL here doesn't seem to be a worthwhile exercise?
RewriteRule ^survey_thank_you.php?survey_name=([0-9a-zA-Z]+)/?$ Thank_you [L,NC,QSA]
RewriteRule ^([0-9a-zA-Z]+)/?$ survey_form.php?survey_name=$1 [L,NC,QSA] #works like charm.
You are using a survey_name URL parameter (referencing an alphanumeric value) in your directives, but a survey_id ("numeric"?) URL parameter in your earlier example? So, which is it? Or are these rules unrelated?
You state that the second rule "works like charm", but how? What URL are you requesting? That would seem to rewrite /Thank_you to survey_form.php?survey_name=Thank_you - but that does not look correct?
As mentioned in comments, the RewriteRule pattern matches against the URL-path only. To match against the query string you need an additional condition that matches against the QUERY_STRING server variable. This would also need to be an external 3xx redirect, not an internal rewrite (in order to change the URL that the user sees). Therein lies another problem... if you don't change the URL that your PHP script is redirecting to then users will experience two redirects after submitting the form.
You also need to be careful to avoid a redirect loop, since you are internally rewriting the request in the opposite direction. You need to prevent the redirect being triggered after the request is rewritten. ie. Only redirect direct requests from the user should be redirected.
So, to answer your specific question, it should be rewritten something like this instead:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^survey_name=[0-9a-zA-Z]+/?$
RewriteRule ^survey_thank_you\.php$ /Thank_you [QSD,R,L]
The check against the REDIRECT_STATUS environment variable ensures that only direct requests are processed, not internally rewritten requests by the later rewrite. REDIRECT_STATUS is empty on the initial request and set to the string 200 (as in 200 OK status) after the first successful rewrite.
The QSD flag (Apache 2.4) is necessary to discard the original query string from the redirect response.
So the above would redirect /survey_thank_you.php?survey_name=<something> to /Thank_you.
But this is losing the "survey_name" (or survey_id?), so should perhaps be more like the following, in order to preserve the "survey_name":
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^survey_name=([0-9a-zA-Z]+)/?$
RewriteRule ^survey_thank_you\.php$ /%1/Thank_you [QSD,R,L]
Where %1 is a backreference to the value of the survey_name URL parameter captured in the preceding CondPattern.
However, you would then need to modify your rewrite that turns this back into an understandable URL.
(But you should probably not be doing this in the first place without first changing the actual URLs in the application.)
I have an e-commerce site that resides in:
http://dev.gworks.mobi/
When a customer clicks on the signin link, the browser gets redirected to another domain, in order for authentication:
http://frock.gworks.mobi:8080/openam/XUI/#login/&goto=http%3A%2F%2Fdev.gworks.mobi%3A80%2Fcustomer%2Faccount%2Flogin%2Freferer%2FaHR0cDovL2Rldi5nd29ya3MubW9iaS8%2C%2F
I'm trying to rewrite http://dev.gworks.mobi/* to http://frock.gworks.mobi:8080/openam/*, without redirection.
I've tried this in the .htaccess of the dev.gworks.mobi site:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/openam(.*)$ [NC]
RewriteRule ^(.*)$ http://frock.gworks.mobi:8080/$1 [P,L]
</IfModule>
But when I access http://dev.gworks.mobi/openam, it shows a 404 page not found page.
Can anyone help me to achieve my use case?
Try this:
RewriteEngine on
RewriteBase /
# Make sure it's not an actual file being accessed
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match the host
RewriteCond %{HTTP_HOST} ^dev\.gworks\.mobi
# Rewrite the request if it starts with "openam"
RewriteRule ^openam(.*)$ http://frock.gworks.mobi:8080/$1 [L,QSA]
This will rewrite all the requests to dev.gworks.mobi/openam to frock.gworks.mobi:8080.
If you want to mask the URI in a way that it's not visible to the visitor that she's visiting the authentication app, you need to add a P flag. Please note that it needs Apache's mod_proxy module in place:
RewriteRule ^openam(.*)$ http://frock.gworks.mobi:8080/$1 [P,L,QSA]
Feel free to drop the L flag, if it's not the last rewrite rule. See RewriteRule Flags for more information.
The 404
If it's all in place and you're still getting a 404 error, make sure that the target URL is not throwing 404 errors in the first place.
Second, check if you're still getting the error with the correct referrer URI set. It might be designed in a way to throw a 404, if the referrer is not correctly set. If that's the case, which I suspect, you need to use the R flag and redirect instead of proxying the request.
Last thing that comes to my mind, some webapps are not built in a way to figure out the URI address. The host, as well as the port number, might be hard-coded somewhere in the config files. Make sure that the authentication app is able to be run from another URL without the need to edit the configs.
Test
You can test the rewriterule online:
I'm very new to Apache and already run into an problem which already takes a lot of time and I'm not even sure if it's possible.
I have two servers and one Domain called szop.in which is having an A record to my first server. On the first server I'm running an URL shortener called YOURLS, it's under szop.in/admin. I want the second server serve my homepage, therefor I want to redirect all requests like szop.in or http://subdomain.szop.in to the second server but not http://szop.in/admin.
Is this possible?
This doesn't seem to be the right solution and the mod_rewrite is causing me some headache:
RewriteEngine On
RewriteCond %{HTTP_HOST} szop.in [NC]
RewriteRule !^/admin$ hxxp://other-domain.in [R=301,L]
My idea was, since I need just one URL to work on the first server http://szop.in/admin, to redirect everything that is not starting with /admin to the other domain.
You almost got it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^szop\.in$ [NC]
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteRule ^ http://subdomain.szop.in%{REQUEST_URI} [R=301,L]
You cannot use the negation on the RewriteRule like that, you use it on a conditions.
This should do what you want, it verify if domain is szop.in and if folder is not /admin and redirect to subdomain.szop.in.
I'm trying to set up a rewrite rule which will force all requests coming in on port 80 to use HTTPS by force.
I'm only getting my head around mod_rewrite but this is what i currently have;
RewriteCond ${lowercase:%{REQUEST_URI}} /securePath$
RewriteRule ^(.*)$ https://www.mydomain.com/$1
In the RewriteCond securePath is the requested path (not including my domain).The full URI would be www.mydoamin.com/securePath
In the ReWriteRule $1 is supposed to be the output from ${lowercase:%{REQUEST_URI}} in the RewriteCond
However when i restart my IHS server and attempt to hit the URL it isint forcing access through HTTPS. Any suggestions on what is wrong with these two lines?
Thanks
RewriteRule ^securePath/(.*)$ https://www.mydomain.com/$1 [NC,R=301,L]
Probably it's just you example being broken, but you are explicitly lowercasing the incoming request path and try to match that to "/securePath" which includes a capital letter. -That is never going to match.