htaccess with specific folder exception - .htaccess

I've set up an htaccess file in order to create a small routing structure where urls redirect to index.php. I want to add an exception where if the user types in "/admin" and anything after, the url instead redirects to admin.php.
htaccess -
RewriteEngine On
RewriteBase /
RewriteRule ^admin/?(.*)$ admin.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Example: http://mysite.com/admin/settings/ should lead to admin.php?page=settings, while http://mysite.com/about/ should lead to index.php (not using any parameters here). Regexp is really not my cup of tea, especially when there are multiple conditions to consider. Ideas?
Edit: ^admin/([^/]*)/? solved it.

You may try this in one .htaccess file in root directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !admin\.php [NC]
RewriteRule ^admin/([^/]*)/? /admin.php?page=$1 [NC,L]
RewriteCond %{REQUEST_URI} !/admin/ [NC]
RewriteRule .* /index.php [NC,L]
For permanent redirection, replace [NC],L] with [R=301,NC,L]

Related

htaccess - rewrite url from /test to index.php?page=test

I'm trying to rewrite URLs using htaccess. The idea behind this is quite simple:
Once a visitor visits https://example.com/pineapples, the browser should load https://example.com/index.php?page=pineapples instead while remaining the original URL.
This should only happen when a file exists in /pages/ with the name of the requested URL, in this case pineapples.php.
This also counts for subdirectories: https://example.com/pineapples/flamingos should redirect to https://example.com/index.php?page=pineapples/flamingos when the file exists in /pages/, in this case /pages/pineapples/flamingos.php, where pineapples is a subdirectory.
Now I've tried to do this, but it's very buggy. It doesn't work for subdirectories as explained above, and it seems to work for all the directories, instead of just /pages/.
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif|svg)$ [NC]
RewriteCond %{REQUEST_URI} !^/pages(/|$)
RewriteRule ^([^/]+/[^/]+/). /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
If someone please can help me out get this to work, I will be very grateful!
This question has been answered using the following htaccess.
Options -Indexes +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif|svg)$
RewriteRule ^(ajax|assets|auth)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
You can't check REQUEST_FILENAME because in your case it has to be in pages
so you may want to try
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteLog /path/to/log
RewriteLogLevel 5
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif|svg)$ [NC]
RewriteCond %{REQUEST_URI} !^/pages(/|$)
RewriteRule ^([^/]+/[^/]+/). /$1 [R=301,L]
RewriteCond %{DOCUMENT_ROOT}pages%{REQUEST_URI}.php -f
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
Also I have enabled rewrite log, so in case it doesn't work, please post the logs your get

Exclude folders when redirect with htaccess

I have a question.
I want to redirect domain.com/var to domain.com/id=var
I can do that, but when users write a var like my folders, the htaccess redirect to the folder first :/
This is my actual htaccess
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9-+/]+)$ index.php?id=$1 [L]
RewriteRule ^([a-zA-Z0-9-+/]+)/$ index.php?id=$1 [L]
But when the user write for example css, the htacces redirecto to domain.com/css/?id=css
And the second problem is if the user put / after the var, example domain.com/var/
How can I change that? Thx 4 all!!
You need to skip files/directories from rewriting:
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9+/-]+)/?$ index.php?id=$1 [L,QSA]

.htaccess rewrite to subfolder with codeigniter rewrite

I have a site that needs to exist in a subfolder
example.com/site
But i'm trying to use the .htaccess to remove any links that contain www (to make sure codeigniter csrf doesn't throw errors), so i've added
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/site/$1 [R=301,L]
This works well when there is a page identifier specified, so
www.example.com/site/book rewrites to example.com/site/book
But when there is no page identifier specified I get a 404
www.example.com/site rewrites to example.com/site//usr/local/pem/vhosts/103480/webspace/httpdocs/new
I was wondering if anybody could point me in the right direction?
This is my full .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/site/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
You may try this instead:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{REQUEST_URI} ^/(.*) [NC]
RewriteRule .* http://example.com/%1 [R=301,L]
Maybe, you're just missing a RewriteBase
Depending on where the .htaccess file is, try either
RewriteBase /
or
RewriteBase /site
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

.htaccess redirect all sub-directories to file

I have been trying to find a method to redirect all directories except a few to a file with a variable
Example:
site.com/test -> index.php?a=test
I want all subdirectories to be forwarded except for the following
/images
/admin
/includes
I have tried the following withough success.
RewriteCond %{REQUEST_URI} !^/images(/|$)
RewriteCond %{REQUEST_URI} !^/admin(/|$)
RewriteCond %{REQUEST_URI} !^/includes(/|$)
RewriteRule ^([\w/]*)$ index.php/?a=$1 [L]
Is there a way to complete this using .htaccess
EDIT:
I found this which is closer to what im looking for but it needs to be updated to allow access to /images /admin /includes.
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?a=$1 [NC,L,R=301]
In .htaccess you can use this rule
RewriteEngine On
RewriteRule !\.(js|jpeg|ico|gif|jpg|png|css|xml|html|swf|zip|tar|pdf|flv|gz|wmv|avi|rar|mp3)$ index.php [L]
In your application you can get $_SERVER["REQUEST_URI"] and manipulate as you whish!
<?php
$route = explode('/', $_SERVER['REQUEST_URI']);
print_r($route);

htaccess 301 redirect example.com/beta/xyz to example.com/xyz

I need a general rule that maps every URL in the /beta subdirectory to the corresponding URL in the root; essentially I need to remove /beta from all URLs.
In case it makes any difference, the URLs are dynamically generated by WordPress.
Currently my .htaccess file is:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#END WordPress
Can you tell me where to put the new lines?
Thank you!
Could you try this?
RewriteEngine On
RewriteRule ^beta/(.*)$ http://example.com/$1 [R=301,L]
Your question is not entirely clear, but I would bet that what you want is having a Wordpress installed in somedir/beta appear in yoursite.com/ instead of yoursite.com/beta/. The rules you pasted are in somedir/beta/.htaccess, and are the default Wordpress rules. You must leave those alone.
What you need for that is to put the following rules in the root directory, as in, somedir/.htaccess, after changing example.com to your own domain. Your webserver first reads this root .htaccess, and when it does, it will know to rewrite requests to /beta.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/beta/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /beta/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ beta/index.php [L]
</IfModule>
More info in the Codex:
https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

Resources