I had this setup:
images
image1.jpg
image2.jpg
header.html
about
index.php
image3.jpg
But going to MyWebsite.com/About gave it an extra slash on the end. I decided to go with the solution of creating a file called about in my home directory:
images
image1.jpg
image2.jpg
header.html
about
aboutfiles
image3.jpg
The problem is that now this file won't let me use .php:
<?php include('header.html');?>
It's not showing the header file. What can I do to make this work?
Your problem is that your web server does not recognize the filename "about" as a php document. You have three options
Use a ".php" extension on your page document, such as "index.php" which will run your php scripting.
Install a solution such as mod_rewrite that will translate urls such as /about to a file actual like "about.php".
Adjust your servers mime-type for php documents. Learn more about MIME types here. https://developer.mozilla.org/en-US/docs/Properly_Configuring_Server_MIME_Types
The first solution is the simplest and easiest, in any document you use PHP, the extension should be .php
About the mywebsite.com/about issue, it's more an Apache configuration issue. You have to tell Apache that index.php should be an index file.
First, you can configure your Apache (or IIS) to use whatever Extensions to process PHP-Code.
You can define .ThisIsAPHPFile as valid extension, if you want.
However, Directorys are always reflected with a trailing /: www.example.com/dir1/ (Browsers not always showing the trailing /) while files have an extension: www.example.com/dir1/index.html.
So, from what i see, you want to use www.example.com/about but showing the about-FILE ?
Therefore you can use rewrite Engines of your Webserver. Either have a look at mod-rewrite (http://httpd.apache.org/docs/current/en/mod/mod_rewrite.html), when using Apache, or (one possibility) ISAPI-Rewrite (http://www.isapirewrite.com/docs/), when using IIS.
If you edit your virtualhost directive in your apache conf file, you can add the following:
DefaultType application/x-httpd-php
This will tell apache to send web paths that do not end in an extension to render using the php engine.
Therefore, /about would act as if it was about.php. Another potentially more useful approach is to name the file about.php on the server, and allow referencing it without the .php in the url. For this, you would configure it the opposite way.
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
Related
How do I point a URL to a file so when I go to the URL it points to that file but doesn't change the URL. For example:
mydomain.com/orders/create should point to /myfiles/orders-create.php
then when I go to mydomain.com/orders/create it will display all the contents of orders-create.php.
Any ideas?
If you want to do it on the server-side you could edit the .htaccess file similar to this question's answer.
The main changes you're looking at are:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*)$ $1.php [NC]
This would let you create files without an extension and it would run force run the PHP interpreter.
Here's a good read for pretty URLs to find out more ways you could do this, and it'll explain more about what they actually are.
If you can change create-order.php, then you can make it so that it does not rely on relative paths. One way to do this is to create a bootstrap.php file that has all of the includes and then your "leaf" PHP files only have to find this bootstrap.php file.
Another option is to set the PHP path. If you change the include path to include the root of the directory, then when including files in create-order.php will look in the root and it will find them. You can set the include path in your PHP files or in the PHP configuration.
If you don't want or can't change create-order.php then one way to do this would be via the webserver. For example, in apache, you can use mod_rewrite to do just that, have a public URL actually invoke a specific local file. The rewrite configuration might look like this (example for just this one file):
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^orders/create to create-order.php$ create-order.php [L]
or a catch-all rule with this (untested):
RewriteRule ^orders/(.*)$ $1?%{QUERY_STRING} [L]
I am new to htaccess files, and I understand how to do basic rewrites of URLs such as removing index.php, extensions, etc. I am also able to use $_SERVER["PATH_INFO"] to work with anything trailing the file.
What I struggle with is how it would be possible to do this with a trailing faux-directory structure on another file other than the (not-shown) index.php. Lets say I have
domain.com/render.php/this
and I want it to read
domain.com/render/this
My workaround is currently to do all my logic in my index.php file, but I would like to break it up into several files, so that I would have index.php doing my home-page stuff, and render.php something completely different.
Thank you for you time.
It depends on your overall directory structure. Take a look at Apache .htaccess to hide both .php and .html extentions, for example.
If you already have /render/this configured to go to /render.php/this, and all you have to do is perform redirection the other way, then try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} \b\.php\b
RewriteRule ^([^/.])\.php/(.*)$ $1/$2 [R]
(The \b part matches at a word boundary, as per pcrepattern(3), which is from the pcre library that both Apache httpd as well as nginx use in support of regular expressions.)
I want to map URL in my localhost XAMPP into custom files.
For example:
localhost/index.php --> d:\xampp\htdocs\index.php (default)
localhost/normal/data.php --> d:\xampp\htdocs\normal\data.php (default)
localhost/view/userinfo.php --> d:\xampp\htdocs\view.php?p=userinfo (custom)
localhost/view/welcome.php --> d:\xampp\htdocs\view.php?p=welcome (custom)
So, basically, all URL that goes into inside view path will be mapped to view.php files with the filename.php (minus the .php) as its query parameter. There's actually no physical folder view, and no physical files userinfo.php and welcome.php inside the folder.
The reason that I need to do this is that so I can pass all the pages that viewing data into an "application frame" that will wrap the page with header, menu, and footer, and I don't need to give header, menu, and footer call in each page. I might have the actual files userinfo.php that I can $include_once, or I might not (I can just generate it from within the view.php), but hey, that's one of the power of this kind of framework, right? And, if someday I need to change this structure, I can change it from just within one file (view.php), not all.
Can I do this in PHP and XAMPP? How? I've noticed that some website seems to used this practice (URL which have no actual files or even path at all), but when I try to read tutorial for it, I got confused.
URL mapping in PHP?
The accepted answer listed 3 links to learn about URL rewriting. Mostly they're written for Apache in Linux, and mostly they pull all the possible scenario and configuration that I got confused which one I really need with all those long documents and technical jargon. I need just the practical step of my specific problem, and then, I will be able to start from there to explore myself if I have more advanced needs. Please help.
if you do want to go down the mod rewrite route adding the following to an .htaccess file in the site root should do it. You will need to make sure mod rewrite is on for XAMPP and I can't help you there I'm afraid. As you can see it rewrites the url, not the windows filename - so it would work on any OS.
The ([a-z]*) means it will take any filename.php with lowercase letters and redirect to /view.php?p=$1 where the $1 will be replaced by filename.
the [L,R] (L means last rule so stop processing if any more are reached, and the R means redirect (it will change the url in the browser). Use P instead to reverse Proxy (the user will still see the url they requested but the server will serve the correct file) - This will require mod_proxy as well.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/([a-z]*).php$ /view.php?p=$1 [L,R]
</IfModule>
XAMPP uses apache so the rewrites would work the same in Windows as they do in Linux. You could place a .htaccess in the site root directory with some rewrite rules.
However, using PHP
in d:\xampp\htdocs\view\userinfo.php you could include the line
<?php
header('Location: http://localhost/view.php?p=userinfo');
?>
But this must be before any thing is echoed to the screen (even whitespace).
You can use the Apache module mod_rewrite to edit requests before they hit PHP. You want to put something like the following in a .htaccess file in your htdocs directory.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/(.*)\.php.*$ view.php?p=$1 [L,QSA]
QSA means Query String Append. This means that if there are any GET parameters set on the original request they will be appended to the end of the new request too.
Note that this assumes that Apache is configured with AllowOverride enabled and the mod_rewrite module loaded.
I have a file
http://www.www.com/somefile.php
I would like to be able to get to the same file by going to
http://www.www.com/somefile
But I still need
http://www.www.com/directory
to open the file
http://www.www.com/directory/index.php
if, in fact, directory is a directory.
This is a typical LAMP setup with Red Hat and Apache.
Is that possible? If so, how?
An apache rewrite rule will do this for you. The only limitation is you can't have a file named "somefile.php" and a directory named "somefile" (though you can still have directories.
Here's an example rule that supports multiple filenames (just put in in your .htaccess file).
RewriteEngine on
RewriteRule ^(somefile1|somefile2|somefile3) $1.php
Will change /somefile1 to /somefile1.php, /somefile2 to /somefile2.php, and /somefile3 to /somefile3.php.
Look at Apache's ForceType.
If you want to disable the need of file extensions on the url globally on your site, you can also add the following to your .htaccess:
Options +MultiViews
This will enable Content Negotiation. But if you want to match only certain files and/or urls, stick with SoapBox's Answer instead.
I'm writing multilingual website. I have several files on server like:
/index.php
/files.php
/funny.php
And would like to add language support by placing language code into URL like this:
http://mywebsite/en/index.php
would redirect to:
http://mywebsite/index.php?lang=en
And
http://mywebsite/en/files.php
would redirect to:
http://mywebsite/files.php?lang=en
I would like to put more languages for example:
http://mywebsite/ch-ZH/index.php
And I would like this to work only for files with php and php5 extension. Rest of files should be the same as they are.
So for example when i will go to address
http://mywebsite/ch-ZH/index.php
I would like my PHP to recognize that current path is
http://mywebsite
and NOT
http://mywebsite/ch-ZH
It's necessary for me because in my PHP code I relate on current path and would like them to work as they are working now.
Could you please write how to prepare htaccess file on Apache to meet this criteria?
Try this:
RewriteEngine on
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*) $3?lang=$1 [L,QSA]
And for the current path problem, you have to know how relative URIs are resolved: Relative URIs are resolved by the client from a base URI that is the URI (not filesystem path!) of the current resource if not declared otherwise.
So if a document has the URI /en/foo/bar and the relative path ./baz in it, the client resolves this to /en/foo/baz (as obviously the client doesn’t know about the actual filesystem path).
For having ./baz resolved to /baz, you have to change the base URI which can be done with the HTML element BASE.
If you don't want to use ModRewrite, you can put symbolic links in the web folder (ln -s . en) and check the URL in PHP.
Something like this should do the trick,
RewriteEngine on
RewriteRule ^(.[^/]+)/(.*).php([5])?$ $2.php$3?lang=$1 [L]
This will only match .php and .php5 files, so the rest of your files will be unaffected.