I am developing an app with PHP 7.0 and implementing routes with MVC. My root folder ('/') is the 'public' directory. When I access the address 'localhost' I am redirected to index.php with have the routes available. But when I try another url to access another route, like 'localhost/contact' the server doesn't find the entry and give this message:
Not Found
The requested URL /contact was not found on this server.
I am pretty sure that the problem in on my server config (apache2 on linux mint 18), because my friend's PC works normal. I'm using a .htaccess file too inside the public directory:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
It seems to me that the server does not run index.php and tries to access the path. How could I ever force the execution of index.php to see if there is a route to the url informed?
here follows my apache2 config files.
http://pastebin.com/2mgSjWWV
http://pastebin.com/yD4RpfK8
I'm still newbee and I understand very little in server configuration. Please someone can give me a light? Thanks!
This:
RewriteRule ^.*$ - [NC,L]
It matches EVERYTHING, and since it's tagged [L], no other RewriteRules will be evaluated, so all rewriting stops here. That means a request for example.com/foo will match this rule, rewriting stops, and a literal foo file will be searched for in the file system - which doesn't exist.
And then, even if this one rule wasn't there, this next line
RewriteRule ^.*$ index.php [NC,L]
would also not work. ANY url would match it, but then strip off the relevant data, so a request for example.com/foo would be identical to a request for example.com/index.php. no query parameters would be passed in.
Your logic should be more like:
RewriteRule ^(.*)$ index.php?args=$1 [QSA,L]
which would do these sorts of translations:
example.com/foo -> example.com/index.php?args=foo
example.com/bar?baz -> example.com/index.php?args=bar&baz
After an exhaustive battery of tests, I found a solution. The htaccess file and code was fine. The problem was my server not reading the .htaccess. Editing the apache2.conf and seting 'AllowOverride All' on , finally works.
This page https://docs.bolt.cm/3.0/howto/making-sure-htaccess-works help me to solve that!
Related
So, I'm not very good with Apache config or .htaccess rewrite rules.... And I'm trying to do some modifications to how my localhost server works...
What I'm trying to do is return a 404 error on any request with the extension '.php'. If the uri does not have an extension, then route the request to 'ini.php'. If the uri contains an extension that isn't of '.php', then it should just follow normal procedures in fetching the file.
What I have now:
Rewrite Engine on
DirectorySlash off
RewriteCond $1 (.php)
RewriteRule ^(.*)$ - [L,NC,R=404]
RewriteCond $1 !^(.+)
RewriteRule ^(.*)$ ini.php [L,NC]
My logic is that if it's not a .php, and it doesn't have an extension, then route it to ini.php. Otherwise it should route normally.
Right now it looks like the .php rule is working in returning 404 errors.. However, if a request for a path without an extension is received, it tries to route to ini.php and hits a 404 page. Is it maybe processing like the second rule and then hitting the first rule?
Anyways, can someone help me sort it out and give me some guidance on it? I tried google and a bunch of different solutions, but I couldn't find something that worked for this situation...
UPDATE:
I changed the code to the following and added ini.php to the DirectoryIndex settings in httpd:
RewriteEngine on
RewriteCond %{REQUEST_URI} (\.[php^\\/]+)$
RewriteRule ^(.*)$ - [L,NC,R=404]
RewriteCond %{REQUEST_URI} !(\.[^\\/]+)$
RewriteRule ^.+$ / [L,NC]
Can you check if it looks alright?
I've turned on DirectorySlash again. Thanks.
This will do it:
RewrieEngine on
# 404 any URL ending .php (ignoring any query string)
RewriteRule ^(.+)\.php$ - [R=404,L,NC]
# Rewrite any URL that does not contain a dot (.), and therefore has no extension, to ini.php
RewriteRule ^([^.]*)$ ini.php [END]
I am assuming it will go in a .htaccess file from what you said. It would need changing to go in the main config.
Don't turn DirectorySlash off. It's a security risk to do so (see the link) and it only applies to existing directories anyway so is not causing any problems for you. There is no space in RewriteEngine.
I have set up Usbserver as my local webserver on my PC so that the server root is http://localhost:8081/ but all my webprojects are in http://localhost:8081/WWW/PHP/ subdirectory of my local server.
Now: all is working fine - all relative paths work as they should but as I play with the .htaccess file and I want to set up my custom 404.php page with relative path like this:
ErrorDocument 404 /php/errors/404.php
...it just do not work as expected cos it, of course, points to http://localhost:8081/ instead of, let's say, http://localhost:8081/WWW/PHP/myproject/ that is my actually acting kind-of server root (cos once uploaded to remote server it would become http://myproject.com/).
This would be absolutely fine on remote server but not here on my local server structure. I also try one suggestion like this:
ErrorDocument 404 ./php/errors/404.php
...but it ended up with blank white page with sentence "./php/errors/404.php" displayed.
QUESTION: how to write the code in .htaccess "universally", so it would think that root server is in "myproject" directory instead of "http://localhost:8081/" but once uploaded on my remote server, let's say http://myproject.com/ it would still work as normally?
REMEMBER: I cannot use rewriting the root of my local server in Usbserver settings cos I have also quantum of other webpages in "http://localhost:8081/WWW/PHP" so they all would stop working that way!
I guess there is some kind of RegEx code that would solve this (as I saw many times here on Stackoverflow guys do this kind of stuff that way) but I am quite bad in RegEx myself so without help from others I am "lost".
I searched web already before asking this here: there were many solutions but I did not find any that would actually solve this specific task (most of them suggesting just rewriting server root in my Virtual Server - Usbserver - settings which I cannot do for reason explained above).
I simply need it to work OK on localhost and on remote server afterwards without rewriting my .htaccess code everytime I upload my website to remote server and vice versa.
P.S.: I also noticed that when I set up my custom 404.php page with absolutle path for my local server like:
ErrorDocument 404 http://localhost:8081/WWW/_PHP_/myproject/php/errors/404.php
...it works BUT it change URL to that custom 404 page instead of staying at the original "not existent" URL address - why? Cos with normal not-edited 404 page the URL doe snot change, it stays the same (= that non existent link).
EDIT - FINAL SOLUTION:
So after hours of playing, browsing web, searching for any bits of some additional useful information I finally got it working exactly as I wanted - different directory for error pages (404 etc.) specified in case of localhost and remote server, here it is (mind you: it is my own code so it may not look good - I am newbie in .htaccess stuff - but at least it really work for me as I expected it to work):
# RULES FOR ERROR PAGES DIRECTORY IN CASE OF localhost:8081
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^localhost:8081 [NC]
RewriteRule .? /WWW/_PHP_/myproject/php/errors/404.php [L]
# RULES FOR ERROR PAGES DIRECTORY IN CASE OF remote server (actual webpage on public server)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^localhost:8081 [NC]
RewriteRule .? /php/errors/404.php [L]
Relative path is not allowed in error document directive. so you can use an absolute path instead of the full url.
ErrorDocument 404 /www/_php_/myproject/php/errors/404.php
Reference :
https://httpd.apache.org/docs/2.4/custom-error.html
This is the working solution
# RULES FOR ERROR PAGES DIRECTORY IN CASE OF localhost:8081
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^localhost:8081 [NC]
RewriteRule .? /WWW/_PHP_/myproject/php/errors/404.php [L]
# RULES FOR ERROR PAGES DIRECTORY IN CASE OF remote server (actual webpage on public server)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^localhost:8081 [NC]
RewriteRule .? /php/errors/404.php [L]
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.).
I'm looking to handle the URL's except homepage with a common PHP file. This is just like a PHP $_GET request except the difference that there would be no parameter. It'll be just like a file.
Ex- http://localhost/ - This should be managed by index.php file as usual.
http://localhost/ANYTHINGHERE - This should be thrown to a custom PHP file which would then decide what to do.
Actually, I'm working on a project where I need to hide the URL information from the users. So, the file that would manage the ANYTHINGHERE URL would actually access a directory localhost/i/.
Thanks and waiting for best response!
To achieve this you need two parts:
First: .htaccess which redirects all accesses to your domain passed to a php script (index.php here):
RewriteEngine On
RewriteRule (.*) index.php/$1
Second: In index.php you get the user-entered URI as $_SERVER['PATH_INFO'] (starting with /)
This, however, makes all requests to go through the index.php script (depending on the location of index.php you could also get an endless recursion, so read on ;) ). Normally one doesn't want that (e.g., images should be served directly by the web server). Thus, one normally uses (i.e., existing directories, files and links are served by the web server directly):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^(.*)$ - [NC,L]
RewriteRule (.*) index.php/$1
If this should take place in a subdirectory you need to add RewriteBase /subdirectory directly after RewriteEngine On.
If you don't want to use $_SERVER['PATH_INFO']you can also use RewriteRule (.*) index.php?url=$1 [QSA], then you get the user entered URI as $_GET['url'].
This requires mod_rewrite to be loaded on the server.
See http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html.
I've searched and found a lot of questions on this site and elsewhere that are very similar, but I've tried implementing and modifying all the suggestions I've found and none of it works. I realize this is a very basic question an I am extremely frustrated because nothing I'm trying is working.
With that having been said... I am trying to organize my content pages within kurtiskronk.com/pages/... (e.g. kurtiskronk.com/pages/about.php)
What I want to do is make it so that I can simply link to kurtiskronk.com/about ... So how do I go about stripping "pages/" and ".php"? I don't have a ton of content pages, so it's not a big deal if I have to specify for each page, though something dynamic would be handy.
NOTES: I am using Rackspace Cloud hosting, and WordPress is installed in /blog. My phpinfo() can be seen at http://kurtiskronk.com/pages/phpinfo.php
This is my existing .htaccess file (in the root)
php_value register_globals "on"
Options +FollowSymLinks
RewriteEngine On
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
RewriteBase /
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [L]
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
# PHP - MAIL
php_value mail.force_extra_parameters -kurtis#kurtiskronk.com
I tested and the rewrite works with the line below (/about as URL brings up file /pages/about.php), but then the homepage gives a 500 Internal Server Error:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
So I'm still sort of in the same boat as before, and as a follow-up, possibly more difficult question, if you go to http://kurtiskronk.com/weddings I am using SlideShowPro (flash) w/ SSP Director (self-hosted) as the back-end for it. When it pulls up a new image, it adds the following after /weddings ... "#id=album-152&num=content-9698"
There are four sections of the portfolio
# Homepage (kurtiskronk.com) id=album-148 ($id is constant for this section)
# Weddings (/weddings) id=album-152 ($id is constant for this section)
# Portraits (/portraits) id=album-151 ($id is constant for this section)
# Commercial (/commercial) id=album-150 ($id is constant for this section)
Assuming we get kurtiskronk.com/weddings to rewrite successfully without breaking anything, how would we make the total URL something cleaner kurtiskronk.com/weddings/9698 since the $num is the only thing that will change within a given section?
Kurtis, thanks for the extra information. It's a lot easier to give a specific answer to this.
My first comment is that you need to separate out in your thinking URI space -- that is what URIs you want your users to type into their browser -- and filesystem space -- what physical files you want to map to. Some of your mappings are URI->URI and some are URI->FS
For example you want to issue a permanent redirect of www.kurtiskronk.com/* to kurtiskronk.com/*. Assuming that you only server the base and www subdomains from this tree, then this cond/rule pair should come first, so that you can assume that all other rules only refer to kurtiskronk.com.
Next, you need to review the RewiteBase documentation. .htaccess files are processed in what Apache calls a Per-Directory context and this directive tells the rewrite engine what to assume as the URI base which got to this directory and .htaccess file. From what I gather, your blog is installed in docroot/blog (in the filesystem, and that you want to get to directory by typing in http://kurtiskronk.com/blog/ but that this .htaccess file is for the root folder -- that is the base should be (this goes before the www mapping rule)
DirectorySlash On
DirectoryIndex index.php
RewriteBase /
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
You can add some field dumps look for REDIRECT_* in the Server or Environment table in the phpinfo O/P to see if these are sensible. For example:
RewriteWrite ^(.*)$ - \
[E=TESTDR:%{DOCUMENT_ROOT}/pages/$1.php,E=TESTPDR:%{DOCUMENT_ROOT}/pages/$1.php]
Your next rule is that if the file exists in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [NS,L]
[Note that some shared service sites don't set up DOCUMENT_ROOT properly for the rewrite engine so you may need to run a variableinfo script (<?php phpinfo(INFO_ENVIRONMENT | INFO_VARIABLES); to see if it sets up alternatives. On your site you have to use %{ENV:PHP_DOCUMENT_ROOT} instead.]
Your next rule is that if the file exists, but with the extension .php in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [NS,L]
Now redirect any blog references to the blog subdirectory unless the URI maps to a real file (e.g. the blog stylesheets and your uploads.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
A complication here is that WP may be using a poorly documented Apache feature call Path Info that is a script can act as a pseudo directory so http://kurtiskronk.com/blog/tag/downtown/ is redirected to docroot/blog/index.php/tag/downtown/ which is then executed by `docroot/blog/index.php using /tag/downtown/ as the PATH_INFO. But this is one for Wordpress experts to comment on. If this last rule doesn't work then try:
RewriteRule ^blog/(.*) blog/index.php/$1 [L]
PS. I like your site. I wish I was that young again :(
Postscript
When you say "it doesn't work", what doesn't with this .htaccess?
http://kurtiskronk.com/phpinfo,
http://kurtiskronk.com/phpinfo.php,
http://kurtiskronk.comblog/tag/downtown/
It's just that these rules work for these tests (with domain swapped) on mine. (One way is to move or copy the above variableinfo.php to the various subdirectories. If necessary temporarily rename the index.php to index.php.keep, say, and copy the variableinfo.php to the index.php file. You can now enter the various URI test patterns and see what is happening. Look for the REDIRECT_* fields in the phpinfo output, and the SCRIPT_NAME will tell you which is being executed. You can add more {E=...] flags to examine the various pattern results. (Remember that these only get assigned if the rule is a match.
Lastly note the changes above especially the additional NS flags. For some reason mod_rewrite was going directly into a subquery which was resulting in redirect: being dumped into the file pattern. I've had a look at the Apache code and this is a internal botch to flag that further redirection needs to take place (which then replaces this or backs out). However this open bug indicates that this backout can be missed in sub-queries and maybe that's what is happening here. Certainly adding the NS flas cured the problem on my test environment.
PS. Note the added explicit DirectoryIndex directive and also that whilst http://kurtiskronk.com will run the root index.php, the explicit /index.php version will run the one in pages, because that's what your rules say.
Here is a simple solution. You can use it apache conf file(s) or in .htaccess (easier to set up when you're trying).
mod_rewrite has to be enabled.
For example, use .htaccess in your DocumentRoot with:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
It will redirect /about to /pages/about.php, and any other page.
The "RewriteCond" part is to authorize access to an existing file (eg: if you had an "about" file at the root of your site, then it will be served, instead of redirecting to /pages/about.php).
Options +FollowSymLinks
RewriteEngine On
RewriteRule /([0-9]+)$ /pages/$1.php [L]
Put something like this in your .htaccess file. I guess that is what you want.
Juest a redirect from a simple url to a longer url.