php, url hiding with the .htaccess - php

This is the real url:
http://web/app/index.php?id=1
Here I have used current .htaccess and working fine with me.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1
Now the url is : http://web.com/app/index/i and working fine
but here is the problem the real url
http://web.com/app/index.php?id=1&name=abc
How can I set mention url with .htaccess for keep it short or any other good solution for hiding URL-.
is it possible to show user only one url like , http://web.com/app but it could go to other pages while the url stay one url static.

If we say, good practice is to rewrite everything to one index page, like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
then in your index.php you get uri by $uri = $_SERVER['REQUEST_URI'] :
http://example.com/app -> $uri = /app
http://example.com/app/sub-app -> $uri = /app/sub-app
but also query string applies to the $uri variable:
http://example.com/app/?a=1&b=3 -> $uri = /app/?a=1&b=3
in this case, if you want to examine just uri part before ? question mark:
$realRri = strtok($uri,'?'); // and you have '/app/' instead of '/app/?a=1&b=3'
next you can examine and manipulate $uri;
Example:
$trimmedUri = trim($realRri, '/');
if ($trimmedUri == 'app')
{
// you can show app page
}
elseif ($trimmedUri == 'contact')
{
// show contact
}
else
{
// show 404 page not found
}
// you can have some dynamic also also
if (is_file($filePath = 'custom-page/'.$trimmedUri))
{
include $filePath;
}
else
{
// 404 page
}
Final Code (regarding your current script)
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
#rewrite php file
RewriteRule ^([a-zA-Z]+)$ $1.php
#rwwrite with id
RewriteRule ^([a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2
#rewrite with id and name
RewriteRule ^([a-zA-Z]+)/([0-9]+)/([0-9a-zA-Z]+)/?$ $1.php?id=$2&name=$3

I think what you're looking for is something like AJAX, mostly because of
but it could go to other pages while the url stay one url static.
in which case, you would looking at JavaScript and not php. You would need a way to quickly deal with the data and change it how you want it. I've recently started learning Angular, and it shows a lot of promise. Go through the tutorial yourself (just google angular.js and the tutorial is on their website), or some other JavaScript library (unless you want to hard-code everything, in which case, good luck) like jQuery or MooTools (names mentioned because they're libraries i have knowledge with). To my knowledge (I am no code guru) but a website's URI is what tells the server what to show you. You can make shortcuts
If, however, you want for it to just not have the .php filename, then I believe that
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)\.php\??(.*)$ $1.php&$2
Should, theoretically get you what you want.
Note: I can't test it right now, so it may error out.
EDIT: It does error out.

Related

.htaccess PHP Parameter Friendly URL

I would like to make the URLs of my Store URL-friendly.
Current URL Structure
https://my-domain.com/store/store.php?page=packages&id=1
Desired URL Structure
https://my-domain.com/store/packages/1
And also for direct access to the PHP files such as:
https://my-domain.com/store/profile.php to https://my-domain.com/store/profile
How would I need to go after this? I really appreciate any help you can provide.
Also might be note worthy that in the base directory a WordPress site is running with its own .htaccess file.
I already tried it with this
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/store/page/(.*)/id/(.*) /store/store.php?page=$1&id=$2
RewriteRule ^store/store/page/(.*)/id/(.*)/ /store/store.php?page=$1&id=$2
But that didn't work
This code will work.
RewriteEngine will remove .php from all PHP Files
RewriteRule will rewrite url like page/id
For Removing .php extension
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
For page/id
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)? store.php?page=$1&id=$2 [L]
</IfModule>
You can use this for the first part:
RewriteRule ^store/((?!store)[^/]+)/([^/]+)$ /store/store.php?page=$1&id=$2 [L]
Although nothing is wrong with anyone else's answers, the more modern way to do this (including WordPress, Symfony and Laravel) is to send non-existent URLs to a single router script. By doing this, you only have to mess with an htaccess file once to set things up, and never touch it again if you add more "sub-folders", you can do all of that in just PHP. This is also more portable which means you can bring it to other server platforms such as Nginx with little changes, and don't need to deal with RegEx.
The htaccess is fairly straightforward. Route all requests that start with /store/ and don't exist as a file (such as images, JS and CSS) or directory to a single new file called router.php in your /store/ folder. This is an internal redirect, which means it isn't a 301 or 302.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/ /store/router.php [L]
Then in your new router.php file you can parse $_SERVER['REQUEST_URI'] to determine the URL that was actually requested, and you can even rebuild the global $_GET variable:
// Parse the originally requested URL into parts
$requestUrlParts = parse_url($_SERVER['REQUEST_URI']);
// Parse the query string into parts, erase the old global _GET array
parse_str($requestUrlParts['query'], $_GET);
// Handle
switch($requestUrlParts['path']){
case '/store/store.php';
include '/store/store.php';
exit;
// Custom 404 logic here
default:
http_response_code(404);
echo 'The page you are looking for cannot be found';
exit;
}
I'd also recommend putting the htaccess rule into the site root's htaccess folder, above WordPress's. There's nothing wrong with creating multiple files, this just keeps things in a central place and makes it easier (IMHO) to debug.

I cannot use $_Get when I add my .htaccess

I have a website that works fantastic. I'd like to organize it a little better so instead of loading every file by typing it in the url, i'll just make a index file that can include the files by a get method.
$key = $_GET['KEY'];
if ($key == 'signup'){
include "forming/signup.php";
}
To make the url a little cleaner I added this in the .htaccess file
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ index.php?KEY=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?KEY=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Which turned
localhost:8888/index?KEY=signup
into
localhost:8888/signup
Now the only problem is that when I use the $_GET, it simply doesn't notice it. I noticed it by a redirect (Programmed in If statement) so I tested it by making a file and with this code
<?php
$test = $_GET['id];
echo "<h1>" . $test . "</h1>";
And it didn't work, like expected.
I don't know why $_GET stopped working after I used that .htaccess code.
(I'm a novice at .htaccess)
I would truly appreciate if anyone could tell me another way to cleanse the url and still have the $_GET working. (Or just if you see any error in the .htaccess)
UPDATE
I'd like to clarify. Essentially I want this
localhost:8888/index.php?KEY=post&id=1
to
localhost:8888/post/1
(But for every post, not just nr 1)
As PHP is a server-side language, $_GET only works on what's actually in the URL string at the time the request is sent to the server. If you purely want to alter what's visible in the address bar in order to achieve a cleaner look, you'll need to use a server-side language.
Luckily you can do this with Javascript pushState. There's an answer here that covers it in all possible detail.
If you want my advice, follow these steps to get the best site :)
1 -> First, create folder in your public_html , name it : "auth" for example
in this folder you can upload you files like "signin.php" , "signup.php" , "forget_password.php"
2 -> in your htaccess file , put this :
I've written this file for you, he is ready :)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
deny from 10.0.0.182
AddDefaultCharset utf-8
Options -MultiViews
Options -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
#REDIRECTS HERE LIKE REF SYSTEM
RewriteRule ^ref/(.*)$ auth/signup?ref=$1 [L]
</IfModule>
</IfModule>
3 -> Your links now is :
http://example.com/auth/signin
http://example.com/auth/signup
http://example.com/auth/forget_password
http://example.com/ref/username
NOTE : in ref system , you can get username value from $_GET['ref']

how to redirect a URL according to get variables in URL

I am trying to change my website URL according to get variables so that I can increase the security of my website.
For example I want to change my address, this is my htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /category.php?cat_id=$1&mode=full&start=$1
And my website URL is:
http://joinexam.in/category.php?cat_id=17&mode=full&start=36
I want to convert this URL to:
http://joinexam.in/category/1736
where cat_id = 17
and start= 36
So it will become 1736 after the category.php page, I am trying to do it by using .htaccess file.
Here I want to take both cat_id and start as get variable, then according to these get variables, I want to change the URL of my website.
Can anyone explain the correct way to achieve this?
.htaccess
This forwards every URL to index.php.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
PHP
Get the "original" URL inside index.php:
// this gives you the full url
$request_uri = $_SERVER['REQUEST_URI'];
// split the path by '/'
$params = split("/", $request_uri);
Then you might route based on these $params.
As a good and fast router lib i suggest: https://github.com/nikic/FastRoute
By using this, you don't have to mess around with htaccess regexp stuff and can keep things at the PHP level :)

URL parameters with PHP

Are there ways to pass variables in a URL similarly to GET data? For example, with slashes?
I currently have a single .php file which reloads a div with different content using javascript to navigate pages, but as you can imagine, the address in the address bar stays the same.
I want users to be able to link to different pages, but that isn't possible conventionally if there is only one file being viewed.
You're probably going to want to use something along the lines of Apache's mod_rewrite functionality.
This page has a nice example http://www.dynamicdrive.com/forums/showthread.php?51923-Pretty-URLs-with-basic-mod_rewrite-and-powerful-options-in-PHP
Try using:
$_SERVER['QUERY_STRING']; // Or
$_SERVER['PHP_SELF'];
If that doesn't help, post an example of what kind of URL you are trying to accomplish.
Something like this might do the trick;
place this in /yourdir/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ yourindexfile.php?string=$1 [QSA,L]
All requests will be sent to yourindexfile.php via the URL. So http://localhost/yourdir/levelone becomes yourindexfile.php?string=levelone.
You'll be able to break down the string like so;
$query= explode('/', rtrim($_GET['string'], '/'));
the technology your looking for is .htaccess. technically this isn't possible, so you'll have to hack your mod rewrite to accomplish this.
RewriteRule On +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(user)/([^\.]+)$ ./index.html?tab=user&name=$2
add this to your .htaccess page in your top directory. you'll have to alter your website structure a little bit. assuming that index.html is your index. this is a backwards rewrite so if one was to go to the page with the query string it won't redirect them to the former page and if one went to the page without the query string it will work like GET data still.
you GET this data with your php file using $_GET['tab'] and $_GET['name']
I think the Symfony Routing Component is what you need ;) Usable as a standalone component it powers your routing on steroids.
I'm doing it like this (in my like framework, which is a fork of the JREAM framework):
RewriteEngine On
#When using the script within a subfolder, put this path here, like /mysubfolder/
RewriteBase /mysubfolder/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Then split the different URL segments:
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url_array = explode('/', $url);
Now $url_array[0] usually defines your controller, $url_array[1] defines your action, $url_array[2] is the first paramter, $url_array[3] the second one etc...

htaccess and php - redirects and pretty urls

I have a webcommunity, and it's growing now. I like to do a link makeover for my web, and then I need to know the best solution for my case.
Right now my htaccess looks kind of like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]
You are able to link to users like this domain.com/username and that's nice.
Then I have different pages like
index.php?page=forum&id=1
index.php?page=someotherpage&id=1&anotherid=5
index.php?page=3rd
... and so on. I want them to look something like this:
domain.com/forum/23/title-of-the-thread
domain.com/page2/id1/id2
... and so on.
How do I make these pretty urls without removing my domain.com/username functionality? What solution would you suggest?
I was thinking about creating a file that checks the URL, if it matches any pages, and users and so on. Then it will redirect with a header location.
If all of the urls you are going to rewrite are going to the same end point, you could simply use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
in index.php:
<?php
$url = $_SERVER['REQUEST_URI'];
How you use the request uri is up to you, you could for example use a simple strpos check:
<?php
$url = $_SERVER['REQUEST_URI'];
$rules = array(
'/forum/' => 'forum',
'/foo/' => 'foo',
'/' => 'username'
);
foreach($rules as $pattern => $action) {
if (strpos($url, $pattern) === 0) {
// use action
$file = "app/$action.php";
require $file;
exit;
}
}
// error handling - 404 no route found
I was thinking about creating a file that checks the URL,
you actually have that file, it's index.php
if it matches any pages, and users and so on. Then it will redirect with a header location.
that's wrong. HTTP redirect won't make your URLs look "pretty"
you have to include appropriate file, not redirect to.
Just change your rule to more general one
RewriteRule ^(.*)$ index.php [L,QSA]
You basically have two options.
Route all URLs to a central dispatcher (FrontController) and have that PHP script anaylze the URL and include the correct scripts
Note every possible route (url rewrite) you have in the .htaccess
I've always worked with option 1, as this allows greatest flexibility with lowest mod_rewrite overhead. Option 2 may look something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^forum/([^/]+)/([^/]+)/?$ index.php?page=forum&id=$1 [L]
RewriteRule ^otherpage/([^/]+)/([^/]+)/?$ index.php?page=someotherpage&id=$1&anotherid=$21 [L]
RewriteRule ^page/([^/]+)/?$ index.php?page=$1 [L]
# …
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]
you said
I was thinking about creating a file that checks the URL, if it
matches any pages, and users and so on. Then it will redirect with a
header location.
While "creating a file that checks the URL" sounds a lot like option 1, "redirect with a header location" is the worst you could do. That would result in
an extra HTTP roundtrip for the client, leading to slower page loads
the "pretty URL" won't stick, the browser will show the URL you've redirected to
losing link-juice (SEO)
This can be done entirely with htaccess or php
//First Parameer
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?page=$1&username=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?page=$1&username=$2
read more about it here:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

Categories