Slim Framework .htaccess HTTPS - php

I am using Slim for an API which is working really well under http but when I request the URL in https I get a 404 error.
I think it has something to do with my .htaccess file, but I am not sure. Can anyone point out what am I doing wrong here.
.htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

Depending on your server configuration the server might serve another directory when accessed via httpd ( eg httpsdocs instead of httpdocs).
You'll either have to change the configuration, copy the content or try to create a symlink from the http directory to the https directory.

Related

Mod_rewrite rule for redirecting all requests within a subdirectory to the index of that subdirectory with path as variable

I have requests coming to my server already. I want to v2 out my API calls.
Currently I have the directory /v2/ and within that I have index.php
If I call /v2/index.php?path=users/123 I get the right data back from my database. All is well.
But to standardize my code, I want to redirect /v2/users/123 to /v2/index.php?path=users/123
I have mod_rewrite loaded in Apache, tested with phpinfo(). I am not sure whether I need to place my .htaccess within the v2 directory or at root. I do not have any .htaccess in root today, and I do not want to modify any calls not made to /v2/.
Any pointers where to place the .htaccess file and the right conditions to do so?
My current file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /v2/index.php?path=$1 [NC,L,QSA]
I have put it in the /v2/.htaccess directory, but navigating ahead of /v2 yields 404 errors.
Thanks!

Apache vHost and API "pretty url" getting 404

I have a REST API that previously had urls on my localhost that looked like:
localhost/api/v1/collection/resource
As I'm working on an API and web app locally, I implemented apache virtual hosts to get an api url of:
api.mydomain.com/v1/collection/resource
I have a front controller that I'm routing calls. api.mydomain.com hits the front controller successfully. api.mydomain.com/v1 lists the directory (all the php files), which is a problem, and api.mydomain.com/collection/resource results in a 404.
My directory structure is:
/api
/v1
php files
.htaccess
Router.php
index.php
.htaccess contains:
RewriteEngine On
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I'm not quite sure where the issue lies. Anything other than the api.mydomain.com seems to bypass hitting index.php. I'm not too familiar with these rewrites, so any insight is appreciated.
Try this
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^api.mydomain.com$ [NC]
RewriteRule ^(.*)$ index.php [R=301,NC,L]
Found the issue. When I created the vHosts I copied an incorrect setting of AllowOverride None, which was rendering my htaccess files ineffective. Changing to AllowOverride All under the associated vHost directory resolved this.

The requested URL /how-do-i-find-the-difference-between-two-dates-using-jquery was not found on this server

I have sub-domain likedemos.testing.com and i would like to get my url like
demos.testing.com/how-do-i-find-the-difference-between-two-dates-using-jquery
I have index.php file in demos.testing.com root folder and i reading the first parameter as
$request_uri = substr($_SERVER["REQUEST_URI"], 1);
When i request site like : demos.testing.com/how-do-i-find-the-difference-between-two-dates-using-jquery it is giving following error :
The requested URL /how-do-i-find-the-difference-between-two-dates-using-jquery was not found on this server.
Please tell me how can rewrite url in .htaccess file.
.htaccess is an Apache specific configuration file, and more over depending on your main Apache configuration, it may or may not even be parsed. To get an answer, you need to properly tell us what Web server you are using (is it Apache? If not .htaccess won't work and you need to write your rewrite rules in some other Web-server specific way). If Apache, do you have AllowOverride set?
Assuming all of this is true (you use Apache and it is set to read .htaccess), try this rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
This basically rewrites everything which is not a real existing file or directory to index.php.
As I said, depending on your setup, this may not work. If it doesn't, please tell us more about your setup (which server, what PHP SAPI, etc.).

Redirecting all request to the root directory somewhere else?

Alright, so I've downloaded a CMS for a server I'm setting up and I have some difficulty with the path the files are on.
I have put the CMS in a subdirectory on my server as the root directory is already being used by another CMS. However, the CMS repeatedly uses "/" in the code to link to their files. For example, an image is found using this code:
<img src="/images/banner.png" />
As you know this will not work, because the link above redirects the request to the images folder in the root of the server, not the subdirectory. The CMS also came with a ".htaccess" file, so I immediately thought that I could redirect all requests made using the "/" character to the subdirectory on the server. This is the original ".htaccess" file:
RewriteEngine On
RewriteRule ^index.php(|/)$ content/.php
RewriteRule ^error.php(|/)$ content/error.php
RewriteRule ^housekeeping(|/)$ housekeeping/index.php
RewriteRule ^(|/)$ content/$1.php
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ content/$1.php
RewriteRule ^rd/([^/]+)(|/)$ /rd.php?id=$1
RewriteRule ^quickregister/start(|/)$ /register/start.php
RewriteRule ^quickregister/step2(|/)$ /register/step2.php
RewriteRule ^quickregister/step3(|/)$ /register/complete.php
RewriteRule ^community/online(|/)$ content/online.php
RewriteRule ^community/vip(|/)$ content/vip.php
RewriteRule ^credits/goldvip(|/)$ content/goldvip.php
RewriteRule ^home/(..*)$ content/home.php?u=$1
RewriteRule ^articles/(..*)$ content/articles.php?story=$1
ErrorDocument 403 /content/error.php
ErrorDocument 404 /content/error.php
I thought that by adding
RewriteRule ^/$ /subdirectory/
the requests to "/" would be redirected, but to no avail. I have checked the apache configuration and it seems that overwriting the config using htaccess files is enabled, so if anyone is able to help me out I'd be very happy.
Thanks in advance!
EDIT:
I came real close to a solution. I inserted this code just below "RewriteEngine on":
RewriteRule ^(.*)/(.*)$ /private_servers/strebbohotel/$2
This returned the following error page when visiting the url "http://mysite.com/private_servers/strebbohotel/content/.php":
Not Found
The requested URL /private_servers/strebbohotel/.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
As you can see, it skips the "content" subdirectory for some reason. I believe it has something to do with the other lines of the .htaccess file but I can't figure out which ones. It also could be that I need a more complex regex, but I'm not particularly good with it so if someone could help me further I'd be thankful.
You have to change the .htaccess file on yout root directory - and that will mess with the other CMS. The only solution that comes to my mind is to use combination of RewriteCond's:
RewriteCond %{HTTP_REFERER} my_better_cms_url_regex
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^.*$ /subdirectory/%{REQUEST_URI}
That should do the trick. If you want to customize it, refer to this cheatsheet (or simmilar). But be aware, that .htaccess in root directory is checked against ALL requests made to your subdir cms - therefore the need for second condition.
The .htaccess file in your CMS subdirectory would have no effect on requests made against the server root directory. You would need to add an .htaccess there.
Even then though, I don't know how you would differentiate requests from the CMS (like the image request in your example) from those intended for the application in the web root.
Does your CMS not have any configuration setting that allow you to specify a path the the files other than the web root?
You want to use:
RewriteBase /another_root_dir/
in the beginning of your .htaccess file

.htaccess - only one referral allowed; fastcgi

I am creating .htaccess file first time and it doesn't work for me.
My htaccess looks as following:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https://(www\.)?mysite.net/.*$ [NC]
RewriteRule .* - [F]
.htaccess file is in directory that corresponds to subdomain support.somesite.com
I want to allow access only for https://www.mysite.net domain (https://www.mysite.net/qa/test.aspx and other has links to support.somesite.com)
However, above .htacess doesn't work. I ran phpinfo and see that Server API is cgi/fastcgi and not Apache. I do not know if this is an issue.
Is this something with mod_rewrite module? Can I use above htaccess with fastcgi Server API?
HTTP_REFERER does not work with https.

Categories