htaccess redirects unexpectedly to folder - php

I have strange for me problem. I'm developing Zend Framework application and have this file structure:
/
/application/
/public/
/public/.htaccess
/public/index.php
/public/js/
/public/style/
/public/profile/
/.htaccess
My domain point to folder /
When I enter the address example.com/profile/ it goes to the controller profile and this is good.
When I enter the address example.com/profile the server redirects me to:
example.com/public/profile/
I would like to have a solution that whenever I request:
example.com/profile/ or
example.com/profile
The same page will be rendered but second version gives me a redirect and I don't know why.
The /.htaccess file is:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
The role of this file is to route all traffic from / to /public but without any redirects.
The /public/.htaccess file is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Can anybody help me with this? Thanks.
I fixed this. Here is the solution if somebody have the same problem:
To fix this you have to set the following options and disable DirectorySlash
Options -Indexes FollowSymLinks
DirectorySlash Off
Now Apache shouldn't add trailing slashes at the end of uri when you pointing to directory.
This also disable redirect to the uri with trailing slash.

Optionally match the last /. Change your .htaccess file to this:
RewriteEngine On
RewriteRule ^(.*)/?$ public/$1 [L]

This works for me:
RewriteEngine On
RewriteRule ^/$|^(.*)$ /public/$1 [QSA,NC,L]

Related

Remove Subfolder[Codeigniter] only for specified URLs using Htaccess

My Folder Structure
Root Domain
assets/ (contains css, images, js folders)
subfolder/ (contains Codeigniter Project inside and seperate .htaccess)
index.php
contact.php
.htaccess
Root Htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)subfolder
RewriteRule ^(.*)$ subfolder/$1 [L]
Subfolder(Codeigniter Project) Htaccess
DirectoryIndex index.php index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|install|update)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For godady Shared Hosting Server uncomment the line below
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
List of Subfolder URLs
http://example.com/subfolder/products/
http://example.com/subfolder/product/product-slug-123
List of Root URLs
http://example.com/index.php
http://example.com/contact.php
In my Codeigniter Project I've set Products controller as default_controller.
So the problem is, when I try to access the index.php page in the Root folder it only displays the Codeigniter products list as Home page.
I need following URLs:
http://example.com/index.php
http://example.com/contact.php
http://example.com/products/
http://example.com/product/product-slug-123
Any solutions with htaccess or with code?
Finally I got this working.
RewriteCond %{REQUEST_URI} !((.*)subfolder|^/assets) // to Exclude this
RewriteCond %{REQUEST_URI} (^/products(.*)|^/product(.*)) // to Include this
RewriteRule ^(.*)$ subfolder/$1 [L]
Exclamation(!) symbol used to 'not' matching urls.
So I removed Exclamation symbol and used another RewriteCond (Condition) to redirect only if the url contains segment as products or product http://example.com/products/ or http://example.com/product/product-slug-123. It's working perfectly now. I thought, sharing this would be useful to someone. Happy coding.

Treat .php File as Root Folder

So right now I have a index.php file that acts as an API in the root of a subdomain.
So if you go to http://some.domainname.com/index.php/items/ for example you get a response.
However if you go to http://some.domainname.com/items/ you get a 404 error.
I'm assuming there is some .htaccess rewrite command I can add to make this work but I haven't had any luck finding it.
Here is an example based on how Magento does it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
It will effectively remove the index.php from your urls, but it won't stop anyone from accessing a file that actually exists, like www.example.com/i-exist.php
Note:
This is specifically geared toward a site whose urls alway end with a trailing slash (/), like www.example.com/index.php/somepage/. This is set with the RewriteBase / line.

CakePHP without URL rewriting: redirect / to index.php

I'm using a CakePHP framework on a shared hosting and cannot turn on URL rewriting.
The installation is in a folder called /tc. Everything works fine when I open /tc/index.php/Controller/Action.
However, I would like to make it a little easier for my users so that they do not have to open /tc/index.php but only /tc (which then should be redirected to /tc/index.php).
How do I do that with .htaccess?
Thank you!
Clarificaiton
I want a request on /tc/ to be redirected to /tc/index.php
SOLUTION
Worked for me with a simple
RewriteEngine On
RewriteRule ^$ index.php/$1 [L,QSA]
CakePHP handels the rest because all the links point to /tc/index.php/Controller.
Thanks for your help!
Try Using following code in .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
IndexIgnore *
</IfModule>
try this :
1) open " mod_rewrite " from apache server.
2) comment " Configure::write('App.baseUrl', env('SCRIPT_NAME')); " this line from /confige/core.php
Below is the rule of .htaccess by which you can remove index.php from URL means if anyone enters http://example.com/index.php it redirects to http://example.com/.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^/]+/)*index.php HTTP/
RewriteRule ^(([^/]+/)*)index.php$ http://www.%{HTTP_HOST}/ [R=301,NS,L]
Above are the two rules by which you can redirect index.php to root directory.

mod_rewrite not working correctly - accessing static resources

Right now I have a few folders (img, css, js, and ico) in the root directory of my website. However, I want to move them into a new directory called public_html. For example, instead of the image folder being located in /img, it would be instead located in /public_html/img. But I'm having problems doing this, and i suspect it's a problem with my .htaccess file.
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 static/404.html
ErrorDocument 403 static/403.html
RewriteCond %{REQUEST_FILENAME} !-f
# ---------- Custom Routes ----------------
# -----------------------------------------
RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA]
I can access /public_html/css/style.css just fine, but when I add in that first RewriteRule line and try to access /css/style.css, it doesn't work.
Can anyone figure out why? Thanks.
Add the [L] flag to the first rule. Otherwise it drops through to the second rule and attempts to pass it in r= to index.php. Accessing it via public_html works because of the RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2 [L]
# Update: Try using the RewriteCond before this line
# as well as where you have it earlier
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA]

i need some help creating Htaccess

i need to hide the extensions of my webpage and also want to let the user put the links in both (lower and upper) case:
Example:
the file name is demo.php
www.example.com/demo
www.example.com/DEMO
www.example.com/Demo
Running PHP in a LAMP server, no access to php.ini, just .htaccess
Actualy im using a file like this:
RewriteEngine On
RewriteBase /
RewriteRule ^/(OUTSOURCING|outsourcing|Outsourcing)$ outsourcing.php [NC,L]
And i m reciving this error:
Not Found
The requested URL /outsourcing was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
RewriteEngine On
RewriteBase /
www.example.com/DEMO www.example.com/Demo or doing www.example.com/DEMO/page2
RewriteRule ^/(DEMO|demo|Demo)/(.*)$ demo.php?=$1 [NC,L]
RewriteRule ^/(DEMO|demo|Demo)$ demo.php [NC,L]
RewriteRule ^/(D|d)emo$ demo.php [NC,L]
or pass anything www.example.com/DeMo www.example.com/bob to demo.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ demo.php [NC,L]
you may want to test if your allowed .htaccess RewriteRule /*$ http://google.com [R][L]
here is a good way to do it with case insensitive method
EDIT:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ${lc:$1}.php [NC]
this way anything entered will be redirected to a php file.
edit : this way your js and css file can still run
RewriteRule ^/[Dd][Ee][Mm][Oo]/?(.*)$ demo.php [NC,L]
will redirect any capitalization of "Demo" to demo.php
First add this line in the <VirtualHost> section OR at the end of your httpd.conf file (to enable lc function in .htaccess for later use):
RewriteMap lc int:tolower
Then have these rules in .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond ${lc:%{REQUEST_FILENAME}}.php -f
RewriteRule . ${lc:%{REQUEST_URI}}.php [L]
First rule in .htaccess is doing external redirect by making a URI of /index.php to /index
Second rule in .htaccess is doing internal redirect by makign a URI of /INDEX to /index.php by lowercasing the URI. Assuming you have filename index.php physically present.
That way you can always write URLs without .php extension.

Categories