RewriteEngine is not working [.htaccess] - apache

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

Related

RedirectMatch file not found

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

rewrite rules not working

I use some rewrite rules inside the Apache virtual hosts configuration httpd-vhosts.conf and it works fine there. Because I want to move to a managed server I have to use the .htaccess file because I don't have access to the apache configuration files.
I tried it locally and somehow I don't get this working at all.
Locally I use xampp 1.8.3 (Apache 2.4.10). I put the .htaccess file in the root directory of the virtual host and use this lines for testing
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule /testrule /about/about.php [L]
I always get an 404 error when calling localhost/testrule
It works fine when I put it in the httpd-vhosts.conf.
mod_rewrite is enabled and I have set AllowOverride All in the Directory part.
This blog post clearly mention
In VirtualHost context, The Pattern will initially be matched against
the part of the URL after the hostname and port, and before the query
string (e.g. “/app1/index.html”).
In Directory and htaccess context, the Pattern will initially be
matched against the filesystem path, after removing the prefix that
led the server to the current RewriteRule (e.g. “app1/index.html” or
“index.html” depending on where the directives are defined).
So in virtual host context, matches start from /testrule/...
While in .htaccess and <Directory/> context, it matches testrule/...
You can quick check the htaccess rewrite rules here:
http://htaccess.madewithlove.be/ or here:
http://martinmelin.se/rewrite-rule-tester/
Just get rid of the leading slash in the RewriteRule and you are done
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule testrule /about/about.php [L]

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 can I rewrite this URL with Apache?

I want to be able to access sitenamehere.com/folder/index?a=something by visiting sitenamehere.com/folder/something in my address bar.
How can I do this?
I've looked into mod rewrite but I don't understand it.
mod_rewrite is an Apache (web server) extension not related to PHP. You'll want to create a file called .htaccess and include the following line:
RewriteEngine On
RewriteRule ^folder/(.*) /folder/index.php?a=$1
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Your .htaccess or httpd.conf.
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Your rule
RewriteRule ^([^/]+)/([^/]+)/?$ $1/index?a=$2 [L]
This assumes you want where folder is to be mapped over to where folder is in your example. If you want to match the literal folder, just replace the first capturing group with it (and add it to the replacement).

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.