How to redirect all files/folders except index.php, to index.php? - apache

I've set up a dummy website to test MediaWiki at https://wiki.rehman.website/
I want to redirect all direct external traffic to files and folders located after wiki.rehman.website/ to wiki.rehman.website/index.php. So for example:
These should redirect to wiki.rehman.website/index.php:
wiki.rehman.website/extensions/
wiki.rehman.website/docs/contenthandler.txt
wiki.rehman.website/NonExistantFileOrFolder
But obviously these should not be redirected (to prevent circular redirects):
wiki.rehman.website/
wiki.rehman.website/index.php
How do I do that please?
What I'm trying to achieve here is to prevent anonymous users (i.e. non-MediaWiki logged in users) accessing any part of the web directory or files.
This is my first time setting up a website, and my first time installing a private instance of MediaWiki. If you spot any other loophole or issue, it would be most helpful if you could let me know.
Many thanks in advance!

You can setup a .htaccess file in your root folder to accomplish that.
RewriteCond %{REQUEST_URI} !index\.php$ [NC]
RewriteRule ^(.*)$ http://www.example.com/index.php [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]
See this other question for more information:
How to Redirect All urls to a new domain in apache, except one

Related

htaccess rewrite root url to subfolder with accessing other folder?

I have following folder structure in apache server.
Public_html
-->admin
--->admin_login.php
-->website
--->index.php
since the index.php inside the website folder,
i have given following code in .htaccess
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule ^(.*)$ /website/$1 [L,NC]
so that the when the user enter root url , it will appear "www.myurl.com" instead of "www.myurl.com/website/"
but the issue is, i could not be able to access admin_login.php.
is there anyway to modify .htaccess, to come website/index.php in main url and able to access admin_login.php(both)?
Thanks in Advance
You need to add an exception to existing rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(website|admin)/ [NC]
RewriteRule .* website/$0 [L]
Negative condition %{REQUEST_URI} !^/(website|admin)/ will match every URI except URIs that start with /website/ or /admin/. This will allow you to directly open www.myurl.com/admin/admin_login.php.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/?$ website/index.php [QSA,NC,L]

Can't get rewrite to redirect all to front controller

I'm trying to redirect all site traffic to my front controller, but I've been having trouble with it getting my site to redirect when the file already exists. For instance: www.example.com/IDontExist/ doesn't exist and sends me to the controller, but www.example.com/maintence/ exist and therefore skips my controller entirely. Not the intention.
I've tried a couple of things in .htaccess files, and I need to use them because I have to upload it to a Godaddy Shared Linux Server, and don't have much access to higher class configs.
This is my current .htaccess file:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php [NC,L,QSA]
</IfModule>
I need to send people going to www.example.com/maintenance/ and all other urls to the front controller to get the page processed before sending it off to the client. Right now it's finding the file and loading that up before going to the front controller.
Edit: Forgot removing RewriteCond commands for some reason result in a 500 error.
Remove your file and directories excludes, after that
RewriteEngine On
RewriteRule ^([^?]*)$ /index.php [NC,L,QSA]
It was a redirect loop. Removing the conditions and then adding the following one would fixed it.
RewriteCond %{REQUEST_URI} !/index.php

Which way do htaccess rules cascade?

I'm trying to archive an old website behind a /v4 directory on the new website. I was hoping that the below .htaccess was going to redirect any page from the example website to its corresponding page archived under the new domain. However, when I just tested the site it appears that example.org.au/contact.asp just went to newdomain.com/v4/ instead of `newdomain.com/v4/contact.html
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.org\.au$
RewriteRule ^(.*)\.asp$ https://newdomain.com/v4/$1.html [R=302,NC,L]
RewriteRule (.*) https://newdomain.com/v4/ [R=302,NC,L]
The second RewriteRule is there for when people just go to the root domain but is it overriding everything?
Do it like this, and also your first rule was set to ignore example.org.au so would never fire. This will process any host served by the site. If that's not what you want let me know:
RewriteEngine On
RewriteRule ^(.*)\.asp$ https://newdomain.com/v4/$1.html [R=302,NC,L]
RewriteRule ^$ https://newdomain.com/v4/ [R=302,L]

Moving domain to subdirectory with CPanel

I recently moved several of my domains to GoDaddy, and am currently struggling to get the directory structure properly set up with each of them. I have each of my addon domains in its own directory under public_html/. Additionally, I want to move my primary domain into its own subfolder for cleanliness. As a result, my directory setup looks like this:
public_html/
primarydomain/
addondomainA/
addondomainB/
addondomainC/
I setup my .htaccess file under public_html as follows:
RewriteEngine on
# Rewrite direct addondomain access to their proper domains
RedirectMatch ^/addondomainA.com/(.*)$ http://addondomainA.com/$1
RedirectMatch ^/addondomainB.com/(.*)$ http://addondomainB.com/$1
RedirectMatch ^/addondomainC.com/(.*)$ http://addondomainC.com/$1
# Rewrite primary domain access to the primarydomain/ folder
RewriteCond %{HTTP_HOST} ^(www.)?primarydomain.com/
RewriteCond %{REQUEST_URI} !^/primarydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /primarydomain/$1
RewriteCond %{HTTP_HOST} ^(www.)?primarydomain.com$
RewriteRule ^(/)?$ primarydomain/index.html [L]
The addon domain redirects work perfectly, and if I try to access primarydomain.com, the request is properly rewritten to the primarydomain/ subdirectory. However, if I try to access primarydomain.com/primarydomain, nothing gets rewritten (as far as I can tell) and the user can directly access the page. I would like to disallow all direct access requests for the primarydomain/ folder, but nothing I try seems to work. I thought that removing:
RewriteCond %{REQUEST_URI} !^/primarydomain/
Would do something, but it seems to have no effect on the behavior. Similarly, both:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
...seem to have no effect if deleted. I even tried adding a fourth RedirectMatch clause up top, hoping to catch the folder name if it's inserted, but it leads to a redirect loop, as if it's processing the commands out of order. I've devoured all the answers I could find on stack overflow, but none of them solve the issue. I've tried multiple fresh browsers, and am pretty certain that this is not a cache problem.
Does anybody have any ideas how to accomplish this seemingly super-basic task? Is there something I've overlooked? Thanks in advance.
you could try using a cname back to the proper directory (cname in advanced dns in cPanel)

How to restrict other websites from accessing by htaccess

Recently I have encounter a strange issue , my website www.xyz.com is being pointed by some one on the web domain let suppose www.abc.com.
Though the whole website is on www.xyz.com but the other domain display every single content and directory path structure by their domain...e.g. the real path is www.xyz.com/somepage/id/etc can be work by www.abc.com/somepage/id/etc with same directory paths....
This other website is just redirecting everything to my website and I want to stop this domain to use my directory structure. This www.abc.com is also being crawled by Google crawler and added its link in Google search engine.
This is a very new issue to me I have one solution to restrict every single request and check if its from my own website or not.
Second solution is to restrict them through htaccess but I don't find perfect solution using htaccess.
I saw on the web it stop all the referrer, but doing that I am afraid if it will stop users coming from other website to my website ...I just need to restrict other domains to use my whole website as theirs using redirection...i have taken this issue on go daddy and they said they also don't know why the other website is pointing to my ip address ... so clueless I need expert advice to secure my website from future issues like this ...kindly advice...
My htaccess is
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
and i am using YII...
You can place this rule just below RewriteEngine On line:
RewriteEngine On
RewriteCond %{REMOTE_HOST} abc\.com$ [NC,OR]
RewriteCond %{HTTP_REFERER} abc\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !xyz\.(com|net)$ [NC]
RewriteRule ^ - [F]
In your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.xyz\.com$
RewriteCond %{HTTP_HOST} !^subdomain\.xyz\.com$
RewriteRule .* - [F]