rewrite rules not working - apache

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]

Related

htaccess for laravel without affecting subdomains

I'm trying to get htaccess working for laravel 5.4, but without it affecting the sub-domains that are created.
My current htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
What I've tried is setting an htaccess in the sub-domain folders with the follwing:
RewriteEngine On
As I read elsewhere this should stop the top domain htaccess, yet when for example I have a sub-domain like dev.example.com, it will redirect to dev.example.com/dev
Anyway of getting rid of the /dev at the end?
Folder structure:
app
bootstrap
config
database
public
storage
resources
routes
dev --> subdomain
Upfront, I don't see how this /dev is related to the directives, you have shown. None of them do anything to add a /dev anywhere. Maybe it's just the subdirectory applied somehow.
The claim "this should stop the top domain htaccess" is not entirely true. From Apache - How directives are applied
The configuration directives found in a .htaccess file are applied to the directory in which the .htaccess file is found, and to all subdirectories thereof.
However, it is important to also remember that there may have been .htaccess files in directories higher up. Directives are applied in the order that they are found.
Therefore, a .htaccess file in a particular directory may override directives found in .htaccess files found higher up in the directory tree. And those, in turn, may have overridden directives found yet higher up, or in the main server configuration file itself.
So an .htaccess doesn't stop another .htaccess, but a directive overrides a directive. This means, you may have some directive from one .htaccess and another unrelated directive from the top level .htaccess.
In your case, RewriteEngine on just overrides RewriteEngine on from the main .htaccess file.
If you want to prevent any RewriteRule from the top .htaccess, I would rather try
RewriteEngine off

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

hhvm fastcgi and apache .htaccess rules

Ok so I have this rewrite rule:
RewriteRule ^images/$ index.php?id=images [L]
So hhvm instead of using this rule it searches first for the images folder and looks for a index.php file
I'm using this proxy directive in the virtual host file:
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/hhvm/mydomain/htdocs/$1
What can be done so this rule can work correctly even if the folder images exists ?
Thanks,

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 can I implement a global RewriteCond / RewriteRule in Apache that applies to all virtual hosts?

The title pretty much says it all. :-) I have lots of virtual hosts and I want to put a single rewriting block at the top of the httpd.conf file that rewrites URLs no matter which virtual host the request might be directed to. How the heck do I do this?
I found this but my question is the same: how can I do this without resorting to .htaccess files and performing some other action for each virtual host?
OMGTIA!
Specify RewriteOptions InheritDown in the parent scope (such as httpd.conf) to get your rules applied in child Virtual Hosts without modifing them.
This will only work on Virtual Hosts where the RewriteEngine directive is set to on:
Note that rewrite configurations are not inherited by virtual hosts. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use rewrite rules.
(source)
Apache supports this since 2.4.8 (not available at the time of the original question).
From documentation for RewriteOptions:
InheritDown
If this option is enabled, all child configurations will inherit the configuration of the current configuration. It is equivalent to specifying RewriteOptions Inherit in all child configurations. See the Inherit option for more details on how the parent-child relationships are handled.
Available in Apache HTTP Server 2.4.8 and later.
InheritDownBefore
Like InheritDown above, but the rules from the current scope are applied before rules specified in any child's scope.
Available in Apache HTTP Server 2.4.8 and later.
IgnoreInherit
This option forces the current and child configurations to ignore all rules that would be inherited from a parent specifying InheritDown or InheritDownBefore.
Available in Apache HTTP Server 2.4.8 and later.
(http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions)
By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each <VirtualHost> section:
RewriteEngine On
RewriteOptions Inherit
click http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html to find more information
Looks like the simplest possible solution is to add
RewriteOptions inherit
to each VirtualHost directive. This is at least a lot simpler than messing with .htaccess files. Apache is pretty clear on the fact that
by default, rewrite configurations are
not inherited. This means that you
need to have a RewriteEngine on
directive for each virtual host in
which you wish to use it.
(http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html)
and apparently the way to change the default is via RewriteOptions in the child (vhost or director), so you have to do something in each child.
I've never tested it, so it might not work, but I would try adding an include directive in all of the virtual host blocks to a single file. You would have to change each virtual host configuration block once, but after that, you should have a central place from which to make changes. YMMV.
If you're only trying to rewrite something in the domain part of the name, e.g. to fix a common misspelling, you don't even need the 'inherit' option. I setup a no-name virtual host to catch all invalid host names and respell them correctly before redirecting them.
Since this uses redirects, the appropriate virtual host will be found after the rewrites have been applied.
Options +Indexes +FollowSymLinks
RewriteEngine on
# If it begins with only domain.com, prepend www and send to www.domain.com
RewriteCond %{HTTP_HOST} ^domain [NC]
RewriteRule ^(.*) http://www.domain.com$1 [L,R=301]
# Correct misspelling in the domain name, applies to any VirtualHost in the domain
# Requires a subdomain, i.e. (serviceXXX.)domain.com, or the prepended www. from above
RewriteCond %{HTTP_HOST} ^([^.]+\.)dommmmmain\.com\.?(:[0-9]*)?$ [NC]
RewriteRule ^(.*) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+\.)?domain.com(.*) http://$1domain.com$2 [L,R=301]
# No-name virtual host to catch all invalid hostnames and mod_rewrite and redirect them
<VirtualHost *>
RewriteEngine on
RewriteOptions inherit
</VirtualHost>
You may want to use InheritDownBefore to avoid having to add more junk to your vhosts.
An example of a global letsencrypt alias:
# letsencrypt
<IfModule alias_module>
Alias /.well-known/ /var/www/html/.well-known/
</IfModule>
<IfModule mod_rewrite.c>
# prevent vhost rewrites from killing the alias
RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} ^/\.well\-known
RewriteRule . - [L,PT]
</IfModule>
Then you can do this in each of your vhosts, with no other directives:
<VirtualHost *:80>
....
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/.* /index.php [L,PT]
</IfModule>
</VirtualHost>
Thanks to everyone to answered above. It helped me find my answer.
Question has been answered already, I just wanted to add an example in case you are using Google Compute Engine. It says it requires Apache HTTP Server 2.4.8 BUT it works with Apache/2.4.25 (Debian). Even when I try to upgrade, I cannot go past Apache/2.4.25. It says this version is the latest version.
Here's an example of how to implement.
RewriteOptions InheritDown
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} !\.co$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.[^.]+$ [NC]
RewriteRule ^ https://%1.co%{REQUEST_URI} [L,NE,R=301]
<VirtualHost *:80>
RewriteEngine On
ServerAlias *.*
</VirtualHost>
ALSO OF NOTE (For Testing):
When you are testing your rewrite engine. It is really easy to get confused about if it is working or not because of cache and cookies. If you got it to work once on a browser, it will continue to work even if you delete the rewrite code. Testing rewrite is really annoying sometimes. You might think it works but then it stops or starts.
Best way to test rewrite code is to open an incognito tab in your browser, clear or cookies and cache. Open developer mode just in case. DO NOT JUST REFRESH. You need to click into the URL and refresh. Or open new tab. Or copy/paste URL into new window. If you use same window with refresh, it might be just redoing results from the past instead of renewing the new code.
I've always used a "catch-all" VHost for directives I wanted across the board, like......
Listen 80
NameVirtualHost *:80
<VirtualHost *:80>
ErrorLog "/var/log/apache2/error_log"
</VirtualHost>
<VirtualHost *:80>
ServerName alloftherestoftheVHosts.com
DocumentRoot "/ServiceData/.........
............
And it's always seemed to work... error logs were getting combined properly, etc...... but it IS possible that this was the result of an earlier / conflicting / like-minded directive.
Personal note.. Whoever dreamed up the Apache configuration schema and syntax was a dingbat, or a group of dingbats, who spent too much time in their cave.... The whole thing should be exorcised and XMLized, or something! Although they are both wildly different... the Hello-Kitty setup process of Cherokee.. to the viciously succinct NGinx config.... are both so much more logical..