Zend Framework 2 Mod Rewrite in htaccess is not working - .htaccess

I wrote application using ZF2 and at the end of the project I use some htaccess rewrite rules which let me to remove public keyword from application url. I found this rule in a internet tutorial and it works fine but i tried to add my own mappings and they don't work. Here is my .htaccess file content:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index [NC,L]
I tried to add rule
RewriteRule ^test-test-test-test-providers$ /providers?localisation=$2&serviceType=$4 [NC,L]
at the last line of file but, after checking, Zend is giving me error that requested URL can't be found in mapping.

Have you tried adding in the public dir:
RewriteRule ^test-test-test-test-providers$ /public/providers

Is there a reason why you remove the public via htaccess? The prefered solution is to set the main directory of your domain to the public folder, as described at https://framework.zend.com/manual/2.4/en/ref/installation.html - there is also a suggestion for a htaccess file. But this is not necessary when the current settings also work.
Zend is giving me error that requested URL can't be found in mapping
This one is important. I assume you mean that the error comes from the Zend Framework (Zend is only a company, it could be also the Zend). That means, that your request goes directly to the Zend Framework. Wouldn't it be better to use your route directly in the route configuration instead of remap it with your htaccess rules?
Could be possible, if the Zend Framework is still using the "test-test-test-test-providers" for it's routing. If so, you should use it in your routing configuration or test a Redirect instead of a RewriteRule; but this means, that the "test-test-test-test-providers" in the URL will be replaced by the "providers".

Related

.htaccess rule help in a plex non-windows hosting environment

I'm having trouble writing a rule for .htaccess which will redirect HTML to PHP. I'm using 302 until I get it to work right - then I'll change to a 301. I found several postings that describe this, but I am having problems - possibly because I'm running a hosting package, and each client is in a virtual/subfolder (sorry for poor description of hosting environment).
The rule I am using is
...
RewriteEngine on
RewriteRule ^(.*).html$ $1.php [R=302]
...
When I try to go to https://dmcelebratealife.com/index.html I get a 404 message saying:
https://www.dmcelebratealife.com/var/www/vhosts/dmcelebratealife.com/public_html/index.php
I added the following and it seemed to work for me.
RewriteEngine on
RewriteRule ^(.*)\.html$ /$1.php [R=302]
This works for me and is domain-independent.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(?!.+\.\w{2,4})(.+)$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(?!.+\.\w{2,4})(.+)$ $1.html [L]
The first two redirect to the php file if it exists. If it doesn't then the second two will try for an HTML file.
Note it doesn`t use a 302 as you don't want to tell the user what you are doing. This does an internal redirect.
This means that if the user types in
https://www.example.com/test
it will remain in the URL box, but the following file will be executed
https://www.example.com/test.php

htaccess redirect, everything is redirecting instead of specific requests

I'm trying to setup a new project structure. Due to some limitations of my cloud provider, I need to do some htaccess magic which I'm struggling with.
The project structure is as following
/docroot => Contains my Drupal installation
/docroot/frontend => Contains an Angular frontend
/docroot/pim => Is a symlink to /docroot.
What we need is that when we surf to example.com/pim that it redirects to the Drupal docroot /docroot. Since due to limitations of the cloud provider we can't put it in the /pim folder, they suggested to make a symlink.
Any other request to example.com should go to docroot/frontend.
So I've added the following code in my htaccess file
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/frontend.*$
RewriteRule !^pim($|/) http://example.com/frontend%{REQUEST_URI} [L,R=302]
Current behaviour is that all requests go to /frontend/index.php (I think the index.php comes from the .htaccess of Drupal, so the redirect is not done cleanely) While I would expect a request to example.com/test to direct to example.com/frontend/test
Also example.com/pim points to example.com/frontend/index.php instead of staying in the /pim directory
I've been able to fix it. :)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/frontend.*$
RewriteCond %{REQUEST_URI} !^/pim.*$
RewriteRule ^ frontend%{REQUEST_URI} [L,R=302]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/frontend.*$
RewriteRule ^ %{REQUEST_URI} [L]

Modrewrite url on index.php

I created a zend framework application and the modrewrite only worked when i did:
index.php/dashboard for instance, accessing the dashboard module.
With that being said, I'm working on a new project (not zend framework) that requires a url of: http://server/username. I'm already on the impression that i need to do something like http://server/index.php/username being that it was the only working method for my prior zend framework project.
I have tried several htaccess solutions, and none have worked. They all go to a 404. Here is what I've tried:
RewriteRule /([a-zA-Z]+)$ profile.php?name=$1
RewriteRule ^/([^/\.]+)$ /profile.php?name=$1 [L]
RewriteRule ^/index.php/([^/\.]+)$ /profile.php?name=$1 [L]
RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L]
Nothing seems to be working. Can someone help?
EDIT = I believe the problem is with my apache server. All of these rewrite rules should work. I've looked at httpd.conf and the virtual host configurations. Any insight?

Htaccess rule to make urls like this ?page=1 look like /page/1 in Codeigniter

This is how my urls currently look:
http://mysite.com/?page=1
How can I make this work?:
http://mysite.com/page/1
There is a post on StackOverflow that asks the same question. But the accepted solution isn't working for me. Because I am using Codeigniter and my page results in a 404 perhaps because since the url pattern of a CI site is:
domain/controller/method
The system is assuming that I am requesting a controller called "page" and a method called "1" both of which of course doesn't exist. Or maybye it's due to a conflict with the other code in my htaccess file (which I downloaded from the CI wiki, it gets rid of index.php and does a few security things). Here is my entire htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users. Additionally this will allow you to create a System.php controller, 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder. This snippet prevents user access to the application folder. Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file, such as an image or css document, if this isn't true it sends the request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
#Pretty urls for pagination links
RewriteRule (.*) index.php?page=$1
</IfModule>
The non indented bit is the solution I got from that other SO question that isn't working for me.
Any solutions to this CI pagination issue?
UPDATE
Ok, read some of the docs and now I have this working:
http://mysite.com/home/index/2
What would be the htaccess rule to turn that into?:
http://mysite.com/page/2
You should make this configuration at /application/config/routes.php (and let the .htaccess just for hide the index.php as you are already doing).
$route['page/(:any)'] = 'home/index/$1';
Or better, like #zaherg remembered (ensures that only numbers could by matched):
$route['page/(:num)'] = 'home/index/$1';
This way all the requests to http://mysite.com/page/2 will be treated internally as http://mysite.com/home/index/2 and so forth.
I suggest you take a look at CodeIgniter User Guide - URI Routing and CodeIgniter User Guide - Tutorial − Introduction.
Good luck.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
from CodeIgniter docs
That will handle removing the index.php, but what happens after that depends how CodeIgniter's query string handling is set up: it can be configured to use a query string rather than a path. See the link for more details.

mod_rewrites .htaccess not working with godaddy hosting (linux)

On my site www.sqcp.com in testing on another linux server, all worked as it should. However since moving it to godaddy, the mod_rewrites haven't been working, therefore none of the other pages have been accessible. Even if I create a blank directory/folder in the what it's trying to tidy the url to it then works for that page (obviously isn't a fix).
So any help would be great here my .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ / [L,R=301]
RewriteRule (.*)/{2,}$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteRule ^(.*)/(\d{4}\-\d{2}\-\d{2}\-[a-zA-Z0-9\-_]+)$ $1?s=$2 [L]
RewriteRule ^(.*/)?staff.php/([a-zA-Z0-9\-_]+) $1staff.php?s=$2 [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule (.*)/$ $1 [L]
</IfModule>
Godaddy run a perfectly good shared hosting service, addressing a large market sector -- users who want an active site (that is with some scripting) but without the cost or complexity of paying for or having the expertise to administer their own Linux VM. This isn't a GoDaddy problem. Its yours.
So first get to understand the environment that you are running under by running a phpinfo script, and make sure it creates the variables that you use. As far as I can see on your example:
Rules 1-3 are 301 redirections to enforce some request naming convention.
Rule 1 redirects /index.php to /
Rule 2 collapses trailing multiple / to a single /
Rule 3 rewrites GET requests for *.php to *.php/
Rules 4-6 map public URIs to internal ones
Rule 4 rewrites /*/yyyy-mm-dd-word to *?s=yyyy-mm-dd-word (note no QSA)
Rule 5 seems to be attempting to rewrite /*/staff.php/word to /*/staff.php?s=word but the syntax is wrong for this.
Rule 6 replaces any trailing / by .php on redirection
Rule 7 strips any trailing '/' unless the uri is a directory with an index.php (I assume that you are assuming a DirectoryIndex index.php (is this the case for GoDaddy?)
This is all hopelessly confused. Are you hiding or exposing the .php extension? Because Rule 3,5 and 6 are inconsistent. And rule 5 would seem more logical as
RewriteRule ^(.*?)/staff.php/([a-zA-Z0-9\-_]+) $1/staff.php?s=$2 [L]
Go back to the drawing board and work out what you are trying to do with your htaccess rules; what you want your public URI grammar to be; how your scripts are laid out; what redirects you want to pass back to the client browser and which you want Apache to handle as internal rewrites and what extra conditions are needed to prevent looping and misfiring. Make sure this makes sense and then debug them by building up your .htaccess file one rule at a time and using test requests to exercise each rule in turn to validate what its doing.
Trying adding the following at the start of your htaccess file. I had the same problem getting rewrites to work on GoDaddy which worked everywhere else:
Options -Multiviews

Resources