Use. htaccess to rewrite urls in a subdirectory only - php

I am trying to configure an .htaccess file to reroute all urls to the nearest index subdirectory, like this:
http://example.com/admin/blah/test/whatever should stay in the address bar, but point to:
http://example.com/admin/blah/test/whatever/index.php if it exists, otherwise:
http://example.com/admin/blah/test/index.php if it exists, otherwise:
http://example.com/admin/blah/index.php if it exists, otherwise:
http://example.com/admin/index.php
If the url is that of an actual file, it should always go to that file. so http://example.com/admin/blah/file.css or http://example.com/admin/blah/item.inc should all still work if they exist, otherwise it should redirect to index.php in that folder, or nearest parent folder like shown above.
I'd also like to have this only affect a subfolder, if that's at all possible. In the above example, I'm assuming the .htaccess file would go in the /admin/ folder.
Update:
Here's what currently works:
# No Directory Listings or trailing slashes
Options -Multiviews -Indexes +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash Off
# See if the current request is a directory containing an index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^(.*)/?$ $1/index.php [L,QSA]
# if index.php doesn't exist in current dir then
# forward to the parent directory of current REQUEST_URI
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1$2/index.php !-f
RewriteRule ^(.*?/)?([^/]+)/?$ $1 [L,QSA]
</IfModule>
This mostly works. If I type in http://example.com/admin?p=2 it resolves as expected, but if I use the URL http://example.com/admin/?p=2 it ALSO resolves, without explicitly removing the trailing slash.

This is fairly tricky rule. Try this in your your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# if index.php doesn't exist in current dir then
# forward to the parent directory of current REQUEST_URI
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1$2/index.php !-f
RewriteRule ^(.*?/)?([^/]+)/?$ $1 [L]

Here's what eventually worked.
In my .htaccess file I had:
# No Directory Listings
Options -Multiviews -Indexes +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash Off
# see if the current directory contains an index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^(.*)/?$ $1/index.php [L,QSA]
# if index.php doesn't exist in current dir then
# forward to the parent directory of current REQUEST_URI
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1$2/index.php !-f
RewriteRule ^(.*?/)?([^/]+)/?$ $1 [L,QSA]
</IfModule>
Links to existing directories look for an index.php in that directory. Links to existing files display that file. Everything else redirects to my index.php. From there, I can read any queries with $_GET as normal and I can read and parse the full URL to see what page to display. This also works in a subdirectory if I don't want it to apply to an entire site.

Related

Removing 2 folders from URL and redirecting to page

I am creating a new web app and this is how my project folders look like:
So as you can see I have renamed that htaccess in the root folder because I am not using that one and I am using the one situated inside of the public folder. I am trying to change my URLs from this:
http://example.site/public/pages/about
to:
http://example.site/about
I was able to remove the .php from the file extensions but the code I added to remove the folders from the URL is not working.
Note: in the root folder (App/index.php) the index.php file simply has this:
<?php
header('Location: public/pages');
so I am just using this to send to the correct folder.
This is what I have so far:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /App/public/
RewriteRule ^pages/(.+)$ /$1 [L,R=302,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) $1.php [NC,L]
I have seen this question/answer from this url:
Remove / Rewrite 2 directory names from URL
but it is not working for me. Looking forward to anyone who can help, thanks.
Assuming ``htdocs/app/is yourDocumentRootforapp.blu` domain.
Inside htdocs/app/public/.htaccess you may use this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# if URL has /pages/ then remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^pages/(.+)$ /$1 [L,R=301,NC]
# landing page for app subdomain to show index.php
RewriteRule ^$ pages/index.php [L]
# check & rewrite if a matching .php file exists in pages/ subdirectory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/pages/$1.php -f
RewriteRule ^(.+?)/?$ pages/$1.php [L]
Besides this you will need to use this code in htdocs/app/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# send all the traffic to /public/
RewriteRule .* public/$0 [L]

Make HTACCESS to not be applied for some subfolders

I am using this htaccess for my application.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any URL I will type in my browser, if the respective file doesn't exist, it will open the index.php.
The problem is I have many applications like this installed on the same domain.
For example, the root contains an index.php file and a htaccess like above.
I have also, /store, which contains a index.php file and a htaccess like above, too, but when I access /store/something it opens the index.php of root (not the correct /store/index.php
How can I solve this problem (how to make the htaccess of root to not override the htaccess of /store)? Thanks!
You can have a skip directory rule to skip all the directories:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# skip index.php and some directories
RewriteRule ^(store/|site1/|site2/|index\.php$) - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

.htaccess PHP - Prevent rewriting when only domain is entered

I have a site located at
application
controllers
views
...
public <- the root of the site
index.php <- entry point
css
...
So the url is http://localhost/application/public. I would like when the user enters only http://localhost/application/public to do nothing except calling index.php without showing it.
Here are some examples:
http://localhost/application/public -> http://localhost/application/public or http://localhost/application/public/
BUT
http://localhost/application/public/asdf -> Rewrite internally to http://localhost/application/public/index.php and show only http://localhost/application/public/asdf
I have written the .htaccess. The only problem is that when entering http://localhost/application/public it opens http://localhost/application/public/index.php instead of http://localhost/application/public
One more thing - I want existing directories to be rewritten, so only existing files should not be rewritten to index.php So basically http://localhost/application/public/css should be rewritten to index.php. The reason I want this is to ommit the 403 Forbidden error, and kindly tell the user that the path does not exist.
HTACCESS
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /application/public/
#Remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
#Do the rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L]
The reason I put RewriteCond %{REQUEST_FILENAME} !-d is to prevent http://localhost/application/public from rewriting. If http://localhost/application/public is rewritten, it loads http://localhost/application/public/index.php
Actually I know there are needed some modifications on RewriteCond %{REQUEST_FILENAME} !-d. I want the root, public folder to not get rewritten, it should be the only exception. All of the rest physical folders like css, js, have to be rewritten, like not existing. Files should not be rewritten. Thanks!
Ok, I think I found a solution.
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /application/public/
RewriteRule ^(css/?|js/?)$ index.php [NC,L] <- Folders css, js will be rewritten, but root directory won't be rewritten. Files inside them also won't be rewritten.
#Remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
#Do the rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L]

rewrite other requests to index.php does not work completely

I got the following .htaccess for my apache2 webserver in e. g. "some-folder":
<IfModule mod_rewrite.c>
RewriteEngine On
# Folders / files to exclude from rewrite divided by Pipe goes here:
RewriteRule (^|/)web(/|$) - [L,NC]
# turn empty requests into requests for "index.php",
# keeping the query string intact
RewriteRule ^$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !favicon.ico$
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
for http://host/some-folder/index/index it works, but for http://host/some-folder/ajax/test it doesn´t. It should redirect both to index.php.
What went wrong?
Edit: If I set the RewriteBase to
RewriteBase /~alpham8/ephp/
it works.
So, the folder under which the .htaccess file is located (in my case a home_dir). But how can I do the same without knowing the current directory under which the .htaccess file is located?

how to fix url with htacces

I have a url which initially http://web.com/index.php/home/funct/ and I want to change this by removing the index.php so that it becomes http://web.com/home/funct/
If you are using Apache server, create a file called .htaccess and put this in it:
<IfModule mod_rewrite.c>
RewriteEngine on
# If the file/dir is NOT real go to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>
Then just remove the reference to "/index.php/" from your links and when the server doesn't find "home/funct" it will go looking for "/index.php/home/funct".

Categories