Htaccess rules get overriden - apache

I want my .htaccess in the web server's root dir to redirect any request starting with ^assets/(.*)$ to the folder public/$1. All other requests have to go to index.php?request=$1. I also want to keep get parameters (L flag does it) in this case.
RewriteEngine on
RewriteBase /
RewriteRule ^assets/(.*)$ public/$1 [QSA,L]
RewriteRule ^(?!assets)(.*)$ index.php?request=$1 [L,QSA]
The only problem is that the last line overrides the previous :/
I also tested with (.*) but without success.
Thanks for your help.

Try this:
RewriteEngine on
RewriteRule "^/assets/(.*)$" "/public/$1" [L,QSA]
RewriteCond %{REQUEST_URI} "!^/assets/"
RewriteCond %{REQUEST_URI} "!^/index.php"
RewriteCond %{REQUEST_URI} "!^/public"
RewriteRule (.*) "/index.php?request=$1" [L,QSA]
QSA: appends the parameters to the rewritten part
L: last. Which means if it matches, do not process any other rewrite.
Rewrites start at /
To apply a condition on a RewriteRule, use RewriteCond
References:
https://httpd.apache.org/docs/2.4/rewrite/flags.html
https://httpd.apache.org/docs/current/mod/mod_rewrite.html
The first rule says:
if it starts with "/assets/", anything following the second / will be rewritten as "/public/$1".
The RewriteCond and RewriteRule says:
if it does not start by "/assets/", take whatever you received, add it at the end of "/index.php?request=".
So http://www.example.com/directory/somepage.html will go to http://www.example.com/index.php?request=/directory/somepage.html.
If there are parameters in the request, they are appended at the end like so:
http://www.example.com/directory/somepage.html?a=1&b=4 will go to
http://www.example.com/index.php?request=/directory/somepage.html?a=1&b=4
EDIT Nov 19 1115
Added a second RewriteCond to make sure "/index.php" does not rewrite to itself. And a third, in case you want "/public" to also not redirect to "/index.php".

Related

htaccess pretty urls not working

Folder structure:
- assets
- all css / js
- calsses
- all models, db ant etc
- views
- admin
- app
- index.php
- customers.php
.......
my .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?localhost:8080$
RewriteRule ^(.*)$ /views/$1
RewriteRule ^(/)?$ /views/index.php [L]
address : localhost:8080/app/ - working fine, but then I try to add pretty url for example in my customers.php - localhost:8080/app/customers.php?id=5 change to localhost:8080/app/customers/id/5
htaccess added new line:
RewriteRule /id/(.*) customers.php?id=$1
It's not working, it always return 500 Internal Server Error there could be the problem?
plus Need all urls without .php extend
You'd have to include those conditions for every rule. You'd be better off just rewriting everything to, say views/router.php then using PHP to include the different controllers, or serve a 404 when the URL isn't valid.
RewriteRule !^views/router\.php$ views/router.php [NS,L,DPI]
I agree with Walf in that handling routes through a router class is a better idea (especially in the long run!) than using .htaccess redirects.
However, as your question seems to be more about why is this not working than about how you should do it, here is an explanation for what is going on.
I will be using these URLs as examples:
localhost:8080
localhost:8080/app
localhost:8080/app/customers/id/5
Your first rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?localhost:8080$
RewriteRule ^(.*)$ /views/$1
As you intended, this RewriteRule will match any URL which is not a file, not a directory, and made to localhost:8080.
localhost:8080 # not matched because it leads to a directory.
localhost:8080/app -> localhost:8080/views/app
localhost:8080/app/customers/id/5 -> localhost:8080/views/app/customers/id/5
Your next rule:
RewriteRule ^(/)?$ /views/index.php [L]
It is important to realize that RewriteCond statements apply only to the first RewriteRule following them, thus all that is being checked here is the path.
Side note: ^(/)?$, as you are not using $1, can be simplified to ^/?$.
localhost:8080 -> localhost:8080/views/index.php
localhost:8080/views/app # not matched
localhost:8080/views/app/customers/id/5 # not matched
As the L flag is specified, Apache will immediately stop the current iteration and start matching again from the top. The documentation is badly worded. Thus, localhost:8080/views/index.php will be run through the first rule, fail to match, be run through this rule, fail to match, and then as no other rules exist to check (yet) no rewrite will be done.
Now lets look at what happens when you add your broken rule.
RewriteRule /id/(.*) customers.php?id=$1
There are a few problems here. First, as you don't require that the URL start with /id/ the rule will always match a URL that contains /id/, even if you have already rewritten the URL. If you amended this by using ^/id/(.*), then you would still have issues as the string that the rewrite RegEx is tested against has leading slashes removed. Lastly and most importantly, customers.php does not exist in your root directory.
localhost:8080/views/index.php # not matched
localhost:8080/views/app # not matched
localhost:8080/views/app/customers/id/5 -> localhost:8080/customers.php?id=5
This is the last rule in your file currently, so now Apache will start over. customers.php does not exist in your directory, so it will be rewritten to views/customers.php. No other rules matched, but the URL has changed and so Apache will start over again, as /views/customers.php does not exist, it will be rewritten to /views/views/customers.php ... This pattern will repeat until you hit the maximum iteration limit and Apache responds with a 500 error.
You can solve this several ways. Here would be my preferred method, but only if you cannot use a router.
RewriteEngine on
# Rewrite the main page, even though it is a directory
RewriteRule ^/?$ views/index.php [END]
# Don't rewrite any existing files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .? - [S=999,END]
RewriteRule ^app/?$ views/app/index.php [END]
RewriteRule ^app/id/(.*)$ views/app/customers.php?id=$1 [END]
TL;DR Use a PHP based router. .htaccess rules can be incredibly confusing.
Please refer to the question, How to make Clean URLs
I think this is what you needed.
you can use RewriteRule ^(.*)$ index.php [QSA,L]
Having another crack.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{HTTP_HOST} !^(?:www\.)?localhost:8080$ [OR]
RewriteCond $0 =views
RewriteRule [^/]* - [END]
RewriteRule ^(app|admin)/([^/]+) views/$1/$2.php [DPI,END]
RewriteRule ^(app|admin)/?$ views/$1/index.php [DPI,END]
You may have to use L instead of END flags if your Apache is older. Set up an ErrorDocument for 404s, too.
Don't muck around with query strings, just parse $_SERVER['REQUEST_URI'] in PHP, e.g. start by exploding it on /. Then you'll have all the parameters of the original pretty URL. You can do that part in an include so each controller can reuse the same code.
I tried your structure and .htaccess file myself and found an endless loop in the apache logs. I bet you got something like this:
Mon Nov 28 19:57:32.527765 2016] [core:error] [pid 10] [client 172.18.0.1:35048] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
I could fix it by adding the last rule like:
RewriteRule id/(.*) /views/app/customers.php?id=$1
The leading / is not needed for the match and the target needs the full path. Note that I got the id double (e.g. 123/123) on the url: http://localhost:8080/id/123.
This is caused by one of the 2 previous rules (removing them fixes it) so you might need to change them.
Here is what you want :
RewriteEngine On
RewriteBase /app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^\/?$ views/index.php [L]
RewriteRule ^([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)\/?$ views/$1.php?$2=$3 [L]
RewriteRule ^([a-zA-Z0-9]+)\/?$ views/$1.php [L]

RewriteRule Apache - Redirect only for directory, not for files within it

I believe this question may be close but I'm very new to mod_rewrite so forgive me if this is elementary.
I need to redirect requests to a particular folder and it's default page ONLY, allowing all other requests to continue as specified. For example:
Only these urls should be rewritten:
http://example.com/abc
http://example.com/abc/
http://example.com/abc/index.html
...let's say to http://example.org
...but, the following requests should be permitted:
http://example.com/abc/anotherpage.html
http://example.com/abc/subdir
http://example.com/abc/subdir/
http://example.com/abc/subdir/index.html
I tried the following...
RewriteRule ^/abc/ http://example.org/
RewriteRule ^/abc/index.html http://example.org/
... but the first rewrite behaves recursively and redirects anything below abc.
Can a redirect be achieved only for a folder and not its contents (except for files specified)?
Try adding a $ delimiter at the end of your regex to indicate that it is the end of the match:
RewriteRule ^/abc/$ http://example.org/
RewriteRule ^/abc/index.html$ http://example.org/
You could alternatively combine the two into just:
RewriteRule ^/?abc/(?:index\.html|)$ http://example.org/ [L,R]
or using mod_alias instead of mod_rewrite:
RedirectMatch ^/abc/(?:index\.html|)$ http://example.org/
With the $, something like /abc/123 won't match because the regex says that the / must be at the end.
Try this:
RewriteCond %{REQUEST_URI} ^/abc/index.html [OR]
RewriteCond %{REQUEST_URI} ^/abc$ [OR]
RewriteCond %{REQUEST_URI} ^/abc/$
RewriteRule ^(.*)$ http://your.domain/ [L,R]

Mod_rewrite in two directions?

An existing page is called /foo/bar.php. What I have done is a rewrite so that when a user types /foobar, it load the contents of /foo/bar.php (while keeping /foobar in the url bar)
But I also want the opposite - when a user clicks on a link or types /foo/bar.php, I want to have /foobar in the url. The reason is to avoid manually changing all the links.
How could I do that (if possible without an http redirect, but via some rewrite magic)? And is it possible for those two rules to co-exist?
Edit - After the first response, I realized my description of the problem was not proper. /foobar is not supposed to be a concatenation of foo, bar of /foo/bar.php, but an arbitrary string (/whatever).
Edit 2:
I now added RewriteRule ^whetever/?$ /foo/bar.php [L] in the / .htaccess. Then I added RewriteRule bar\.php$ /whetever [R=302,L] in the /foo .htaccess. The problem is it 's a circular reference and fails.
Thanks,
John
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/foo/[^/]+\.php$
RewriteCond %{IS_SUBREQ} !true
RewriteRule ^/foo/([^/]+)\.php$ /foo$1 [R,L]
RewriteCond %{REQUEST_URI} ^/foo[^/]
RewriteRule ^/foo(.*) /foo/$1.php [L]
The first part matches /foo/something.php and transforms them into /foosomething, but only if it is not a sub-request.
The second part takes any /foosometing and transforms it into /foo/something.php, via sub-request
You can try matching against %{THE_REQUEST} and only do the redirect when the actual request is for the php file:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo/bar\.php
RewriteRule bar\.php$ /whatever [R=302,L]
RewriteRule ^whatever/?$ /foo/bar.php [L]

mod_rewrite condition for existing folder

I'm trying to set-up an .htaccess file that will pass every request URL as GET into a file called index.php. The only exception is, when the request URL points to a directory res.
Examples:
/wiki/cool-stuff => index.php?uri=/wiki/cool-stuff
/wiki/cool-stuff?news=on => index.php?uri=/wiki/cool-stuff&news=on
/res/cool-photo.jpg => res/cool-photo.jpg
Two problems:
The GET variable passed to /wiki/cool-stuff in the second example is not passed to index.php
Accessing /res (not /res/!!) suddenly shows me /res/?uri=res in my browser and index.php with uri=res/. Accessing /res/ instead, shows me index.php with uri=res/ and the URL in the browser stays (which is okay).
The .htaccess:
RewriteEngine on
RewriteBase /subthing/
RewriteCond %{REQUEST_URI} !/res/(.+)$
RewriteCond %{REQUEST_URI} !index.php
RewriteRule (.*) index.php?uri=$1
How can I achieve the desired behaviour?
Try using the Query-String-Append flag, QSA
Make the trailing slash optional - in Regex, this is achieved by adding ?.
New .htaccess:
RewriteEngine on
RewriteBase /subthing/
RewriteCond %{REQUEST_URI} !/res(/.*)?$
RewriteCond %{REQUEST_URI} !index.php
RewriteRule (.*) index.php?uri=$1 [QSA]
Note that I have tweaked the Regex on the /res folder to cause /resabc to be redirected (if the slash was the only optional piece, anything beginning with res would match.
Apache Mod_Rewrite Documentation

direct all webpage requests with .index.html to /

I want to direct all requests for any URL that ends with index.html to /. I have one domain on the server.
Example:
If someone wants "www.thissite.com/index.html--it is directed to www.thissite.com/.
AND
if someone wants "www.thissite.com/anyword/index.html"--it is directed to www.thissite.com/.
AND
if someone wants "www.thissite.com/folderdoesntexistonthissite/index.html"--it is directed to www.thissite.com/.
What is the .htaccess code that would enable this? (Both the rewritecondition and rewriterule)
This doesn't quite do the job:
RewriteCond %{THE_REQUEST} index\.html [NC]
RewriteRule index\.html$ http://www.thissite.com/$1 [R=301.L]
You could try this (without RewriteCond):
RewriteRule /index\.html$ http://www.thissite.com/ [R=301,NC,L]
Maybe the Error was the Period in [R=301.L].
You will need to use %{REQUEST_URI} variable to match in RewriteCond otherwise Apache strips out starting / in RewriteRule. Use below code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^.*/index.html$ [NC]
RewriteRule . / [R=301,L]