Exclude root directory from htaccess redirection - .htaccess

This is my .htaccess but I would also like to exlude the root directory ('mysite.com' slash nothing) from the rule.
RewriteCond %{REQUEST_URI} !(wp-admin|index.php)
RewriteRule ^([a-zA-Z0-9\D]+)$ /index.php?username=$1 [L]
Any thoughts? :)

To exclude the root dir, put this condition above your rule
RewriteCond %{REQUEST_URI} !^$

Related

How to make redirect in htaccess except for one folder and its directories and files inside and one path for cms Joomla

How to perform redirection in .htaccess file except for one folder and its directories and files inside and one path
I would like to perform the redirect as follows
from domain
www.olddomain.org
www.olddomain.org/^ (any path)
to the domain:
www.newdomain.org
www.newdomain.org/^ (any path)
BUT except for
www.olddomain.org/administrator/test/test1/test2
www.olddomain.org/administrator/ (any path)
AND
www.olddomain.org/test-article
I would like to add that I want to make these redirects for the Joomla 3 CMS system
I try:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^olddomain.pl
#redirect them to new-example
RewriteCond %{REQUEST_URI} !=/administrator/^
RewriteCond %{REQUEST_URI} !=/testarticle
RewriteRule (.*) https://newdomain.pl/$1 [R=301,L]
and
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/administrator.*/$ [NC]
RewriteRule .* https://www.newdomain.com/ [R=301,L]
But it doesn't work the way I want
Try this one:
RewriteEngine On
RedirectMatch 301 ^/((?!administrator).*)$ http://newdomain.org/$1

htaccess redirect root to subfolder - but keep other root subfolders beyond

My server has several system folders and files by default. so for better overview I want to put my website in a subfolder like this:
www.domain.de/web/index.html
ALL links and website-related stuff is found in this subfolder "web". BUT the folder "web" should not be seen in the url!
I got this with these lines in my htaccess and it works:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
RewriteCond %{REQUEST_URI} !^/web/.*$
RewriteRule ^(.*)$ /web/$1
But now the problem:
I also want other folder on my server to work. So www.domain.de/downloads/ should work, too. This folder is NOT in the subfolder "web". So if I write the direct url "www.domain.de/downloads" in my webbrowser, I don't want to get a redirect to "www.domain.de/web/downloads/", you know?
Is there any solution? :)
Thanks!
You Can use a RewriteCond to exclude your folder :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
RewriteCond %{REQUEST_URI} !^/download [NC]
RewriteCond %{REQUEST_URI} !^/web/.*$
RewriteRule ^(.*)$ /web/$1

htaccess exclude folder from rule

I have the following working rule in my htaccess file:
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
However this prevents my admin panel to work. Is it possible to exclude a directory and its subfolders from this rule?
Exclude folders like this (exclude panel folder):
/panel/
/panel/a-subfolder/
/panel/a-subfolder/deeper/
They should work without the rule, without the ending slash as well like /panel/a-subfolder
Don't exclude folders like this:
/another-unknown-folder/
/some-other-folder/folder/folder2/
They should follow the rule, force slash as the rule does today.
You can add an additional RewriteCond for exclusion:
RewriteCond %{REQUEST_URI} !^/panel/
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

.htacces Nice urls for subfolder

I have 1 subfolder located at my root folder:
/root/task6/index.php
And I need to change urls www.andriussulcas.prinusprojects.lt/task6/index.php?p=naujienos&title=title to www.andriussulcas.prinusprojects.lt/task6/naujienos/title
I've been trying to do this for some time now and all my efforts weren't successful.
While searching for answer, I started thinking that maybe my mod_rewrite wasn't activated. But later on I found how to redirect my web site root to subdirectory, where all my files are. So mod_rewrite works.
I found some similar answers here, on stackoverflow, like how to "rewrite rule" for a sub-sub-directory? .htaccess/php but for some reason it doens't work for me.
Here's my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?andriussulcas.akademija.prinusprojects.lt$
RewriteCond %{REQUEST_URI} !^/task6/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /task6/$1
RewriteCond %{HTTP_HOST} ^(www.)?andriussulcas.akademija.prinusprojects.lt$
RewriteRule ^(/)?$ task6/index.php?p=naujienos [L]
RewriteRule ^naujienos/([A-Za-z0-9-]+)?$ index.php?p=naujienos&title=$1 [L]
.htaccess file is located at my root directory, and there are no other .htaccess files at subdirectories.
All help is much appreciated.
Try replacing your last rule with this:
RewriteCond %{HTTP_HOST} ^(www.)?andriussulcas.akademija.prinusprojects.lt$
RewriteRule ^naujienos/([A-Za-z0-9-]+)$ /task6/index.php?p=naujienos&title=$1 [L]

.htaccess mod_rewrite - limit to root directory

How to apply a rule only to the root directory (where it's placed)?
How about putting:
RewriteCond %{REQUEST_URI} /([^/]*)
before any of your rules. That will match on any number of characters that are not a slash.
I would use the following
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/[^/]*(\?.*)?$
# your rewrite code here

Resources