Block Access to Entire Folder and Instead Route to index.php - apache

I'm trying to write a .htaccess rewrite rule to redirect all traffic coming to domain.com/system/* back to domain.com.
I've written this so far:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1/ [L]
But it's not working as expected, if I go to:
domain.com/system/inexistent_file
it works. But if I go to:
domain.com/system/existing_file.php
It will open the specified file instead of showing index.php contents.
How should I fix this?

Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
# Match against the host, if you wish
#RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule system/(.*) /index.php?/$1 [L,R]
</IfModule>
It works for both existent and non-existent files.
The [R] flag is there because you stated that you want to redirect.

Related

Redirect using mod_rewrite in file .conf is not working

In my server apache, I need to redirect URLs in a .conf file. In my file http.conf, located in /etc/httpd/conf, one of the lines contains the following configuration:
Include conf.d/*.conf
In the folder the setting is indicating, in /etc/httpd/conf.d, I have a file called redirect.conf that has the following redirect code inside:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^PageSpeed=noscript$ [NC]
RewriteRule ^/?abracar/?$ /abracar? [L,R=301]
RewriteCond %{QUERY_STRING} ^PageSpeed=noscript$ [NC]
RewriteRule ^/?presente/decoracao/teu-sorriso/?$ /presente/decoracao/teu-sorriso? [L,R=301]
RewriteCond %{QUERY_STRING} ^p=2&PageSpeed=noscript$ [NC]
RewriteRule ^/?acessorios/confeitaria.html/?$ /acessorios/confeitaria.html? [L,R=301]
</IfModule>
This code should redirect the URL https://example.com/abracar?PageSpeed=noscript to https://example.com/abracar, but it is not working. If I put this code in my .htaccess file, it will work correctly, but due to external factors, I need to put this redirect into a separate .conf file. I have tried several different configurations, I researched a lot about it already, but nothing that could help me solve this problem. Thanks in advance!
I guess the easiest solution would be to drop the query string entirely?!
Note that QSD requires Apache 2.4 or newer.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^PageSpeed=noscript$ [NC]
RewriteRule ^ %{REQUEST_URI} [QSD,L,R=301]
</IfModule>
Added a more generalized and easier to comprehend (for humans) version:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [QSD,L,R=301]
</IfModule>
.+ is the generalized form of and query string that isn't NULL. and the RewriteRule now lists all parts of the URL, but I'd stick to ^ %{REQUEST_URI} for the real version, because both versions just rewrite every URL to itself.

.htaccess rewrite - filename.html to /?c=filename

I need it to work with all html URLs (except index), I tried using .htaccess with no luck.
For example... change this link from: http://www.example.com/signup.html to http://www.example.com/?c=signup.
Also if I enter directly to http://www.example.com/?c=signup, it must mirror http://www.example.com/signup.html.
EDIT
I found the way to do that, except for the case that I enter directly to the .html file, in that case, it does not rewrite.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{QUERY_STRING} ^c=([^&]+) [NC]
RewriteRule ^/?$ %1.html [NC,L,QSA]
RewriteRule ^/?$ %1.html [NC,L,QSA]
To redirect (presumably you meant "redirect", not "rewrite") from signup.html to /?c=signup then you can do something like the following before your existing rewrite:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ([^.]+)\.html$ /?c=$1 [R,L]
The condition that checks against the REDIRECT_STATUS environment variable prevents a redirect loop when your existing directive rewrites to the .html file.
Note that this is a temporary (302) redirect. Change the R to R=301 if this is intended to be permanent, but only once you have confirmed it's working OK.
Your existing directives could be tidied, in summary:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Redirect from signup.html to /?c=signup
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ([^.]+)\.html$ /?c=$1 [R,L]
# Rewrite from /?c=signup to signup.html
RewriteCond %{QUERY_STRING} ^c=([^&]+)
RewriteRule ^/?$ %1.html [QSD,L]
Presumably, you want to discard the c= query string from the rewritten substitution?

Apache URL Rewriting - Hide path in URL only when accessing specific path

I would like to rewrite my URL only when typing this:
www.domain.com/dir1/dir2/dir3/
in that path there is a index.html file that will be automatically loaded. And I'd like it to become this:
www.domain.com/index.html
It is important that the root www.domain.com does not redirect to that subdirs because it already redirects to another .php file.
I wrote the following .htaccess file:
RewriteRule ^dir1/dir2/dir3/(.*)$ dir1/dir2/dir3/$1 [L]
but it redirects even when typing the root domain.com.
Could you help me with that?
Edit:
I have tried in another browser and the real problem is that when typing domain.com/dir1/dir2/dir3 or domain.com/dir1/dir2/dir3/index.html the dir1/dir2/dir3/ is still shown and is not hidden.
Edit 2:
my complete .htaccess:
AddHandler application/x-httpd-php56s .php
ErrorDocument 404 /404/404.html
ErrorDocument 500 /404/404.html
RewriteEngine on
RewriteBase /
Options -Indexes
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /404/404.html [R=404,NC,L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /404/404.html [R=500,NC,L]
Thanks
Replace your current code with this:
DirectorySlash On
RewriteEngine On
# skip all known resources from rewrite rules below
RewriteRule \.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ - [L,NC]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+dir1/dir2/dir3/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^(?!dir1/dir2/dir3/)(.*)$ dir1/dir2/dir3/$1 [L,NC]

Url renaming using .htaccess file

Most are for redirections and removing file extensions or for dynamic urls.
The problem being I can't get a simple static url to rename
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^fileThathasalongname file.html
What I currently have 'mysite.co.uk/fileThathasalongname.html'
What I want is 'mysite.co.uk/file/' while file = file.html
using:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^FileIWantToChange.html FriendlyNamed.html
Using this gives me the error multiple Choices
+++++++++++++++++Edit+++++++++++++++++++++++++
Thought i'd add my final version for people to look at, it may help, the anwser below is correct.
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{HTTP_HOST} ^www.mysire.co.uk [NC]
RewriteRule ^(.*)$ http://mysite.co.uk/$1 [L,R=301]
all works a charm!
I see multiple issues going on. Firstly the regular expression is matched against the friendly URL the user types in. So you need to swap your friendly url and file path with each other. The friendly or "fake" name goes on the left while the url to redirect to silently goes on the right. Also make sure you've set the directory base to /. Finally it's good to add an [L] to enforce it to be the last rule in case anything in the same file tries to rewrite the path. Due note that other htaccess files lower down, depending on the files location, will also be checked even when enforcing the rule has the last rule. Also junk the options part completely. Give this a try:
RewriteBase /
RewriteEngine On
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]
RewriteRule ^fileThathasalongname.html file.html

Rewriting sub directories to Current Directory using .htaccess

AcceptPathInfo On
RewriteEngine On
RewriteBase /%{DOCUMENT_ROOT}/
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*) /index.php?req=$1 [L,PT]
This is what I have currently. If it isn't obvious, i want to find the current location of the .htaccess file currently being run and change any url that its suppose to hit after the current url to hit /CurrentDirectory/index.php?req=restofurl
Unfortunately, this breaks
However,
AcceptPathInfo On
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*) /index.php?req=$1 [L,PT]
Doesn't break and works exactly as desired, except hits root directory
Seems to me like you just want to skip the RewriteBase altogether. Since you want the redirect to be relative to current directory, I'd have thought this would work...
RewriteEngine On
RerwiteCond %{REQUEST_URI} !index\.php$
RerwiteRule ^(.*) index.php?req=$1 [L]