htaccess only in root folder - .htaccess

I was looking for an answer, but didn't find it. I have .htaccess in root file which redirect /loremipsum/ to index.php?url=loremipsum, but, it also wants to work in subdirectories. I have a lot of them, so putting .htaccess file in everyone would be painful. Is there any way to make it working only in root folder?

if you add this rules it will only work on files and folders that didnt exist
Options -MultiViews +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ index.php?url=$1 [L]

To make your redirect rule work only in root dir use this negative lokahead based code:
Options -MultiViews +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)(?!/.*)$ index.php?url=$1 [L]

Related

Infinite amount of rediretcs with .htaccess in subdirectory

I'm trying to get a webpage running which might not get any support anymore, so I have to fix this on my own. So in the root directory, I have this .htaccess file:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*) resources/$1 [L]
So all the content gets redirected to the resources/ folder. Now this kinda works, as the frontpage shows all the static content (CSS, JS, Images). But when I try to open mydomain.com/admin, I'll get an HTTP 500. The logfile says
AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
The .htaccess file in the sub-dir has this content:
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?param=$1 [QSA,L]
For my basic Apache knowledge this does seems quite right; but is it, or do I have change Apache settings? (The VPS is a kind of PaaS-thing, so I can access/change some things)
Thank you.
Have your root htaccess rule file in following way(you need to put a condition for a check before rewriting).
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/resources [NC]
RewriteRule ^(.*)$ resources/$1 [L]
Then for your sub directory level htaccess file try following:
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?param=$1 [QSA,L]
Also in case you want to apply same rules which you have in your root directory to sub-directories then add RewriteOptions InheritBefore before RewriteEngine ON in your sub directory level htaccess file.

Using htaccess to remove sub-directories from URL in CodeIgniter

My CodeIgniter project is placed inside sub-folders and I want to hide those two sub-folders with .htaccess.
Everything is working fine but my current URL looks like this:
example.com/folder1/folder2/ci_project/class/function/$id
I want this to be:
example.com/class/function/$id
Now, I am using this simple .htaccess code in order to remove the index.php from URL:
Options +FollowSymLinks
RewriteEngine on
# Send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Any Ideas?
when i use the following code in htaccess file inside folder1/folder2/ci_project, it does hide the sub directories but i get the error "Object not found"
RewriteCond %{REQUEST_URI} !^/folder1/folder2/ci_project/
RewriteRule ^(.*)$ /folder1/folder2/ci_project/index.php/$1 [L]
Move your current .htaccess file from your ci_project/ to the root of domain, and replace these code inside it:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(robots\.txt|favicon\.ico|public)
RewriteRule ^(.*)$ /folder1/folder2/ci_project/index.php/$1 [L]
</IfModule>
In this case, you should place your public files in a folder like public at the root of the domain.
You should add other folders (like public) to RewriteCond if you don’t want them to be treated by RewriteRule.
If multiple application are exist in the same domain, you might want to consider this option:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder1/folder2/ci_project/index.php/$1 [L]
</IfModule>

How do i redirect a non-existent folder to similar folder?

I am creating a website where I want the members profile links as follows:
somedomain.com/someuser1/ -> somedomain.com/#!/home/someuser1/
somedomain.com/someuser2/ -> somedomain.com/#!/home/someuser2/
etc...
because i don't want to fill the root folder up with a bunch of member profile folders. Can this be done easily in the .htaccess file with rewrites?
I believe i would need to pull the 'someuser1' and 'someuser2' values and append them during the rewrite, but i'm not sure how to do that... the member folders will exist in the /home/ folder, but not the root folder...
Thanks!
Greg
Try this code in your .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ /#!/home/$1 [R=302,NE,L]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Will this work? Put this in the .htaccess file. Untested
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+) /#!/home/$1 [R=301,NC,L]

Htaccess if doesn't exists, doesn't work

I don't have much experience with htaccess. But I have this script:
Options +FollowSymLinks
ErrorDocument 404 /index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ index.php?doel_url=$1 [L,QSA]
But if there exists a file, he just listen to this rule.
Help!
website.nl/categorie is not a file and is not a directory.
if you need categorie to go to the file you will need to make sure it has a file extension (in your case .php) so it should be website.nl/categorie.php to avoid being redirected to the index.php page
Options -MultiViews should fix that.

htaccess not working in subdirectory

Hi I have an admin folder that has pretty much the same custom system as my root folder so I need htaccess to make the URL's look nice so in root I have:
example.com/home and admin should be example.com/admin/home.
My root's htaccess is
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?p=$1 [QSA,L]
This works fine but no matter what I do when I have a copy of this in the admin folder it does not work. I have tried RewriteBase I have tried doing the admin folder from the root htaccess but anything I try always makes the pages 404 or 500 error.
Admin htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?p=$1 [QSA,L]
Are you sure that you have set properly admin folder's AllowOverride directive? You can set that in your site's configuration file.

Resources