How to get to php pages without using .php in URL - php

I have a static website with files like index.php, blog.php, contact.php etc
How can I get my website addresses to work so that www.site.com/blog takes you to blog.php?
I think htaccess could do this for me, but am a php noob!
The only alternative I currently use is to create individual folders called 'blog, contact etc' which contains another index.php file inside it
thanks

Yes, you can use mod_rewrite to rewrite all urls. The following will rewrite all non-existing files and folders to requested filename .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
Visiting /blog and it's not an existing directory will cause this rule to rewrite it as /blog.php.

Something like this should do it.
RewriteEngine on
RewriteRule ^(.*)$ $1.php [NC]

Have a read on mod_rewrite: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

I have a static website with files like index.php, blog.php, contact.php etc
If you are generating your documents with PHP, then the site is dynamic, not static.
How can I get my website addresses to work so that www.site.com/blog takes you to blog.php?
Assuming you are using Apache, turn on MultiViews

In your .htaccess file on your server add
Options +MultiViews

Related

How can I load my PHP file for any HTTP requests inside a directory?

I have a proxy I've been writing with PHP for a while.
Right now, requesting a url like www.mysite.com/proxy/?folder/page.html on my server will return the page at www.theirsite.com/folder/page.html.
I basically am having my index.php file use everything past /proxy/? as the request URI for www.theirsite.com. Any images are copied to the folder /proxy/images/ and the src attributes of the <img> tags are changed accordingly. All this is working great.
Now I would like to change my script so that I will not need the ? anymore. However, the url www.mysite.com/proxy/folder/page.html would result in an HTTP request to page.html, which doesn't exist on my server.
This isn't what I want. I need index.php to be loaded instead, so it can return the page at www.theirsite.com/folder/page.html. To accomplish this, I imagine I would need to use Apache's mod_rewrite, which is working with my WordPress installation.
What would I need in my .htaccess file to do this correctly, while still allowing access to files that exist in the /proxy/images/ directory? Would this affect $_SERVER['REQUEST_URI'] at all?
RewriteEngine on
RewriteRule ^/$ /index.php [R]
Well, I got it working how I wanted. Here's the full contents of my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /proxy/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /proxy/index.php [L]
</IfModule>
Now for the url www.mysite.com/proxy/folder/page.html, index.php is loaded, and $_SERVER['REQUEST_URI'] returns /proxy/folder/page.html. Files that actually exist in the /proxy/ folder or any subfolders do not rewrite to index.php, which is what I wanted.

What's Wrong? Nothing Will Load (Almost)!

Okay...My title is a bit of an exaggeration...
My site is built in PHP, and all the files I'm trying to "require_once" aren't being loaded. The only file I've changed is my .htaccess file. I don't know a thing about .htaccess and what I have is purely from searching the web. What is wrong? Thanks for any help in advance.
RewriteEngine on
<Files 403.shtml>
order allow,deny
allow from all
</Files>
ReWriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule !index.php index.php [L]
Also, if I comment out the bottom two lines, my site works great.
Well, require_once has nothing to do with .htaccess file: it's a PHP directive, not an Apache one. You have to set correctly the include_path for your files and make sure these directories and files are reachable (i.e., with correct privileges set on them).
If you show the error message you got from failed require, it'd be much more simple to give you a specific advice on how to fix it.
UPDATE If what you need is redirecting all the non-AJAX requests for .php files into index.php, your .htaccess should like this:
RewriteEngine on
RewriteCond %{HTTP:x-requested-with} ^XMLHttpRequest$
RewriteRule . - [L]
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule .php$ index.php
This basically means the following: "all AJAX requests - go for what you need, all non-AJAX requests IF you're not going for some directory and are ended with .php - go for index.php instead".
Without checking for .php (or some similar check) you will redirect to index.php all the script loading procedures; and, unless you do it from some external CDN, it's not what would work in your case. )
Try changing the last two lines to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
If you want your URL's to look something like this (you probably do):
http://yoursite.com/some/path/somewhere
then change the last line to:
RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2
If that's what you want to achieve, ensure that if you're trying to go to:
http://yoursite.com/about
That there isn't actually a folder called about, this line:
RewriteCond %{REQUEST_FILENAME} !-d
Checks to see if a folder with the name "about" exists, if it does, then the page will not redirect, the same goes for files, say you go to:
http://yoursite.com/about.html
If about.html actually exists then the page will not redirect.
Hope that makes sense.
If you need more information, http://jrgns.net/content/redirect_request_to_index seems to be fairly succinct and helpful.

Two htaccess files

I have a question about using multiple .htaccess files - I couldn't find the answer to this after looking elsewhere on stackoverflow, so I hope you guys can help.
I currently have one .htaccess file in the root of my site, which performs a simple url rewrite:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [L]
ErrorDocument 404 /index.php
I'm currently working on the second phase of development of this site, and I've made a replica in a subfolder (e.g. www.abcdef.com/new/). The trouble is, at the moment if I click a link on this replica site, it redirects me to the root, original page, whereas I want it to go to the equivalent page in the new/ folder. I've put another .htaccess file in this new/ folder, which however doesn't have any noticeable effect:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /new/index.php?url=$1 [L]
ErrorDocument 404 /index.php
So my question is: is it permissible to have another .htaccess file in a subfolder like this? And if so, why aren't the above lines working?
Thanks in advance for any ideas on this!
It's possible to have multiple .htaccess files, and the system is designed to work the way you want it to.
You're setting RewriteBase, which explicitly sets the base URL-path (not filesystem directory path!) for per-directory rewrites.
So it seems like your requests would be rewritten to /new/new/index.php, a path and directory which probably doesn't exist on your filesystem (thus not meeting your RewriteConds) and such is being redirected to your /index.php 404.
As a test, perhaps try changing the ErrorDocument to:
ErrorDocument 404 /new/index.php
If you see rewritten calls go to this then it might indeed be your RewriteBase.
You say
The trouble is, at the moment if I click a link on this replica site,
it redirects me to the root, original page, whereas I want it to go to
the equivalent page in the new/ folder.
Could it be that you are using absolute links in your pages and not relative ones? For instance if a link looks like "/sample", when in your main site it will link to http://.../sample and the same is true if the link is inside a page under "/new/". If you'd use just "sample" then that would resolve as http://..../sample or http://...../new/sample, depending on the URL of the page.
Having a second htaccess file in a subdirectory shouldn't be an issue, and as far as I can tell, your two look okay.
Are you sure the links in the site are correct? (ex, they are /new/foo, not just /foo)?

How Do I Disable Unwanted Requests? (MySQL, .htaccess, PHP)

To start, I am a beginner with PHP and .htaccess. Here is my dilemma...
I have built dynamic pages and used htaccess to rewrite the urls. There are 3 types of pages... Examples:
State: example.com/massachusetts-colleges.html
City: example.com/massachusetts-colleges/boston-ma-colleges.html
College: example.com/massachusetts-colleges/boston-ma-colleges/harvard.html
The problem is that pages are being requested (from old linking structure probably) that shouldn't exist such as:
example.com/boston-ma-colleges.html
The state urls are stored in a locations table in the database (stateSlug = massachusetts-colleges). The city urls are also stored in the locations table in the database and the corresponding state slug is also stored with that city (citySlug = boston-ma-colleges and stateSlug = massachusetts-colleges). The Colleges are stored in a different table and use ID's to correspond with the cities.
How can I use .htaccess to prevent any "OTHER" urls from being accessible (page displays template and no data), and show a 404 page (or redirect to home page)?
This is what my .htaccess file looks like now:
RewriteEngine on
RewriteRule ^([^/\.]+)\.html?$ php/statePage.php?stateSlug=$1 [L]
RewriteRule ^([^/\.]+)-colleges/([^/\.]+)\.html?$ php/cityPage.php?citySlug=$2&stateSlug=$1 [L]
RewriteRule ^([^/\.]+)-colleges/([^/\.]+)/([^/\.]+)\.html?$ php/collegePage.php?collegeSlug=$3&citySlug=$2&stateSlug=$1 [L]
Again, I am somewhat new to the htaccess and php languages. I would appreciate any help in this matter.
Thank you!
Assuming all of your content is matched by one of the above URLs, you can simply forbid access to everything else by adding another rule to the end:
RewriteRule ^(.*)$ - [F]
To avoid messing with requests your images and CSS, you should add:
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|gif|js|css)$ [NC]
RewriteRule ^(.*)$ - [F]
If you have directories which should be made inaccessible, place a .htaccess in them with only the following:
Order deny,allow
deny from all
Try the following, but replace index.php with your 404 page:
Order allow,deny
Allow from all
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$l [L]
This should redirect all file and directory requests that were not found to index.php or your choice of script file, a 404 page for example. Just remember to set the headers to a 404 response code.
I got this from the CodeIgniter wiki page at http://codeigniter.com/wiki/mod_rewrite/
Their wiki page was very helpful when I tried to solve a similar problem.
rewrite all your public files to fake directory /allow/.., display 404 for any request which is not in /allow/.. directory. Finally rewrite files from /allow/.. to real directory.
It's great workaround IMO :) Better than using deny all in every dir...

Create a Catch-All Handler in PHP?

I want to have a PHP file catch and manage what's going to happen when users visit:
http://profiles.mywebsite.com/sometext
sometext is varying.
E.g. It can be someuser it can be john, etc. then I want a PHP file to handle requests from that structure.
My main goal is to have that certain PHP file to redirect my site users to their corresponding profiles but their profiles are different from that URL structure. I'm aiming for giving my users a sort of easy-to-remember profile URLs.
Thanks to those who'd answer!
Either in Apache configuration files [VirtualHost or Directory directives], or in .htaccess file put following line:
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,NC,QSA]
</IfModule>
It will silently redirect all incoming requests that do not correspond to valid filename or directory (RewriteCond's in the code above make sure of that), to index.php file. Additionally, as you see, MultiViews option also needs to be disabled for redirection to work - it generally conflicts with these two RewriteCond's I put there.
Inside index.php you can access the REQUEST_URI data via $_SERVER['REQUEST_URI'] variable. You shouldn't pass any URIs via GET, as it may pollute your Query-String data in an undesired way, since [QSA] parameter in our RewriteRule is active.
You should use a rewrite rule..
In apache (.htaccess), something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
Then in your index.php you can read $_GET['url'] in your php code.
You can use a .htaccess file (http://httpd.apache.org/docs/1.3/howto/htaccess.html) to rewrite your url to something like profiles.websites.com/index.php?page=sometext . Then you can do what you want with sometext in index.php.
An obvious way to do this would be via the 404 errorDocument - saves all that messing about with mod_rewrite.
If you have not heard about MVC, its time you hear it, start with CodeIgniter, its simplest and is quite fast, use default controller and you can have URLs like
domain.com/usernam/profiledomain.com/usernam/profile/editdomain.com/usernam/inboxdomain.com/usernam/inbox/read/messageid Or use .htaccess wisely

Categories