Create a Catch-All Handler in PHP? - 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

Related

LimeSurvey: Skip "The following surveys are available" Page?

I have recently installed the latest LimeSurvey, and while it has many features I like, I only really intend on having one survey available at a survey.example.com subdomain.
I want to skip the page that states "The following surveys are available:" which is the "survey.example.com/index.php". and go straight to the survey, which is "survey.example.com/index.php/311746?lang=en"
I tried setting a DirectoryIndex in .htaccess but that didn't do anything
DirectoryIndex index.php/311746?lang=en
I've tried playing with mod_rewrite, but LimeSurvey itself already has conditions set up, so whatever I tend to do breaks theirs (likely since they're already rewriting index.php).
<IfModule mod_rewrite.c>
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php
</IfModule>
I was going to try a redirect, but the index.php is more than just the index, so I don't want to change that too much.
I've tried searching around but I haven't had much luck.
Then why not just rewrite the main rule of the survey. Instead of rewriting it to the main index.php which you don't want to do, write it directly to the survey you want. You should be able to use this in your htaccess to go directly to the survey.
<IfModule mod_rewrite.c>
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to the survey
RewriteRule . index.php/311746?lang=en [R=301,L]
</IfModule>
Let me know if this solves your issue

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.

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

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

How do i map myapp/index.php/controller/view to myapp/controller/view?

I am using CodeIgniter framework for PHP. It requires that index.php should be there in whatever url i type. How do i prevent this? Can anybody provide me a simple .htaccess file that will implicitly map it to index.php so user doesn't have to type it every time?
Thanks in advance:)
You need to read this
http://codeigniter.com/wiki/mod_rewrite/
Because you'll also need to make a small code change (in step 2) not covered in Svens answer.
You can use a .htaccess file (or better apache.conf) to forward all requests to index.php with mod_rewrite:
Options +FollowSymLinks
IndexIgnore */*
<ifmodule mod_rewrite.c>
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
</ifmodule>
If your request is http://www.example.com/controller/view, than $_SERVER['REQUEST_URI'] is /controller/view. You can interpret it with Regex or by simply splitting string: explode('/',$_SERVER['REQUEST_URI']);

posting data to a web page,need an alternative

To request some data from a web server, we can use the GET method,like
www.example.com/?id=xyz
but I want to request the data like
www.example.com/xyz
How can it be achieved in PHP?
Create a file in your root directory and call it .htaccess. Put this in it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [R=301,L]
If someone goes to www.example.com/xyz and xyz is not a directory or a file it will load /index.php?xyz instead. It will be completely transparent to your users.
You could use mod-rewrite, some more info is here
http://www.trap17.com/index.php/php-mod-rewrite-tutorial_t10219.html
I'm not sure "posting" the data is the right terminology, but you can use Apache mod_rewrite to make URLs like '/xyz' direct to your PHP application. For example, place a .htaccess file in your web root with the following,
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Now the URL specified is available in $_GET['url].
I don't think you can achieve what you want with the GET method, as PHP will always append form data in a query string to the end of the URL specified in your form's action attribute.
Your best bet is to post the data to a handler (i.e. www.example.com/search) and then use that page to redirect to the correct page.
So if you entered a query for hello+world, that variable would be passed to your /search page and processed by the PHP script to re-direct to /hello+world.
Of course, you're going to need the correct .htaccess rules in place to handle searches like this, as well as sanitizing data.

Categories