rewrite other requests to index.php does not work completely - php

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?

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>

Why does my rewrite rule only work on specific word?

I need a rewrite rule, which rewrites every request to index.php, except when the word web is in the url. So, every image, JS, CSS file is located under the web folder. Here are my .htaccess file which currently does the trick:
<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]
RewriteRule ^(.+)$ index.php [QSA,L]
</IfModule>
This works for:
http://localhost/~alpham8/application/index/index/
http://localhost/~alpham8/application/
http://localhost/~alpham8/application/index/someaction/
http://localhost/~alpham8/application/web/img/someimage.png
But it does not work for:
http://localhost/~alpham8/application/somecontroller/someaction/
The none-working URL´s returns me an 404 error.
Please help me.

Use. htaccess to rewrite urls in a subdirectory only

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.

mod_rewrite: Redirect to cache or script

I'm having some troubles with my mod_rewrite configuration, for a "cache" solution that I'm working on. I have directory called "cache" with compiled html files. I also have an index.php file which handles all the requests.
Right now all requests are rewritten to the index.php file like: "/articles" => index.php?q=articles.
But now I want it to rewrite to "cache/articles/index.html" if it exists, or else just fall back and rewrite to the index.php script.
I have messed around with the rewrite for a while know, but I can't get it to work. The rewrite code looks like this now (without anything for the cache):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Thanks in advance :)
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#if the index.html exists in the cache
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}/index.html -f [NC]
#then display it
RewriteRule ^ cache%{REQUEST_URI}/index.html [L]
#other existing rules go here

Categories