I created a custom php MVC on windows and it worked great without any bugs but on linux I am facing some bugs like I am unable to access any other controller than my default one.
e.g: localhost/mymvc - This url redirects me to my default controller
but when I try to open any other controller e.g: localhost/mymvc/projects I get a "404 not found error"
Here are my functions that redirects:
/* ***** Getting URL ***** */
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = explode('/',$url);
/* ***** When URL does not contain any controller name call default controller ***** */
if(empty($url[0])){
$defaultpage = HOME;
require 'application/controllers/'.$defaultpage.'.php';
$controller = new $defaultpage();
$controller->loadModel($defaultpage);
$controller->index();
return false;
}
/* ***** When URL contains controller name ***** */
$page = 'application/controllers/'.$url['0'].'.php';
if(file_exists($page)){
require $page;
}else{
$this->error();
}
$controller = new $url[0];
$controller->loadModel($url[0]);
I am sure there are no bugs in here but still wanted you guys to review. I think have issues with .htaccess file so here is what I have in it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Undestanding your htaccess
Rewrite engine will be enabled:
RewriteEngine On
Base directory for rewrite will be /:
RewriteBase /
If request match a not existing file, continue:
RewriteCond %{REQUEST_FILENAME} !-f
If request match a not existing directory, continue:
RewriteCond %{REQUEST_FILENAME} !-d
If request match a not existing symbolic link, continue:
RewriteCond %{REQUEST_FILENAME} !-l
Rewrite to index.php:
// L means if the rule matches, don't process any more RewriteRules below this one.
// QSA Appends any query string from the original request URL to any query string created in the rewrite target
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
As we can see there is no actual problem with your htaccess.
Now you need to check if you have mod_rewrite enable in your apache.
You can just output phpinfo() and check if it's enabled.
Now into your PHP.
First be sure to remember that linux is case sensitive.
Debug:
After this line
$page = 'application/controllers/'.$url['0'].'.php';
Add this var_dump
var_dump($page);
Check if the path is correct and then do the rest of your debug analysis!
Regards
Related
I am creating an API type plugin, which fetches activity stream from easysocial.
But I got SEF URL in the activity stream, I want the NON-SEF URL in case SEF is on in Joomla configuration.
Please tell me how can I do this, programmatically.
I tried to override the Joomla config setting in my plugins, but it won't work as given below.
$config = JFactory::getConfig();
$sef = $config->set('sef', 0);
You would need to make a special .htaccess rule for that
Find this section:
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
And add this line above it:
RewriteCond %{REQUEST_URI} !^/index\.php?option=com_something&var=something
Where you change the URL to be the nonSEF url you do not want parsed.
I have not verified this. Let me know if it does not work.
I'm trying to figure out how to modify the .htaccess file so I can do two things:
Not have to include the .php extension on my PHP files (e.g., a request to my.domain.com/page maps to my.domain.com/page.php).
Do #1 while also including additional path info (e.g., a request to my.domain.com/page/path/stuff/here maps to my.domain.com/page.php/path/stuff/here).
I've found out how to do #1 by adding the following to the .htaccess file:
# Allow PHP files without ".php" extension.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /$1.php [L,QSA]
However, now I'd like to modify the RewriteRule so it works for #2.
OK, after searching for MultiViews, I found several articles warning against them (eh, to each his own), but that also led me to an answer that uses 2 rules instead of just 1:
RewriteRule ^([^\.]+)$ /$1.php [L]
RewriteRule ^([^\./]+)/(.*) /$1.php/$2 [L]
The first rule catches case #1 above, and the second rule catches case #2 above. Voila!
You could just try to use Multiviews, which is made to do exactly this:
Options +Multiviews
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^([^\.]+)$ $1.php [NC,L] #Remove the .php
Not sure what you want with the pathing stuff though.
Edit based off your comment, I've used something like this with php/angular. It's probably not "correct" or the best way to do it, but it worked for me.
Htaccess
RewriteEngine on
# Allow the API to function as a Front Controller
RewriteRule ^api/(.*)$ api/index.php?rt=$1 [L,QSA,NC]
# Allow Angular to have Pretty URL's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
api/index.php
// Pull the routing path
$router = explode('/', $_GET['rt']);
$version = $router[0];
$controller = $router[1];
$action = $router[2];
// Check for the file
if(file_exists($version . '/controllers/' . $controller .'.class.php')) {
include $version . '/controllers/' . $controller .'.class.php';
} else {
return false;
}
// Initialize and execute
$method = new $controller($action);
print $method->$action();
This lets me do something like: api/v1/users/login in the url, then will find the users.class.php file in the V1 folder, and run the function login.
I have an issue with correctly modifying my .htaccess file.
The goal is to implement a web service on my server to handle Apple's passkit requests.
This works perfect.. almost. Because my intention was, that when a user is typing: www.domain.com he should be directed to www.domain.com/index.php. This would be the usual index.php I would like him to see, when accessing my website.
And when Apples Server is trying to send requests to my web service, they should be redirected to the subfolder /server/index.php. This works fine, when calling www.domain.com
But when calling any different URL, that should be redirected, I get an error:
"Too many redirects occurring.."
I expected the redirection to /server/index.php
How do I have to modify my config file correctly?
I configured the file as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
# If file with specified name does not exist, procede to rewrite rule below
RewriteCond %{REQUEST_FILENAME} !-f
# if request = root
RewriteRule ^/?$ /index.php
# else
RewriteRule !^/?(index\.php)$ www.medifaktor.de/server/index.php [R=301]
# Check for HTTP Header Authorization and if so, import it as a server environment variable into PHP
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] [L]
</IfModule>
Your second rule is wrong since it is checking for not equal to /index.php and redirecting to /server/index.php.
Try this code:
RewriteEngine On
# If file with specified name does not exist, procede to rewrite rule below
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# if request = root
RewriteRule ^/?$ /index.php
# else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^/?(server/index\.php)$ /server/index.php [NC,L]
# Check for HTTP Header Authorization and if so, import it as a server environment variable into PHP
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] [L]
Thank you anubhava! I just tried what you suggested and it works fine. But now something interesting is happening. To test if the URL with the components that don't physically exist will be redirected correcty, I tried the following:
<?php
require_once("lib.php");
// get the request parameters out of the request URL
$requestURL = (($_SERVER['REDIRECT_URL'] != "") ? $_SERVER['REDIRECT_URL'] : $_SERVER['REQUEST_URI']);
$scriptPath = dirname($_SERVER['PHP_SELF']);
$requestURL = str_replace($scriptPath,"",$requestURL);
$requestParts = explode("/", $requestURL);
echo $requestURL;
This should output the components of the URL that is requested by my web service and should split it into its components. But whatever URL I try: I always get the result: /index.php
but there should be the components requested with the URL I think be shown?
I think, I just got the answer by accident:
By switch REDIRECT_URL to REQUEST_URI in the code, I got the correct output.
I want to redirect all to one script e.g. index.php?url=inputurl
With if/else I want to parse url
in index.php run query for url in my custom table
if url is mach: echo "ok"
else do nothing
How should I set .htaccess in root folder of Wordpress?
Example:
URLs in custom_table:
asd
dfg
ghj
If user puts:
www.mysite.com/asd
-> mod_rewrite should output this: www.mysite.com/index.php?url=asd
Else if user puts:
www.mysite.com/zzz
-> do nothing
I think the following .htaccess should solve the problem:
RewriteEngine On
# Redirects everything that is not index.php to index.php
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php?url=$1 [L,R]
Edit: to not include your folders and files (like /js, /css, etc.) in rewrite, add the following lines before the RewriteRule line (see comments):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
And in the PHP script:
$url = $_GET['url'];
// the method is_valid should check if the page exists in DB
if (is_valid($url)) {
// do something here
// maybe redirect with header('Location: path')
} else {
// show a not found page (error 404)
}
You want to both be able to read from a database and do nothing if there is not match.
This would require you run code to access db then return back to apache to process and is not possible from .htacccess (though it is from httpd.conf).
The .htaccess solution would be to specify all the "table" entries inline as below.
RewriteEngine on
RewriteBase /
#if asd or dfg or ghj
RewriteCond %{REQUEST_URI} ^/(asd|dfg|ghj) [NC]
RewriteRule . index.php?url=%{REQUEST_URI} [L]
I've currently got a web application that I need optimizing, and one of methods or something I'm trying to achieve is such:
http://myweb/dept/app
from
http://myweb/?dept=dept&app=app
I've currently this as the PHP code for it:
if(empty($_REQUEST['dept'])) {
$folder = "apps/";
} else {
$folder = str_replace("/", "", $_REQUEST['dept']) . "/"; }
if(empty($_REQUEST['n'])) {
include('user/home.php');
} else {
$app = $_REQUEST['n'];
if(file_exists($folder.$app.".php")) {
include($folder.$app.".php");
} else {
include("global/error/404.php");
}
}
How do I do this?
I'm currently half there with:
RewriteRule ^([A-Za-z]+)$ /index.php?app=$1
but that only rewrites part of it.
Thanks
The way many frameworks do this is with one of the following rules:
RewriteRule ^(.*)$ /index.php?q=$1
RewriteRule ^(.*)$ /index.php
In the 1st case you get the query string in $_GET["q"].
In the 2nd case you have to get the query string from $_REQUEST or something. (just do some var_dumps till you find what you need).
Then you explode("/") this and you're all set.
Have a look at how TYPO3, eZPublish, Drupal do this.
You should also add the following conditions to allow the site to open your static files (like images/css/js/etc). They tell apache to not do the rewrite if the URL points to a location that actually matches a file, directoy or symlink. (You must do this before the RewriteRule directive)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
This should work:
RewriteRule ^([A-Za-z]+)/([A-Za-z]+)$ index.php?dept=$1&app=$2 [QSA]
You need the QSA part in order for any GET parameters to be appended to the rewritten URL.
You might find that it can be more flexible to rewrite everything to index.php, and then handle splitting up the url there, e.g.
.htaccess:
#only rewrite paths that don't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
PHP:
<?php
$parts = explode('/', $_SERVER['PATH_INFO']);
$dept = isset($parts[0]) ? $parts[0] : 'someDefault';
$app = isset($parts[1]) ? $parts[1] : 'anotherDefault';