MOD_REWRITE but not in sub directories - php

I have the following HTACCESS code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_URI} !(/cms/|/js/|/mobile/)* [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /page.php [L]
As it stands, it works as I need it to within the root of the site, but accessing an admin page (/cms/), I can tell via PhpConsole that it is still hitting page.php. I think this is because the admin is controlled with a query string: URIs look like /cms/?view=pages&action=edit&id=4
If I uncomment the first condition, this problem no longer occurs, but my front end comes back with a 404 on files that don't exist, rather than redirecting to page.php
What is wrong with my code?

Change your code with this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|js|mobile)(/.*|)$)^.*$ page.php [L,NC]

Related

Omit folder and few scripts in mod_rewrite

I have following problem. My .htaccess looks like this
RewriteEngine On
RewriteBase /phpuserarea/
RewriteCond %{REQUEST_URI} !^mod/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]
ErrorDocument 404 /phpuserarea/404/index.php
But whenever I try to directly access anything in the mod directory the server responds with 404.
Other problem is, that besides the index.php I want to allow direct access to let's say testa.php and testb.php
Those are my ajax files and I can't seem to find a solution.
Have it this way:
ErrorDocument 404 /phpuserarea/404/index.php
RewriteEngine On
RewriteBase /phpuserarea/
RewriteCond %{REQUEST_URI} !/mod/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
You need leading slash in RewriteCond %{REQUEST_URI} and better not use anchor ^ as your .htaccess is inside /phpuserarea/.
Also you don't need to use ?%{QUERY_STRING} in target since query string is automatically passed over to target if your rule doesn't overwrite it.

How to get RewriteRule to redirect both existing and non-existing pages?

Goal: Send all requests, regardless whether the directory/file exists or not, to http://example.com/index.php
What I have tried:
Rewrite Engine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/index.php [L]
It redirects URL's of non-existing directories/files fine, but when I access an existing subdirectory and file http://example.com/test/test.php it only gives me the contents of the test.php page instead of the main index.php page.
If I understand correctly, the RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d tell it to perform the RewriteRule if those criterias are met, which makes sense why it only redirects on non-existing directories and files. But I've tried removing those RewriteCond's, and all it does is put my site into a redirect loop.
I've also tried doing
Rewrite Engine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://example.com/index.php [L]
But then it doesn't redirect at all.
What do I have to put to accomplish this?
Your first example would only redirect things that does not exist to your index.php as the conditions you have there:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Basically means if file or directory does not exist, do this.
Your second example is even worst as it tries to see if a file, directory exist and does not exist which makes no sense.
To redirect anything existent or not to a main handler in this case index.php all you need is the below:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule ^ index.php [L]
The condition makes sure you're not redirecting it to yourself to prevent a loop. If you're running HTTPD version 2.4 and above you can simple use:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^ index.php [END]
The flag [END] takes care of not executing any further redirects.

.htaccess mod_rewrite in main directory and subdirectory

I've a main directory with a .htaccess file that looks like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
The one in the subdirectory looks identical to this one. What I want the .htaccess files to do is redirect to the index file in each directory.
So if you visit main/example you get redirected to the index.php file in the main directory but if you visit main/subdirectory you get redirected to the index.php file in the subdirectory.
I've searched for an answer but haven't found anything that works.
Thankful for any help!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/ /$1/index.php [L]
This configuration do the following:
if we have this files
/index.php
/foo/index.php
/var/index.php
/var/boo/index.php
and you request the follow file
/foo/hello
you will go to:
/foo/index.php
and, if you request
/var/boo/world
you will go to:
/var/boo/index.php
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 -d
RewriteRule ^(.+)/ $1/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
You can use a simple solution like this one, no need to put .htaccess files everywhere
on your main directory you put one .htaccess file, you keep your code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
and you add those two lines
Options All -Indexes
ErrorDocument 403 http://domain.com/index.php
these two lines prevent the user to access the folder, and returns the statut 403 and then you redirect your 403 statut to the adress of your index.php of your main directory

mod_rewrite: multi-level URL using a request parameter

I currently have this set up and working fine inside a users folder.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?i=$1
e.g 127.0.0.1/site/users/admin goes to 127.0.0.1/site/users/index.php?i=admin
Now as this is for a profile page, how would i do something such as this.
users/admin/activity
So that it would show the activity page for that user? I am totally confused on how i would go about this.
Would it be best to make the index.php accept a page $_GET variable? But the how would i get htaccess to work around it?
You rules will look something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ index.php?i=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?i=$1 [L]
Now your index.php should be getting 2 $_GET variables, i and page.

htaccess behavior when accessing root

I've been using this htaccess code to pass on vars to redirect.php which handles the includes for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)/(.*)$ redirect.php?value1=$1&value2=$2&value3=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)$ redirect.php?value1=$1&value2=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ redirect.php?value1=$1 [L,QSA]
but i noticed that it will not go to redirect if all arguments is empty, so if i go to http://domain.com/ it will open index.php, but if i go to http://domain.com/any-param/ redirect.php handles it correctlty. How can I make it always use redirect.php as default, even when no additional URL parameters is set?
Your rules appear to be fine to me. Just add this line in the end:
RewriteRule ^$ redirect.php [L]
This will redirect http://domain.com/ to http://domain.com/redirect.php.
use to do it like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z]+)?/?([a-z]+)?/?([-a-z0-9]+)?/?$ [NC]
RewriteRule ^([a-z]+)/?([a-z]+)?/?([-a-z0-9]+)?/?$ /redirect.php?value1=$1&value2=$2&value3=$3 [NC,L]
Note: Even with only 1 param, this will still work as it just leaves the other 2 blank. So you don't have to copy and paste thew same code while removing just one condition.
I found that this is not very scalable. Now I just redirect everything to the index.php and let that file fetch and dissect the URI to handle my system layout. This allows for a very scalable system.

Categories