Can't access directories after setting rewrite rules - .htaccess

I can't access directories on my host/server, after setting htaccess rewrite rules.
The rule is:
RewriteRule ^([A-Za-a0-9-]+)/?$ index.php?page=$1 [NC]
it's supposed to internally redirect http://domianname.com/index.php?page=login to http://domianname.com/login
but now i can't access directories on my server like http://domianname.com/admin.
What am i doing wrong?

You need rewrite conditions to skip files/directories from rewriting:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-a0-9-]+)/?$ index.php?page=$1 [L,QSA]

Related

htaccess rewrite subdirectory as root and also redirect if the subdirectory is accessed directly

I have URLs like this
https://example.com/public
https://example.com/public/faq.php
I want to rewrite it like this
https://example.com
https://example.com/faq.php
Rule I am using is this
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^$ public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/$1 [L]
This rule works very well but it doesn't redirect if someone access "https://example.com/public/" or "https://example.com/public/faq.php"
Solution
I have found a workaround for this. I have added below code to redirect if the public directory will be accessed directly.
RewriteCond %{THE_REQUEST} /public [NC]
RewriteRule .* / [L,R]

htaccess: redirect all non existing directories to index

I want to redirect all non existing directories to index.php.
This URLs should be redirected (examples):
/pages/page1/
/pages/page1
URLs not to be redirected because they are existing (examples):
/admin/
/admin
Any ideas?
Thanks!
You can use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^.]+$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d is the condition to check if it is not a directory.
^[^.]+$ is regex pattern to ensure there is no extension in request URI.
Try it like this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [R=301]

reroute htaccess when index in any subfolder

I currently have the following rewrite set up in my htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/(.*)$ index.php?controller=$1&cmd=$2&params=$3 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)$ index.php?controller=$1&cmd=$2 [L,QSA]
RewriteRule ^([^/]*)$ index.php?controller=$1 [L,QSA]
So for example blog just rewrites to index.php?controller=blog or blog/show/32 rewrites to index.php?controller=blog&cmd=show&params=32 etc etc.
I want to shift all my site into a subfolder called testing so need to update my htaccess to suit. Not worried about rerouting to the folder i.e. i'm not bothered about typing www.example.com and it landing at /testing/index.php, I want to type www.example.com/testing/ and it land at index.php which it does but the other rules seem to break.
If you move this .htaccess to the subfolder, together with the files, it will work just as expected. You don't need to change anything. If you have a RewriteBase directive in your .htaccess, you have to update it to reflect the new path.
Keep this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^((?!testing/).*)$ testing/$1 [L,NC]
Keep this rule in your /testing/.htaccess`:
RewriteEngine On
RewriteBase /testing/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/(.+)$ index.php?controller=$1&cmd=$2&params=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?controller=$1&cmd=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?controller=$1 [L,QSA]

redirect 301 subdirectory in combination with RewriteCond

I am trying to redirect an entire folder (containing hundreds of pages) to the same folder on another domain.
The site uses a CMS that uses rewritecond to generate pretty page url's:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This seems to be conflicting with my redirect.
When I use redirect 301 /folder/ http://www.site.invalid/folder/, the pages are redirected to http://www.site.invalid/folder/index.php?q=folder/pagename.html which returns a 404.
Could anyone point me in the right direction?
Instead of using a 301 like this:
redirect 301 /folder/ http://www.site.invalid/folder/
Try using a 301 like this, and placing it before the other rewrite:
RewriteRule ^/folder/(.*)$ http://www.site.invalid/folder/$1 [R=301,L]
Basically what will happen now is this rule will run first, do the 301 redirect (because it has a R=301 flag), and since it has a last flag (L) then it will stop the server from processing any more rewrites after that.
So your htaccess file would look something like:
RewriteEngine On
RewriteRule ^/folder/(.*)$ http://www.site.invalid/folder/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Keep your entire .htaccess like this:
RewriteEngine On
RewriteRule ^(folder/.*)$ http://www.site.invalid/$1 [R=301,NC,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?q=$1 [L,QSA]
In general keep your 301 rules before other catch-all rules.

htaccess RewriteRule not letting me acces REAL directories

This is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# Force add trainling slash
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://foo.bar/$1/ [R=301,L]
# Prepent www if not exist in URI
RewriteCond %{HTTP_HOST} ^foo\.bar$
RewriteRule (.*) http://www.foo.bar/$1 [R=301,L]
# If path is not a directory or file then apply RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite for profile view
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [NC,L]
# Rewrite for page view
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2&page=$3 [QSA,L]
# Condition to ignore regular links
RewriteRule ^library/templates/([^/]+)/([^/]+)/([^/]+)/?$ library/templates/$1/$2/$3 [QSA,L]
# Rewrite for dynamic page settings
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2&page=$3&$4=$5 [QSA,L]
# Rewrite for account view
RewriteRule ^([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2 [QSA,L]
# Rewrite for site default resources location
RewriteRule ^([^/]+)/([^/]+)/theme/s/([^/]+)/([^/]+)/([^/]+)/?$ library/themes/site/$3/$4/$5 [QSA,L]
I know this is kinda not the best way to go about this, feels like there is too much redundancy but I'm no expert but its working for me. If anyone can give me advise on how to make this more efficient I would highly appreciate it.
Ok so my problem is that I have a real directory at
www.foo.bar/path/to/real/dir
and I want to access a file in that directory, but my rewrite rules aren't letting access that directory unless I make a new rewrite rule redirecting my url to itself.
Check for files symlinks and directories as well (as your first rule):
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC,OR]
RewriteCond %{REQUEST_FILENAME} -l [NC]
RewriteRule .* - [L]

Resources