RedirectMatch file not found - apache

So, I have in my web root an admin.php file and I want every time someone writes /admin to be redirected to that /admin.php.
I wrote in my htaccess
RedirectMatch 301 ^/admin$ /admin.php
However, I get an error from apache "file not found".
I created an empty /admin folder and it works...
Then I try to have an other (also non existant folder) to redirect to admin.php. So I write
RedirectMatch 301 ^/blah$ /admin.php
and it works...
Finally I delete the admin.php and I have the thing redirected to another .php
RedirectMatch 301 ^/admin$ /index.php
and it works again...
I understand that for some reason apache messes things up when the folder to be redirected from has the same name with the .php
With Rewrite I can do the redirection without any problems but I was wondering if it was also possible with just a Redirect... Maybe I am missing something here...

apache messes things up when the folder to be redirected from has the same name with the .php
This is due to Apache's content negotiation module that runs before mod_rewrite, mod_alias and makes Apache web server match extensions of files. e.g. /file can be in URL but it will serve /file.php.
To fix this behavior disable MultiViews by placing this rule on top of your .htaccess:
Options -MultiViews

Related

RewriteEngine is not working [.htaccess]

ReWrite Engine is not working, i'm using 000Webhost as my Web Hosting Service Provider and i'm on free plan.
Problems:
1.) When I go to my domain (without subdomains) like prospekt.ml, i'm gonna be redirected to 000Webhost 404 Error.
2.) When I tried to test my .htaccess if it's working. It's supposed to rewrite the url and add a vanity url. like (user/astroXoom) but instead of that, i'm getting redirected to 000Webhost 404 Error.
The .htaccess file is placed on my public_html folder. It should redirect to "member/user/username.php?username=$1"
Screenshot of my
public_html
Thank you and have a good day!
Edit:
This is my .htaccess file
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
# Fancy Indexing
Options Indexes
IndexOptions FancyIndexing NameWidth=80 ScanHTMLTitles
IndexOrderDefault Ascending Date
IndexIgnore *.jpg *.png
# Rewrite for Users
RewriteEngine on
RewriteRule ^member/user/([^/]+)$ member/user/username.php?username=$1 [L,QSA]
On 000webhost server you have to use an absolute rewrite path (starting with /) or RewriteBase directive to map the rewritten url to correct location as 000webhost server uses virtual user home directory, so it's important to add a RewriteBase directive when you are using a relative path in Rewrite target, otherwise you will get a 404 not found.
Source :
https://www.000webhost.com/faq.php?ID=16

Redirect/Alias of URL in Apache

I have an Apache2.4 server on windows and I have a url like this:
http://example.com/fps/7A/quiz and I wish to redirect it to
http://example.com/fps/7A/quizlet.
I have tried Alias, ScriptAlias and redirect inside the Apache's htaccess file. The alias sometimes caused the whole website and all the pages to break.
Would I have to just make a folder named /quiz/ and put a file that redirects using PHP, or would .htaccess work fine?
Something like this?
RedirectMatch ^/fps/7A/quiz$ /fps/7A/quizlet
Or more general
RedirectMatch ^/fps/([^/]+)/quiz$ /fps/$1/quizlet
Or even more general
RedirectMatch ^/(.*)/quiz$ /$1/quizlet
You can add a 301 after RedirectMatch to make it a permanent redirect:
RedirectMatch 301 ^/(.*)/quiz$ /$1/quizlet

Redirecting all sub-sub pages to another subpage using htaccess

I've a older site running in Apache Server which is already indexed in Google. I wish to redirect all those indexed links to my new site (As the older pages are not existing any more.)
So i wish to redirect all my sub-sub pages to my new root page
I've pages like follows
http://itdost.com/answer-now/Aerobics
http://itdost.com/answer-now/HTML
http://itdost.com/answer-now/Culture
I use the following redirect code for each one
Redirect 301 /answer-now/Engineering http://www.itdost.com/questions/
Redirect 301 /answer-now/Food http://www.itdost.com/questions/
Redirect 301 /answer-now/ASP http://www.itdost.com/questions/
But as the site structure is big, i wish to do it in a single line instead of writing a line for each redirect
Some thing like the following.
Redirect 301 /answer-now/% http://www.itdost.com/questions/
But the above code does not seems to work
In order to use regex better to use mod_rewrite which is more powerful than mod_alias.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^answer-now(/.*|)$ http://www.itdost.com/questions/? [L,NC,R=301]
Try this:
RedirectMatch 301 ^/answer-now/ http://www.itdost.com/questions/

Apache and rewrite/redirect in htaccess to different folder

I know it's not original question but I've read several posts and questions here and still can't figure it out.
I'm on localhost (apache server) and default server home is in /var/www. There is a folder 'project' with several subfolders, one of them is called web with index.php file. So now if I want to go to index, I need to use full url localhost/project/web/.
I want to make it better so that if you go to localhost/project you are either redirected to localhost/project/web/ or the url remains same but it works (sorry for this naive formulation).
In .htaccess I changed directory index but then all links (since they are relative) were not working.
I tried this .htaccess in /var/www/project:
RedirectMatch permanent ^/$ ./web/
<Files .htaccess>
order allow,deny
deny from all
</Files>
Options -Indexes
But it's not working, going to localhost/project tries to list the project directory (it is not allowed due to Options -Indexes, which is what I wanted). I need it to be independent of the 'localhost' in url because it have to work anywhere (if .htaccess is allowed)
Thanks for any advice
RedirectMatch doesn't have a base like mod_rewrite does. Even if it's in the htaccess file in the /project directory, you need the full URI:
RedirectMatch permanent ^/project/$ /project/web/
If you wanted it to go to /project/web/index.php without a browser redirect (meaning the address in the URL bar on the browser stays at localhost/project/) you need to internally rewrite the URI using mod_rewrite:
RewriteEngine On
RewriteRule ^$ web/index.php [L]
Note that mod_rewrite uses the base URI that the htaccess file is in (but the "prefix" or leading slash is removed).

How do I redirect a subdirectory and all files in it to the root using htaccess?

I'm trying to redirect all requests for a subdirectory and any files in it to the root directory, using htaccess. Currently I have redirect 301 /directory/ / But this redirects to the root with the file name requested appended to it.. ie www.domain.com/directory/this.html redirects to www.domain.com/this.html I don't want it to request the file at all. Is there a way to do this?
Thank you
Karl
You will probably have to use mod_rewrite. Assuming you place the .htaccess in the root directory, something like this should work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^name-of-subfolder/.*$ / [R=302]
</IfModule>
NB. I put 302, as it's always sensible to use for testing, 301 can make it a pain checking changes with your browser. Once it's working feel free to change to 302.