ZendFrameworks not working nice with RewriteRule - apache

I have the following RewriteRule on my Apache with ZendFramework in the back:
RewriteRule ^/account([0-9]+) /account?i=$1 [L]
Problem that I'm having is that I have an AccountController.php for ZendFramework, so URL from browser going to ...://myserver/account works just fine. However, when I have this rewriterule in httpd.conf, and point my browser to ...://myserver/account1, then ZendFrameworks complains that /account1 controller is not found, even though I have the rewriterule in apache. My guess is that ZF is looking at the REQUEST_URI which still have /account1 in it.
Anyone knows now I can make this rewriterule work properly with ZF? What I need is basically to have URLs going to /account([0-9]+) forward to /account?i=$1 instead using my ZF's AccountController.php?
Note I do not want to 'expose' the 'i' param to users (for various reasons, and yes, they could guess at looking at account1 that it can be sequenced), hence it is an internal forward instead of an external redirect back to the browser.
Thanks!

Maybe your rule is overwritten by other Zend rules? By default, Zend takes URLs in the form controller/key1/value1/key2/value2/..., maybe you could just try this?

Related

htaccess rewrite only working without "www" and "https"

I have this case where the rewrite only works without www and https, this is .htaccess:
RewriteRule ^en/(.*)/1348-duracell-32-aaa-pack.html$ /$1/3016-duracell-32-aaa-pack.html [R=301,NC,L]
this URL works and redirects:
http://example.com/en/car-accessories/1348-duracell-32-aaa-pack.html
this one doesn't:
https://www.example.com/en/car-accessories/1348-duracell-32-aaa-pack.html
any ideas? I spent a good few hours on this without any solution, not sure what's wrong. The rule is on top of the .htaccess file.
Any help would be greatly appreciated.
What do you mean by doesn't work ? Is the 2nd URL even redirecting ? If you are on a Linux platform you might want to try curl -L --head <url>, then you can see if redirections or taking place (or a redirection loop). Actually curl is a better way to test your rules, because your browser cache could be playing tricks on you.
Note that $1 is the first captured group from your regular expression. So you should add the host you want to redirect to ie:
RewriteRule ^en/(.*)/1348-duracell-32-aaa-pack.html$ https://example.com/$1/3016-duracell-32-aaa-pack.html [R=301,NC,L]
Otherwise the browser is very likely going to keep using the current host name. If you don't provide a full URL, then it has to make up for the missing bits.
But if all you want is remove /en/ from the URL, then you could write a simpler and more straightforward rule.

RewriteRule from mod_rewrite only works as redirect with added http://%{HTTP_HOST}, but doesn't work at all without it

I use this apache config file on centOs 7. This file is is already set up to run
/opt/otrs/bin/cgi-bin/customer.pl
on my machine on a call to
http://localhost/opt/customer.pl
I wanted to use mod_rewrite to run the customer.pl and give it the called url as a parameter.
So i want to enter the url:
localhost/HTMLData/1/Default.htm
and have it call:
/opt/otrs/bin/cgi-bin/customer.pl
with the parameters
?Action=CustomerHTML;Subaction=ContentCall;Target=1/Default.htm
That works somewhat with:
RewriteEngine on
RewriteRule ^/HTMLData/(.*)$ http://%{HTTP_HOST}/otrs/customer.pl?Action=CustomerHTML;Subaction=ContentCall;Target=$1
But i this is a redirect (because of the added "http://%{HTTP_HOST}").
What i need is, that the webrowser keeps thinking http://localhost/HTMLData/1/ is where to get the files.
But if i remove the "http://%{HTTP_HOST}", it doesn't work at all. But the examples i found on the internet said i should work this way. What do i have to change, to have the redirect only internaly without giving the browser the real url back?
Thank you
In case someone else might need this in the future.
What fixed it for me was adding
RewriteEngine on
RewriteRule ^(.*)$ $1
under "". I don't know why, but at least i can use the internal redirect now.

Redirect and Rewrite Rule for Apache

I am trying to install a CalDAV client on my apache webserver, and I am having trouble using a combination of redirects and rewrite rules to get a desired url.
The document root for my webserver is /var/www, and the calendar files are stored in /var/www/agendav/web/public. If I go in through my browser at <website>/agendav/web/public/index.php, I have no trouble getting to the interface and using it, so that is not a problem. However, my desired URL for the calendar is <website>/calendar/, instead of having to go down through the agendav folder tree. I have been trying to perform this with a redirect rule, and a rewrite rule, but to very little success. I have found a few other answers here that have gotten me close, such as this one and this one, with a working redirect, but I am still having issues with the rewrite rule. Here is the current solution I have:
# Rules for the Calendar
Redirect "/calendar" "/agendav/web/public"
RewriteEngine On
RewriteCond %{REQUEST_URI} "^/agendav"
RewriteRule "^/agendav/web/public/(.*?)$" "/calendar/$1"
My current solution seems a little circular. First, I redirect the user to the agendav folder, then then try to hide the redirect behind a rewrite rule, when it seems that I could just get away with a single rewrite rule. Unfortunately the group I am working for is not big enough to have their own dedicated server manager, and I ended up with the job despite knowing very little about it. Any help to get this would be greatly appreciated.
You don't need the redirect, you can do it with just a rewrite rule, the problem was that you have the condition and the rule reversed, you were using the real path for the conditions instead of the "virtual" path:
RewriteEngine on
RewriteCond %{REQUEST_URI} "^/calendar" [NC]
RewriteRule "^calendar/(.*)" "/agendav/web/public/$1"
With this rules all requests for http://yourwebsite/calendar/* are internally served with http://yourwebsite/agendav/web/public/*.

Writing a RewriteRule

I have a website that recently had a domain name change, and I am trying to set the rules up to include ANYTHING that is after the ruleset to be redirected (sub domains currently work, but file exensions do not) The current ruleset I have in place is:
RewriteRule ^subdomain/(.*)$ http://subdomain.example.com [R=301,L]
If I go to www.example.com/subdomain/ or www.example.com/subdomain/another_directory/ it redirects to the appropriate site, but if I go to www.example.com/subdomain/file.ext or www.example.com/subdomain/another_directory/file.ext, the redirect does not work. I know I need to change the regex so that it works correctly, but i'm not sure what it needs to be changed to.
You are not using captured group in target URL. Use it like this:
RewriteRule ^subdomain/(.*)$ http://subdomain.example.com/$1 [R=301,L,NE,NC]
You don't need mod_rewrite for this.
A simpler, lighter approach would be
RedirectMatch ^/subdomain/ http://subdomain.example.com/

apache rewrite map redirect to 404

My Situation:
I implemented an apache Rewrite Map to redirect incoming requests based on a database
RewriteEngine On
RewriteMap dbapp prg:/usr/local/somewhere/dbapp.rb
RewriteRule ^/(pattern)$ ${dbapp:$1} [R]
So far everything works fine, but I want to decide in the dbapp.rb script weather to redirect or give the client a http-status-code-404. I could just deliver a local page that doesn't exist but that doesn't seem right. I also want this to be usable on any server, and redirecting to "localhost" is also not an option ;-)
You could return -, which essentially means: 'no rewrite', but I don't know whether that's supported in a maps/[R] combination. Better may be to check with RewriteCond ${dbapp:$1} !^$ or something that it doesn't contain an empty string.