Subfolders in a REST API - php

Let’s say I have a structure like this https://domain.com/api/rest/v1.0/, but in the folder v1.0 I have subfolders like data-model and constants.
Now let’s say I’m using mod rewrite, so all URLs that have the base URL https://domain.com/api/rest/v1.0/ get forwarded to https://domain.com/api/rest/v1.0/index.php.
Would this affect my PHP files contained in https://domain.com/api/rest/v1.0/data-model/ and https://domain.com/api/rest/v1.0/constants/?
For example, if in https://domain.com/api/rest/v1.0/index.php I try to add a file from https://domain.com/api/rest/v1.0/constants/ using require_once, would that cause some kind of redirect loop?
I’m not getting any output after the lines of code where I do the above, but am before. Similarly I’m not getting any errors/reloading of the page in the browser so am at a bit of a loss.
If this is the issue, does anyone have any pointers to a better REST file structure than just lumping all the files in the root folder?
Thanks
UPDATE
Here is my rewrite rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule api/rest/v1.0/(.*)$ api/rest/v1.0/index.php?request=$1 [QSA,NC,L]
</IfModule>

No, neither your php files nor your directories are affected. mod_rewrite only rewrites URLs. That means that urls are only rewritten if accessed via http request (e.g. with a browser).
Probably your rewrite rules are wrong. Also check php log files for errors.
What are your rules like?

It turns out the solution was that my subdirectories weren’t properly added in require_once as my paths were relative. So instead of require_once 'folder/file.php' I changed my files so they now read require_once dirname(__FILE__) . '/folder/file.php'

Related

htaccess friendly URL rewrite rule

I have recently change my hosting and i need a htaccess rewrite rule for my files. I tried many examples but no one really works for my case. I have never been really good in htaccess and on my older hosting i didn't really need anything it just worked but here is not. Basically i want that my PHP files are without extensions and treated like a directory. So for example i have a URLs like these:
www.domain.com/file1/{id}/{nick}
So for example:
www.domain.com/myfile1/104/username
www.domain.com/myotherfile/455/nick
File1 in this case is a PHP file and {id} and {nick} are changable. I have this structure on my entire site for many other PHP files etc. So if possible i want one universal rule for all files. I tried with htaccess to remove php extenstion etc but all I got is 404 error. Also URL in browser should stay friendly without PHP extension. So in my case if i rewrite my URL manually in:
www.domain.com/file1.php/{id}/{nick} it worked but i don't want to change all the links etc on my website. So all i want is to hide PHP extension and treat PHP files as directory.
Thanks
You can use this single and generic front controller rule in site root .htaccess:
AcceptPathInfo On
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/(.*)$ $1.php/$2 [L]

.htaccess mod_rewrite does not work with two parameters on localhost

I would like to have clean URLs in my projects. So I've written these codes in a .htaccess file.
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&id=$2 [NC,QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [NC,QSA,L]
But it does not work completely when I'm trying to work with it locally.
Imagine that I have a directory myproject in htdocs (www) in my local web server path and other files are stored in this folder. Now I can see the project if I go to localhost/project.
Now I want to work with URLs.
It works well if I have only one parameter in URI like localhost/myproject/tours. But if I have 2 parameters like localhost/myprojects/tours/inside, it seems that all css, js and images files go away. I've also added RewriteBase /myproject to .htaccess file, but nothing solved.
What is my mistake? I need a solution that works on both remote and local server.
First of all, see my response on your other question about your code: Why .htaccess mod_rewrite does not show a real directory
Now, RewriteBase won't solve your problem about css/js/images etc. It's only for htaccess and it defines the base path when a rule is rewritten.
One common way to avoid this problem is to add in all your files a base url right after <head> html tag.
For you, it would be: <base href="http://localhost/myproject/" />
Otherwise, if you reach localhost/myprojects/tours/inside then your css/js/images links will be resolved as localhost/myprojects/tours/inside/__here__ because the default base path here is the current directory (/myproject/tours/inside/) and this is not what you want
Edit: if that's the case, don't forget to remove leading slashes from your css/images/javascript html links
The browser will build absolute URL paths out of your relative URL paths by looking at your made up context of /myprojects/tours. You may need to strip one or two levels of that prefix off to find the real path.
The access log will show you plain as day what relative URL's come in when you use the old and new URLs.

Php basic website page link

I'm creating a simple php website with changeable themes. I have a directory called MyThemes with a a folder inside containing the following files header.php, sidebar.php, page.php, footer.php now the problem is that i want to display a page from the database using the page.php file of the selected theme, but the generated link will be something like
website/MyThemes/ThemeName/page.php?id=somePageID
I want to change that if possible to something like
website/pages/somePageID
I have a little experience with PHP, but apparently not enough to do this. So any help will be greatly appreciated.
This is typically done via URL rewriting by the webserver: the server makes sure /url/like/this is converted to (for instance) something.php?like=this. After that there is no difference to the application. Apache uses mod_rewrite to do this. If you were using Django, this would be configured in the urls.py files.
You could still simplify your URLs to website/pages.php?id=n by remembering the theme in a cookie or session variable though.
Also, your current file paths suggest that you duplicated the pages for the different themes though: this is never a good idea. Don't repeat yourself!
You can use friendly urls and regular expressions:
See
http://www.phpriot.com/articles/search-engine-urls
In this article, you'll find instructions for using htaccess:
Example:
<IfModule mod_rewrite.c>
RewriteEngine on
#rule not apply directories
RewriteCond %{REQUEST_FILENAME} !-d
#rule not apply files
RewriteCond %{REQUEST_FILENAME} !-f
#Rule to page
RewriteRule ^page/$ page.php
RewriteRule ^page$ page.php
RewriteRule ^page/(.*)/(.*)?$ page.php?id=$1&des=$2
RewriteRule ^page/(.*)?$ page.php?id=$1
</IfModule>

mod_rewrite trouble

So I'm using a simple rewrite rule like:
RewriteEngine On
RewriteRule ^foo/?$ foo.php [NC,L]
It redirects perfectly to foo.php, but it seems like all links and images in foo.php are being taken from folder foo on the server which doesn't exist. For instance, 1.png will now be foo/1.png or index.html will now be foo/index.html. So my question is: is there any way to make thing right without changing paths to the files in foo.php?
Your visitors' browsers see the current page as being at /foo/, thus all relative URLs will be resolved under /foo/. You will need to set the base URL, or update all your relative URLs to point to your site root (e.g. do not use relative/path/url.jpg but /relative/path/url.jpg).
In your code you should provide a rewrite rule for your resources (images, css, etc...) or add conditions for real files / folders like...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
The two RewriteCond lines test to see if the requested URL points to an actual real directory (the !-d part), and the second one tests if it's a real file (!-f)
In the future, you can easily debug your mod_rewrite stuff by adding this two lines to your .htaccess file:
RewriteLogLevel 3
RewriteLog "/path/to/a/file.log"
2 simple ways
absolutize the references as suggest by Ianzz
remove the foo path for referenced object still using htaccess
RewriteRule ^foo(/.*.(jpg|html|gif|css))$ $1 [L]
I prefer the 2nd solution because the htaccess do the mess and htaccess fix the situation, and no changes to your code are needed.

URL Rewriting and actual directory names matched by regex

I created an application using the Front Controller pattern, so basically everything is sent to index.php
This is what I currently have in my .htaccess file
RewriteRule ^([a-zA-Z0-9]+)(\/([a-zA-Z0-9]+))?(\/([a-zA-Z0-9\/]+))?(\/)?$
index.php?class=$1&method=$3&args=$5
(I had to break the line into two, for presentation but you know what I mean)
So it works fine however if I create a directory named /js for example that has to be directly accessible. That means, I can't put an .htaccess file inside it and put:
Deny from all
Then accessing my site using the URL http://mysite.com/js redirects to http://mysite.com/js/?class=js&method=foo&args=
Putting Options -Indexes in the main .htaccess file doesn't really help. Any thoughts?
Any clarifications are welcome, I'm not really an good at explaining things.
Thanks
Use
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Exclude real files and directories from the redirection rule (redirection rule will not apply to the real files and directories).

Categories