htaccess redirect API to sub folder - apache

My document root (/var/www/html/) looks like this:
app/
client/
index.html
server/
api/
user/
get.php
classes/
models/
When the user navigates to any URL on my website that does NOT start with api/, they should load the index.html file inside of client.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond ^ app/client/index.html [QSA,L]
That code works correctly.
However, when the URL starts with api/, I want to direct the URL to the api folder under server/.
For instance, www.website.com/user would load the index.html file.
Whereas www.website.com/api/user/get.php would load www.website.com/server/api/user/get.php.
I know that I would need a special condition for api/ before my current condition.
RewriteRule ^api app/server/api [L]
That rule works when using www.website.com/api, but it does not work when there are slashes after api/. www.website.com/api -> www.website.com/app/server/api, www.website.com/api/test -> www.website.com/var/www/html/test.
Any ideas?

Have it this way:
RewriteEngine On
RewriteRule ^api(/.*)?$ app/server/api$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond ^ app/client/index.html [L]
Make sure to test it after clearing your browser cache.

Related

How to redirect any URL to my index.html using apache?

I want my website to point at my index.html no matter what the URL is.
http://localhost/WebsiteName/
http://localhost/WebsiteName/whatever
http://localhost/WebsiteName/whatever/whatever-1/whatever-2/whatever-3/etc
By using this rewrite code in my apache config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]
My URL works correctly when URL is any of these:
http://localhost/WebsiteName/
http://localhost/WebsiteName/whatever
But breaks when it is like this or further extended:
http://localhost/WebsiteName/whatever/
http://localhost/WebsiteName/whatever/whatever-1/whatever-2/whatever-3/etc
It acts as if there was another folder "whatever" in the directory whenever I use one of the URLs that break.
I don't want the URL to change, I just want it to point at my index.html no matter what it is.
Based off what you have there, all of those url's should go to your index.html. The index.html is assumed to be at the DOCUMENT_ROOT.
I'm not sure what your problem is, but maybe instead of having a condition for rewriting non-files. Stop rewriting when it's a file. I notice any changes on my website, with either config, but maybe it can work for yours.
RewriteEngine on
### If a uri leads to an actual file or directory (presumably with it's own index.html), then don't rewrite any further
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
### everything else
RewriteRule . /index.html [L]

How can I make my site load example.html when somebody visits example.com/example

I have a file in my public_html folder called example.html.
When somebody visits example.com/example.html, they get a 404 response.
How can I make Apache serve example.html instead?
I have a bunch of files like that, so I'd prefer to create some kind of general rule instead of redirecting them all individually.
#Turning RewriteEngine On
RewriteEngine on
#Making sure a directory or file does not exist under that name
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule {WHAT THE USER SEES} {WHAT IS CALLED}
RewriteRule ^example$ example.html
With this, loading example.com/example would show whatever example.html loads.
If you have a bunch of HTML files that you want to load in that format, use this:
RewriteRule ^(.*)$ $1.html
You can put whatever regex you want in the first argument.

Apache RewriteRule pattern

I writing an .htaccess file for the web resources management.
I would like to this pattern of URI to use for static resource
^.*/web-console/static/(.*)$
And, my RewriteRule is below.
RewriteRule "^.*/web-console/static/(.*)$" "resources/floworks-web-console-1.0/static/$1" [L]
And, I request below URI
http://myhost/shop/web-console/static/css/default.css
But the Apache server hasn't match URI.
And if I delete "/web-console" Apache works fine.
Below is the RewriteRule
RewriteRule "^.*/static/(.*)$" "resources/floworks-web-console-1.0/static/$1" [L]
What is a problem?
Appendix
this is my directory structure
web base - /data/web
site base - /data/web/shop
htaccess - /data/web/shop/.htaccess
router script - /data/web/shop/route.php
static resources - /data/web/shop/resources/floworks-web-console-1.0/static
css file - /data/web/shop/resources/floworks-web-console-1.0/static/css/default.css
So, I wish response static resources if URI is started /shop/web-console/static
And, other case is pass to router script using "RewriteRule .* route.php [L]"
You should be able to do it like this. If you have this .htaccess in a sub folder shop, you should add rewritebase.
RewriteEngine On
RewriteBase /shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule "^web-console/static/(.*)$" "resources/floworks-web-console-1.0/static/$1" [L]
I found myself.
RewriteRule "web-console/static/(.*)$" "resources/floworks-web-console-1.0/static/$1" [END]
I'd change that flag from L to END
Thanks.

mod_rewrite directory heirarchy, rules specific to rewritten URLs?

I have directories and subdirectories similar to below:
Each file and folder here is preceded by localhost/dir/
/common/style.css
/common/js.js
/common/top.php
/common/bottom.php
/images/items/1.png
/images/items/2.png
/images/items/3.png
/index.php
/items.php
/specificitem.php
On every page in the default directory, everything in the common folder is included (each page includes top.php and bottom.php, top.php includes js.js and style.css). specificitem.php relies on $_GET to grab data to populate that page, and is directed to from items.php.
In my .htaccess file, I rewrite the URLs for specificitem.php like the below:
RewriteEngine on
RewriteBase /dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule items/john specificitem.php?id=1 [NC,L]
RewriteRule items/jane specificitem.php?id=2 [NC,L]
RewriteRule items/jill specificitem.php?id=3 [NC,L]
...
When directing to these pages, no links work, styling and Javascript are broken/not loaded. Links are in the form ./somelink.php, and when on one of the item pages (eg. localhost/dir/items/john), ./somelink.php tries to direct to localhost/dir/items/somelink.php when this page doesn't exist. Stylesheets and Javascript are also attempted to be found at localhost/dir/items/common/ when likewise this doesn't exist. Changing the links to /somelink.php instead tries to go to /localhost/somelink.php when this doesn't exist.
What have I done wrong in my .htaccess file, or what is missing such that I can apply link rules to rewritten links only? My links, extra files, images are loaded properly for any page without a rewritten URL.
First have your rules like this:
RewriteEngine on
RewriteBase /dir/
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^items/john/?$ specificitem.php?id=1 [NC,L,QSA]
RewriteRule ^items/jane/?$ specificitem.php?id=2 [NC,L,QSA]
RewriteRule ^items/jill/?$ specificitem.php?id=3 [NC,L,QSA]
Then to fix links/css/js/images paths you need to add this just below <head> tag of your page's HTML:
<base href="/dir/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

Removing specific path segments from url with .htaccess

My current url structure looks like this:
Served from /var/www/app:
/example.com/app/news
/example.com/app/files
/example.com/app/offers
Served from /var/www:
/example.com/admin
Slightly strange structure as I've bolted the 'app' onto a current CMS simply by creating a new app sub-directory in the root folder. What I want to do is to serve the app files from eg:
/example.com/news
/example.com/files
/example.com/offers
i.e. just removing app from the url. But I obviously still want the /admin/ routes to be served from the original root directory. How can I go about this?
You can try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/admin$
RewriteRule (.*) /app/$1 [L]
You will find the documentation here: http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html