multiple ht access rules in one file - php

My site should attempt to serve all requests from a cache subdirectory (/app/storage/cache) such that a request for /two.html is served from /app/storage/cache/two.html
More examples:
/ served from /app/storage/cache/index.html
/two.html served from /app/storage/cache/two.html
/folder served from /app/storage/cache/folder/index.html
If no files/directories are found it should handle all requests with a php file /app/router.php
What I've tried:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/storage/www/$1 [L]
RewriteRule ^(/)?$ /app/storage/www/$1 [L]
that works fine for catching all urls and serving the correct resource without changing the url. Now to catch all else:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . app/router.php [L]
That also works, but then the first set of conditions no longer works...

There are three ways I can think of how to go about this. One is check the cache via router.php before generating the page through that same file. The other two ways involve some rewrite-magic.
I have not tested either of these.
Fall-through approach
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/storage/www/$1
#From this point on, the file/dir either exists or
#%{REQUEST_FILENAME} is now /app/storage/www/something
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/storage/www/ /app/router.php [L]
Explicit test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/app/storage/www/$1 !-f
RewriteCond %{DOCUMENT_ROOT}/app/storage/www/$1 !-d
RewriteRule ^(.*)$ /app/router.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/storage/www/$1 [L]
Both approaches can likely be sped up by using Apache's main configuration file (httpd.conf) instead of the .htaccess file, as it does not need to search for/read the .htaccess file on every request.

Related

URL ShortCut Redirects

This is my .htaccess command for making URL shortcuts:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .(.+)$
RewriteRule ^(.) ?open=encyclopedia&letter=$1&term=%1 [R,NC,L]
With the above works just fine:
example.com/Njumba ➛ example.com/?open=encyclopedia&letter=nj&term=njumba
Now, the problem is that it redirects merely one letter, but not two.
This is how I want it to be:
example.com/NJ ➛ example.com/?open=encyclopedia&letter=nj
Is this also achievable with .htaccess?
You can use this rule instead:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.{1,2}).*$ /?open=encyclopedia&letter=$1&term=$0 [R,NE,QSA,L]

Same IP, multiple Laravel installs in sub directories. How to rewrite using .htaccess?

I am trying to figure out (learn) how to set up .htaccess so multiple Laravel installs could be run on the same IP by by using the URL (/site_1, /site_2) like:
http://xx.xx.xx.xx/site_1
So far what I have realized that subsequent request for resources (like: /css/app.css) don't pass the rewrite rules and are lost. I have tried the OR flag to tie the request somehow to the url but then rules gets mixed up.
How could I get this to work?
.htaccess
RewriteEngine ON
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} /site_1 [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site_1/public_html/public/$1 [L]
RewriteCond %{REQUEST_URI} /aloe [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site_2/public_html/public/$1 [L]
Add this block and try again. Hope this will work. It might need some modification for the path you want to access for css and js.
RewriteBase /site_1/public_html/public/
RewriteCond %{REQUEST_FILENAME} \.(css|js)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /site_1/public_html/public/$1 [L]

modrewrite help using multiple dynamic gets

original = http://ritetag.techreanimate.com/signin.php?network=twitter&fhfghdfghh=sadfgsdf
rewrite = http://ritetag.techreanimate.com/signin/twitter?fhfghdfghh=sadfgsdf
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^signin/([^/]*)$ /signin.php?network=$1
i got this working but i cant get other variables using get that come after, how do i get toe rewrite to work for stuff other than network.
I solved it!
RewriteEngine On
RewriteBase /
#This rewrite has no L flag so the second one can continue
#Signin.php can have multiple networks like signin/twitter = signin.php?network=twitter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^signin/([^/]*)$ /signin.php?network=$1 [QSA]
#this rewrite just allows to use files withouth the .php
# Like about instead of about.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [QSA]

Apache ReWrite rule stopping portions of our app

Our application uses rewrite rules in our PHP application and our developer added these rewrite rule to our Apache VHosts file:
I have updated our Rewrite rules based on suggestion below.
Currently my VHosts file configuration looks like this:
RewriteCond %blog !-d
RewriteRule ^/([a-zA-Z0-9-,]+)/?$ /index.php?a=$1
RewriteCond %blog !-d
RewriteRule ^/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/?$ /index.php?a=$1&b=$2
RewriteCond %blog !-d
RewriteRule ^/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/?$ /index.php?a=$1&b=$2&c=$3
RewriteCond %blog !-d
RewriteRule ^/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/?$ /index.php?a=$1&b=$2&c=$3&d=$4&e=$5
I am able to currently access all of our other application tabs successfully such as /goods,etc. But I still cannot access www.test.com/blog
It is still redirecting me to our application's main index.php page. From the rewrite logs:
[13/Oct/2009:17:08:41 --0400] [www.test.com/sid#8d03d8][rid#b902d8/initial] (1) pass through /blog/wp-login.php
[13/Oct/2009:17:08:41 --0400] [www.test.com/sid#8d03d8][rid#b942f8/initial] (1) go-ahead with / /public_html/index.php [OK]
Thanks
You are trying a lot of stuff in your rewrite rules. I advice you not to.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /index.php?url=$0 [PT,L]
I advise you to rewrite the complete string to $_GET['url'] variable. You could explode this variable in you code. This way you have to edit your vhost only once.
The RewriteCond still counts in bothways (in the above and below example).
These rewrite conditions will check if a file or directory exists. If not it will apply the rewrite rule
if the conditions will not work try to place the condition above every RewriteRule
like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([a-zA-Z0-9-,]+)/?$ /index.php?a=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/?$ /index.php?a=$1&b=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/?$ /index.php?a=$1&b=$2&c=$3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/([a-zA-Z0-9-,]+)/?$ /index.php?a=$1&b=$2&c=$3&d=$4
Try using the conditions
RewriteCond %{REQUEST_URI} !^/blog$ [OR]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^/([a-zA-Z0-9-_,]+)/?$ /index.php?a=$1
It's a matter of playing with conditions and regexes. This should prevent triggering the rewriterule when the URI starts with "/blog/"

Mod Rewrite and PHP

I am trying to redirect pages using mod_rewrite to the pages with some variables (for using them with PHP's $_GET).
To give an example with few lines:
When the user enters /c/stg it redirects to item_show.php?id=$1&f=1 (where f is page number).
When the user enters /c/stg/2 it redirects to the second page with show.php?id=$1&f=$2.
I think there are no errors or misuses of these in my file but, here's what I want:
I want user to enter /string directly to go item_show.php?id=$1&f=1 with $1 is our string of course...
But when I change my file by removing the /c/ part from RewriteRule it starts giving errors in all other directories and doesn't read any files (my.css) even though I have already defined a RewriteCond %{REQUEST_FILENAME} !-f...
Do you have any suggestions?
Or how can I made this system possible with any method?
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#user
RewriteRule ^u/([^/]+)/?$ profile.php?username=$1 [L,NC]
#marked
RewriteRule ^marked/([^/]+)/?$ item_marked.php?id=$1 [L,NC]
#content
RewriteRule ^c/([^/]+)/?$ item_show.php?id=$1&f=1 [L,NC]
RewriteRule ^c/([^/]+)/([^/]+)/?$ item_show.php?id=$1&f=$2 [L,NC]
Am I wrong if RewriteCond only are passed on to the first RewriteRule??
Try this instead:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#user
RewriteRule ^u/([^/]+)/?$ profile.php?username=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#marked
RewriteRule ^marked/([^/]+)/?$ item_marked.php?id=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#content
RewriteRule ^c/([^/]+)/?$ item_show.php?id=$1&f=1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ item_show.php?id=$1&f=$2 [L,NC]
Cheers,
RewriteCond directives do only belong to the first following RewriteRule directive. So in your case the two RewriteCond direcitves are only applied to the first RewriteRule directive and not to the others.
But you could use this abortion rule to quit the rewrite process if the request can be mapped to an existing file or directory:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
This would cause that following rules are only tested if the request cannot be mapped directly to an existing file or directory.

Categories