ZF2 Rewrite for Subdirectory / Url Routes - apache

My public folder is within a sub-directory. So I am using two .htaccess files.
The first one outside of the public folder directory is:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [NC,QSA]
The second one inside the public folder is the default Zend one:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
When I go to http://mydomainname.com/myappname I get the following error:
A 404 error occurred
Page not found.
The requested URL could not be matched by routing.
,but this works of course when I go to http://mydomainname.com/myappname/public
I am not sure whether my .htaccess needs to be fixed or if I need to fix my routes

I think the simplest solution would be to move the content of your public folder to its parent directory.
Copy
myappname/public/css
myappname/public/images
myappname/public/js
myappname/public/index.php
myappname/public/.htaccess
to
myappname/css
myappname/images
myappname/js
myappname/index.php
myappname/.htaccess
Make sure you replace myappname/.htaccess with the default zf2 public/.htaccess
Then in myappname/index.php comment out:
//chdir(dirname(__DIR__));

Related

.htaccess Reroute file with hidden parameter to another file

I have a problem which I'm trying to solve nor for hours.
So in my root director i have a folder called plugins with some files in it. The important ones are plugins.php and query.php.
When the url is /test/plugins I wan't to show the content of plugins.php. But when the URL is /test/plugins/param I wan't to keep this URL in the browser but want to show the content of query.php and use param with $_GET['param'] in it. Is this possible?
My latest and most promising .htaccess file looks like this:
RewriteBase /test/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ query.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ query.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ query.php [QSA,NC,L]
With your shown samples and attempts, please try following htaccess rules file.
Make sure to keep your htaccess rules file inside plugins folder and also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^test/plugins/?$ plugins.php [QSA,NC,L]
RewriteRule ^test/plugins/param/?$ query.php [QSA,NC,L]

Started implementing .htaccess but when navigating from the reduced URL, when going into a directory it then can't get back

So I have included an htaccess file to my server in the root directory and changed all of the ./ I could find set the absolutes.
However, when I search by URL into one of the directories pressing the home button does not take me home. Instead, it appends the index onto the end:
/website/book/index.php?p=home
Instead of
/website/index.php?p=home
Where have I made a fuddle?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ https://%{HTTP_HOST}/paperbound/$1 [R=301,L]
RewriteRule ^([^/\.]+)?$ index.php?p=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?p=$1&id=$2 [NC,L]
</IfModule>
Is the htaccess used. https://sitehost/website/book/2 is URL entered and page retrieved which exists as https://sitehost/website/index.php?p=book&id=2, clicking navlink to return to https://sitehost/website/index.php?p=home, instead places https://sitehost/website/book/index.php?p=home into the URL bar and returns an error as the file does not exist.
With your shown samples/attempts, please try following htaccess rules file. Make sure to keep your index.php file is present in website folder and htaccess is present along side with website folder(not inside it).
Please make sure to clear your browser cache before testing your URLs.
##Enabling rewrite engine here.
RewriteEngine ON
##Checking conditions for non-existing pages here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##performing internal rewrite here to index.php file.
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ $1/index.php?p=$2&id=$3 [QSA,L]

.htaccess mod_rewrite exclude root

I have a project with this code in the .htaccess:
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite all other queries to the front controller.
RewriteRule ^ %{ENV:BASE}/app.php [L]
So now every request (except the explicit files) is sent to the /app.php.
I would like to change that behavior:
If someone requests the domain without any further file or directory the index.php should be displayed. In all other cases the given behavior should be performed.
How to achieve this?
To exclude the root folder you can use the following condition
RewriteCond %{REQUEST_URI} !^/$
Try :
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ %{ENV:BASE}/app.php [L]

Change URL for all php files in a folder using .htaccess

Does anyone know how to change the URL for all php files inside a subfolder of the root directory and also remove .php?
http://localhost/pages/x.php
to
http://localhost/x
Example:
http://localhost/pages/example.php
to
http://localhost/example
You can do it like this, which only executes if the request exists in pages and doesn't exist in root. So existing items in root don't get rewritten.
RewriteEngine on
RewriteCond %{CONTEXT_DOCUMENT_ROOT}/$1 !-f
RewriteCond %{CONTEXT_DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^([^/]+)$ pages/$1.php
RewriteEngine On
RewriteRule ^x$ page/x.php [L]
RewriteRule ^example$ pages/example.php [L]

Exclude a directory from being redirected in .htaccess

OK, so we were working in the wrong directory, meh!
We have a .htaccess file that is setup to redirect some files to a PHP script. Now we are adding some hard files which we want to bypass this redirect. So far this does not seem to be working and we are stumped.
Below is our initial .htaccess file contents after starting the engine which is working for another app already:
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
We are trying to load files now from /directory/ So we have tried adding each of these to the RewriteCond section:
RewriteCond %{REQUEST_URI} !directory
RewriteCond $1 !^(index\.php|i/|robots\.txt|favicon\.ico|directory)
Neither seems to be working. Any ideas on how to get this to load?
Examples:
Should redirect
http://example.com/thisredirects/
Should not redirect
http://example.com/directory
http://example.com/directory/
http://example.com/directory/index.php
etc.
have you tried making a second .htaccess file in the /directory dir and doing a NoRewrite in it?
Try this:
Wont redirect
RewriteRule directory/.* - [L]
Redirects
RewriteRule ^$ thisredirects/ [L]
RewriteRule (.*) thisredirects/$1 [L]
It seems you want to redirect anything that isn't actually an existing file in your docroot to your index.php. This little bit of rewrite should handle that:
RewriteCond %{REQUEST_FILENAME} -s [OR] # file with size
RewriteCond %{REQUEST_FILENAME} -l [OR] # file is a link
RewriteCond %{REQUEST_FILENAME} -d # file is a directory
RewriteRule ^.*$ - [NC,L] # don't rewrite [last rule]
RewriteRule ^(.*)$ /index.php/$1 [NC,L] # everything else gets sent to index.php
The [OR] operator may be what you are looking for on the RewriteCond as well.
Also if you just want to whitelist the /directory portion you could put a Rule before your redirects that is marked [L] for "last rule"
RewriteRule ^/directory.*$ - [NC,L]