.htaccess - rewrite user login in url - zend-framework

I need to rewrite URL with user login and i use Zend Framework.
.htaccess of ZF project are follow:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
How to add rule, with rewrite
http://yourdomain.com/user/TOMMY
to
http://yourdomain.com/user/?login=TOMMY
? Where "TOMMY" is user login.
Important: after rewriting, URL in browser leaves without changes.
I can add next rule:
RewriteRule ^user/([a-zA-Z0-9]+)$ /user/?login=$1 [L,R]
But its not correctly, because server redirect browser to other page.
Tnx.

You need to change the other rule so that it doesn't rewrite URI's that start with "/user/"
RewriteCond %{REQUEST_URI} !^/user/
RewriteRule ^.*$ index.php [NC,L]
Now you can rewrite the /user requests using your rule:
RewriteRule ^user/([a-zA-Z0-9]+)$ /user/?login=$1 [L]
Except you don't want that R in the brackets, because it will redirect the browser and change what's in the address bar.

Related

.HTACCESS Redirection Problem: index.php Before URL Slug

I am trying to achieve a simple http to https redirect and www to non-www. The problem is that .htaccess puts "index.php" before the url slug, resulting in a server error. Here is what happens:
WRONG BEHAVIOUR:
http://example.com/url-slug -> https://example.com/index.php/url-slug
DESIRED BEHAVIOUR:
http://example.com/url-slug -> https://example.com/url-slug
Note: I want all queries to redirect to the index.php page in the main directory, unless the requested file exists on the server. I'd like to achieve this without changing the url in the browser, which causes the server to crash.
(Objective: www -> non-www & http -> https)
CURRENT .HTACCESS SETTINGS:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What am I doing wrong?
Your http to https 301 redirect should be at the top before other internal rewrite rules
RewriteEngine On
#http to https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Make sure to clear your browser cache before testing this change.

Zend Framework htaccess Backend / Frontend

In my current ZF project, I've two public access points (files) ready to handle & process coming requests, that's: index.php & admin.php
I need all URLs containing '/admin' to be routed to admin.php while any other URLs doesn't contain '/admin' to be routed to index.php
Knowing that default ZF htaccess contains the following rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
How this can be implemented?
I want to start by saying that I full heartedly agree with AurelioDeRosa. I do believe that routes will be a better choice. However that is your choice not mine and this .htaccess code should do the trick you want.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*(admin).*$ admin.php [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Note however that this script will catch anything that contains admin so it might break your frontend. for example, if you want to list all your users with administrative rights the url yoursite.com/users/listby/admin/ will redirect the request to admin.php, and so will urls like /administrator/ or /adminininini

htaccess https redirect only on specific Zend Frameworks controller/actions

I'm new to this community, but I've found it really useful time to time.
I've searched for the answer a lot, but I didn't find anything like I need. I've tried to solve it on my own, but I still get errors, so I hope to find someone that can show me the right way... :-)
I've got a "classic" ZF website, with many controller/action urls that are redirect to index.php with a .htaccess file.
Now, what I need, is to redirect a couple of controller to https ssl connection excluding some actions of both controllers.
The way I was trying to do it is:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.tld [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond $1 ^!((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond $1 ^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(js|ico|gif|jpg|png|css|swf|pdf|txt)$ index.php
It seems to work when I go to /member controller
But then, when I go to another controller, for example /index or /about it does not redirect to the http connection (port 80), and if I try to change a little the rewrite condition regex it sometimes does a redirect loop and the browser gives me a notice blocking the connection to the site.
Is there anyone that could show me the right synthax to use in my rewrite conditions to allow both the controllers (excluding the given actions) under an https connection and going back to a standard http connection when changing controller?
Thanks in advance.
Alessandro
Try these rules instead (replace appropriate lines):
RewriteCond %{HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !^/(member|shop)/ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} ^/(member|shop)/ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
These rules a bit simple (HTTPS will be applied to ALL URLs in /member/ and /shop/ controllers (e.g. /member/login, /member/dashboard, /member/orders/15423/invoice etc)
Negate ! should be before ^ in RewriteCond directive -- if you want your own rules then replace RewriteCond $1 ^!((member|shop)/(?!(index|login))(.*)) by RewriteCond $1 !^((member|shop)/(?!(index|login))(.*))
A method we use to redirect to https is to leave the default Zend Framework .htaccess settings and create an action helper to redirect to https when required. Our action helper code is:
<?php
class RequireSSL extends Zend_Controller_Action_Helper_Abstract
{
public function direct()
{
if(empty($_SERVER['HTTPS']) && $config['billing']['requireSSL'])
{
$redirector = $this->getActionController()->getHelper('Redirector');
$redirector->goToUrlAndExit('https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
}
}
Then when you are in a controller action that you need to be accessed via HTTPS you simply call this function at the beginning:
$this->_helper->requireSSL();
Also, here is another method for using action helpers that is a little more detailed if you need it:
http://juriansluiman.nl/en/article/110/in-control-of-https-for-action-controllers
Hope that helps!
I have discovered where the origin of the problem is, but I'd still need support to understand how to solve it.
I have tested on a local linux machine the htaccess and the result was the same... testing separately the two https condition statements (on and off) they work correctly redirecting basing on the given RewriteCond regex. When putting together only the redirect from http to https works.
Https to http redirect works only if the regex is not matched, else it redirects to http://www.mydomain.tld/index.php
So I finally tried to delete the last htaccess statement and it started to work correctly, but, obviously, it does not find the url, as it does not redirect to the index.php anymore.
It looks like after the correct https redirect the index.php one creates the problem. So I'm asking myself if there is a way to avoid this and make it work correctly.
As I wrote before, this seems to be a common problem of this htaccess, as its behaviour is the same on the test and on the production server (different linux flavours).
I put here the working code:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteCond %{HTTP_HOST} ^mydomain\.tld [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond $1 !^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [L,R=302]
RewriteCond %{HTTPS} off
RewriteCond $1 ^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=302]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule !\.(js|ico|gif|jpg|png|css|swf|pdf|txt)$ index.php
Finally I have solved it! I persevered in searching the answer because I think that doing it with .htaccess is cleaner, smarter and easy to maintain.
The problem was essentially due to the regexp used in the "ssl to non-ssl" block that was not correctly matching the value passed (that is best matched now reading the env variable %{THE_REQUEST}, avoiding, in some cases, an erroneus redirects loop.
I paste here the working code for further reference:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteOptions MaxRedirects=1
RewriteCond %{HTTP_HOST} ^yoursite\.tld [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /((controller1|controller2)/(?!(action1|action2))(.*))\ HTTP/
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /((controller1|controller2)/(?!(action1|action2))(.*))\ HTTP/
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} \.(js|css|ico|gif|jpg|png|swf|txt|pdf)$ [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]

Redirect all traffic but "beta" sub-domain to Splash screen (mod_rewrite)

I'd like to redirect all my traffic to my root domain, the root domain being a Splash screen. I'd like to do this ONLY when the %{HTTP_HOST} is not beta.example.(com|es|fr).
So I've tried the following in my .htaccess file, with no luck... (infinite loop... I guess it's not getting the REQUEST_URI condition, but cannot figure out why?? I've tried almost everything)
RewriteCond %{HTTP_HOST} !^beta\.example\.com$
RewriteCond %{HTTP_HOST} !^beta\.example\.es$
RewriteCond %{HTTP_HOST} !^beta\.example\.fr$
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^.*$ / [NC,L,R=302]
At that point, I should have all my traffic but "beta" sub-domain redirected to the root domain. So now I'd like to Rewrite "backstage" the root domain with my Splash screen, following in the .htaccess file:
RewriteCond %{HTTP_HOST} !^beta\.example\.com$
RewriteCond %{HTTP_HOST} !^beta\.example\.es$
RewriteCond %{HTTP_HOST} !^beta\.example\.fr$
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ splash.phtml [NC,L]
Note: I'm using the Zend Framework, so these rules are following in the .htaccess file:
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I must say, I'm stuck.
thx in advance
#bsagols
This line from Zend is going to screw you up for sure.
RewriteRule ^.*$ index.php [NC,L]
You need to put your directives above this, if not above all of the Zend stuff. If I'm understanding you correctly, try this:
RewriteCond "%{HTTP_HOST}" "!^beta\.example\.(com|es|fr)$" [NC]
RewriteCond "%{REQUEST_URI}" "!\.(js|ico|txt|gif|jpg|png|css)$" [NC]
RewriteRule ".*" "/splash.phtml" [R=302,L]

ISAPI ZEND url rewrite adding extra "/index.php/" into url

We recently reinstalled our web site on our server, the sys admin says it is an exact rebuild, and it indeed looks to me that it is, but there is something different going on. I did not originally develop the site, and those who did are no longer available.
urls for the admin site are now
//admin.site.com/index.php/schedules
and used to be, and should be
//admin.site.com/schedules
The extra index.php is getting inserted in the url. But all links within the admin site do not include the index.php so they do not work. If you insert the index.php into any url, it works. Where did this index.php come from and how do I eliminate it from the rewrite rules so the links will work.
This is what my httpd.conf file reads:
# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.56
# turn on rewriting RewriteEngine On RewriteBase /
# for all files not found in the file system,
# reroute to "index.php" bootstrap script,
# keeping the query string intact.
RewriteCond %{HTTP_HOST} ^admin.site.com$
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{HTTP_HOST} ^admin.site.com$
RewriteRule ^.*$ index.php [NC,L]
Thank you! Any help would be greatly appreciated.
You need to implement and enable ApacheModrewrite. And then put a .htaccess file in your admin.site.com folder
Its content will be :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]