htacess url rewrite? on a php get system - php

I use the code below to basically go to my pages. How it works is I put index.php?req=pagename and it will check my protected folder to see if the file is there if it is then it goes there. I need a mod rewrite so that it doesnt show all that index.php?req=pagename and just shows /pagename
require_once("protected/header.php");
if (isset($_GET['req'])) {
$req = $_GET['req'];
} else {
$req = "overall";
}
require_once("protected/$req.php");
require_once("protected/footer.php");

Your code allows any php file (barring safemode/open_basedir restrictions) to be parsed and executed. You need to escape that input first, even if it's something as rudimentary as removing slashes, tildes and periods.
As far as rewrite goes, simply create a .htaccess file in your document root along the lines of:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?req=$1 [QSA,L]
</IfModule>

You will have to use POST instead of GET if you want a clean dynamic url. Your question though doesn't make much sense.

Related

Point string to php page and query

I've been trying the past 2-3 hours to get this working but I simply don't have enough knowledge of regex.
I'm trying to make an url shortener, so I want to point a url like domain.com/d5Ds93X to something like domain.com/view.php?s=d5Ds93X
I've been messing around with my .htaccess file and have this now:
RewriteEngine on
RewriteBase /
RewriteRule /(.*?)$ /view.php?s=$1 [L]
And that sort of works, it points to the right file but the string (d5Ds93X) isn't passed on. My view.php file looks like this (for testing purposes):
<?php
if (isset($_GET['s']) && ctype_alnum($_GET['s'])) {
echo $_GET['s'];
}
?>
When visiting the url I just get a blank page. But if I echo anything before the if rule it does get displayed so the page works just fine. Does the data of the GET even get passed on? Or should I add code that detects the data in the url?
Use it like this:
RewriteEngine on
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ view.php?s=$1 [L,QSA]
Make leading slash optional so that rule works from .htaccess as well as from Apache config.
Also restrict rule to skip files and directories.

multitwitch style url rewrite

I'm trying to recreate the url rewriting that multitwitch.tv uses however am having issues in doing this as google is being unhelpful thinking I want to remove trailing slashes.
I'm trying to effectively do this:
site.com/aaa/bbb/ccc/ddd/eee
site.com?aaa&bbb&ccc&ddd&eee
Although multitwitch's source is public on Github it's python and not PHP with a .htaccess file so I've not been able to solve this for a while now.
Any help will be greatly accepted.
user inputted url: site.com/aaa/bbb/ccc/ddd/eee
rewrite to: site.com?aaa&bbb&ccc&ddd&eee
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?channels=$1 [NC,L]
php:
<?php
foreach($_GET as $key => $value){
echo $key;
}
?>
The problem is you want a bunch of levels deep. It might be easier to handle that in php rather than creating a rule for every sub directory added in the URI.
So I would probably do something like this. This will send every request to PHP.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [NC,L]
Then you can type in http://example.com/aaa/bbb/ccc/ddd
In PHP in your index.php you can then do
<?php
$aURI = explode("/",$_SERVER["REQUEST_URI"]);
print_r($aURI);exit;
?>
Then use the parts as you need them. You could even count the number of pieces in the new array and based on that you can do stuff. This way you don't have to have a .htaccess rule for every level you want to add to the URL.

Can't get mod_rewrite to work

I am trying to get url from:
192.168.0.1/movie-page.php?id=123
to:
192.168.0.1/movie/movie-name
or even (for now):
192.168.0.1/movie/123
I've simplified it by using this url (to get something working):
192.168.0.1/pet_care_info_07_07_2008.php TO 192.168.0.1/pet-care/
my .htaccess file:
RewriteEngine On
RewriteRule ^pet-care/?$ pet_care_info_07_07_2008.php [NC,L]
What am I doing wrong? I've tried many combinations but no luck and my patience is running out...
I am running this on my local NAS which should have mod_rewrite enabled by default. I have tested .htaccess by entering random string in .htaccess file and opening the page, I got 404 error. I assume this means that .htaccess is being used since the page stops functioning if the file is malformed.
If you want to rewrite:
192.168.0.1/movie-page.php?id=123 too
192.168.0.1/movie/movie-name or 192.168.0.1/movie/123
Then you would do something like, but will require you manually add a rewrite for any new route (fancy url) you want, and eventually you may want your script to create routes dynamically or have a single entry point to sanitize:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^movie/([a-zA-Z0-9-]+)$ movie-page.php?id=$1 [L]
So a better method is to route everything through the rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
Then handle the route by splitting the $_GET['route'] with explode()
<?php
//192.168.0.1/movie/movie-name
$route = (isset($_GET['route'])?explode('/',$_GET['route']):null);
if(!empty($route)){
//example
$route[0]; //movie
$route[1]; //movie-name
}
?>
You want something like this:
RewriteRule ^movie/([0-9]*)$ /movie-page.php?id=$1 [L,R=301]
That will give the movie ID version with a numeric ID.

.htaccess redirect or PHP change

The main navigation of my site is coded like this:
<li>'.$value.'</li>'."\n";
But I would like the URL of the links to look like domain.co.nz/pagename, not domain.co.nz/index.php?pageId=pagename
How would you recommend I accomplish this?
Something like this should work:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?pageID=$1
First line turns on mod_rewrite. Second line sets the base URL to / (it's annoying, but you have to set it to the base path you're dealing with). Third and fourth lines make sure the request doesn't exist as a file, or as a directory. And the last line is the actual magic; basically it searches for "anything", captures what it finds in $1, and "rewrites" the URL to index.php?pageID=$1. If you learn to use regexes, you can do much more complicated things as well.
Yes, you can accomplish this with a .htaccess RewriteRule. In your .htaccess file, include:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?pageId=$1
This means:
If the REQUEST_FILENAME is not a valid file, redirect the entire URL to index.php?pageId=the entire URL
You'd then change your navigation to:
echo "<li>'.$value.'</li>'."\n";
Edit: I moved Trivikrtam's edit inline, see above. The RewriteRule should be index.php?pageId=$1 not /index.php?pageId=$1. Thanks #Trivikrtam!

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