I'm trying to get action value from variables in this url.
Real Url
index.php?action=ok&id=45&name=LG-Optimus
Friendly Url
home/ok/1/LG-P88
This above friendly url show when redirect an page like this
header('Location: /home/ok/' . $id . '/' . $name);
The page script that contains the code to get the variables
$ok = isset($_GET['ok']) ? $_GET['ok'] : "";
if($ok=='ok'){
echo "<div class='popup'>";
echo "<strong>{$card-name}</strong> added!";
echo "";
echo "</div>";
}
But i get 'Undefined index ok'.
The .htaccess file contains this code
# Turn Rewrite Engine On
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# Rewrite for index.php
RewriteRule ^home index.php [NC,L]
# Rewrite for card-name
RewriteRule ^home/([0-9]+)/([0-9a-zA-Z_-]+)$ index.php?action=$1&id=$2&name=$3 [QSA,L]
Thanks for any help.
You are setting the GET-variable action to "ok", but your code presumes the variable is NAMED "ok".
To check it in your code you should use:
$action = isset($_GET['action']) ? $_GET['action'] : "";
if($action=='ok'){
Another thing, your regular expression lacks the "action" argument, try this:
^home/(.*)/([0-9]+)/([0-9a-zA-Z_-]+)$
You have some errors in this code. use this code
RewriteRule ^home/([a-z]+)/([0-9]+)/([0-9a-z_-]+)$ index.php?ok=$1&id=$2&name=$3 [QSA,L,NC]
Also for security reasons you must avoid from direct use of $_GET . (see)
Related
SOLUTION:
it seems htaccess requires 'B' for it to retain the ampersand
so using
RewriteRule ^album/([a-zA-Z0-9-=_.]+)$ view.php?album=$1 [L,B]
works
SOLUTION
I am trying to read the get value from the url which is:
view.php?album=something
This is one of my rules in my .htaccess file:
#this rewrite 'view.php?album=something' to 'album/something'
RewriteRule ^album/([^/\.]+)/?$ ^view.php?album=$1 [L]
The problem is that sometimes the album name ie 'something' may contain and & in the name and this causes $_GET to trip up.
**For example**
album name is 'football&basketball'
The view.php page will only read up to football and throw an error.
Without the rewrite rule it works perfectly fine, but with the rewrite rule it messes up for that one case.
I am using rawurlencode at the moment
If there is no & in the name then it also works fine - its only with the & when it messes up
Does anyone have any suggestions on how to overcome this? I have tried using htmlspecialchars and htmlentities etc.
EDIT: Added Code
htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
#rewrite view.php?album=whatever to album/whatever
RewriteRule ^album/([a-zA-Z0-9-=_.]+)$ view.php?album=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /ShyamJoshi/dist/
#remove .php and .html extension
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteOptions Inherit
ReWriteCond %{HTTP:accept-encoding} (gzip.*)
ReWriteCond %{REQUEST_FILENAME} !.+\.gz$
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.+) $1.gz [QSA,L]
</IfModule>
PHP:
This function is being used twice. First it shows all directories and then it shows all images in directories (which is the 'else' part)
function getImagesOrImageDirs($dirOnly) {
$dirLoc = getcwd() . '/images/events/';
$allDirs = preg_grep('/^([^.])/', scandir($dirLoc));
echo '<div class="row">';
if ($dirOnly == true) {
foreach($allDirs as $dir) {
$innerDirImages = preg_grep('/^([^.])/', scandir($dirLoc.'/'.$dir));
$firstImageOfDir = reset($innerDirImages);
/* view.php?album=something -> rewritten by htaccess to albums/something*/
$href = 'album/'.$dir;
// $href = 'album/'.$dir;
$imageSrc = 'images/events/'.$dir.'/'.$firstImageOfDir;
echo albumAndPhotoTemplate($href, $imageSrc, true, $dir, 'col-xs-12 col-sm-3');
}
} else {
if (empty ($_GET['album']) ) {
return ;
}
$albumName = $_GET['album'];
print_r($albumName);
$dirLoc = $dirLoc .'/'.$albumName;
$allImages = preg_grep('/^([^.])/', scandir($dirLoc));
foreach($allImages as $image) {
$imgSrcAndHref = 'images/events/'.$albumName.'/'.$image;
echo albumAndPhotoTemplate($imgSrcAndHref, $imgSrcAndHref, false, '', 'col-xs-12, col-sm-3');
}
}
echo '</div>';
}
Try like this..
RewriteEngine On
RewriteRule ^album/([a-zA-Z0-9-=_.]+)$ view.php?album=$1 [L]
Get your variable using
$_GET['album'];
If you want to both variable and name:
print_r($_GET);
To escape & on url use %26 like this..
view.php?album=something%26basketball
See more here escaping ampersand in url
ok,Let me explain you step by step,Point is im new for this developments.
php files in my cproject directory :
allproducts.php
watch.php
in allproducts.php im displaying all the products available in the database. and every products has it own url. In my watch.php im displaying certain data according to a parameter comes from the allproducts.php page.
in my allproducts.php page i have this url :
http://localhost/cproject/watch?v=
url looks like something like this :
localhost/cproject/watch?v=J46TKlqSw3Gt4sk
at the moment i wrote a rewriterule for make watch.php in to "watch"
now what i want is to get rid of this "?" mark in to "/" and "v= " into "/"
so i want my rul to looks like this
localhost/cproject/watch/J46TKlqSw3Gt4sk
i hope now u do understand what i want to do actually ?
if i suddenly explain you what i have on my .htaccess file for this watch.php
i have this line of code
RewriteRule ^watch watch.php [NC,L]
You have to use .htaccess for doing url this.
.htaccess file code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
For PHP version less than 5.2.6 try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
If you need to get the parameter from the url then use:
javascript
var str = "http://localhost/cproject/watch?v=J46TKlqSw3Gt4sk";
var param = str.split("=")[1];
alert(param);
Result
J46TKlqSw3Gt4sk
With the new url
var str = "http://localhost/cproject/J46TKlqSw3Gt4sk";
var param = str.split("/");
alert(param[param.length - 1]);
Result
J46TKlqSw3Gt4sk
For the .htaccess file you can use this RewriteRule which will forward the urls with just a / to the url with /watch?v=.
RewriteRule ^cproject/(.*) cproject/watch?v=$1 [NC]
For example:
Start URL http://localhost/cproject/eeeeeeeaf
End URL http://localhost/cproject/watch?v=eeeeeeeaf
But what i want is to get a url like i have mentioned above and get the parameter in a different page
You can do this with PHP from the /watch file by using the REQUEST_URI element in the $_SERVER[] array.
For the PHP code you could do:
<?php
$request = $_SERVER['REQUEST_URI'];
$exploded = explode("/", $request);
$id = $exploded[4];
echo $id;
?>
In regards to your additions to your question, it is easy to do as I have put above. Instead of what I have put, you can instead just use:
RewriteRule ^watch/(.*) watch.php?v=$1 [NC,L]
You can add this rule alongside your current rule (preferably after) and it should function as you require.
now what i want is to get rid of this "?" mark in to "/" and "v= " into "/" so i want my rul to looks like this localhost/cproject/watch/J46TKlqSw3Gt4sk
The URL RewriteRule I provided will do this :)
Have you actually tested the rewrites you have been provided with?
I am trying to create a simple redirect to another page on login script.
Currently I am rewriting my URLS to the following format using .htaccess:
RewriteRule ^login$ ?i=l [L]
RewriteRule ^login([^/]*)$ ?i=l&=$1
The normal URL is therefore: domain.com/login - although I wish to be able to do the following: domain.com/login&redirect=/account/settings (Where the "redirect" will be the $_GET parameter that I'll be redirecting to after successful login)
My problem is if I access the above URL I get a 404 page not found. What am I doing wrong?
Try these rules:
RewriteRule ^login/?$ ?i=l [L,QSA]
RewriteRule ^login(&[^/]+)/?$ ?i=l$1 [L,NC]
Though I suggest using:
domain.com/login?redirect=/account/settings
and get rid of 2nd rule altogether.
QSA flag in first rule will add redirect=/account/settings query parameter as $_GET to your php file.
I use a .htaccess.
It's more functional and simple. I do not know if that's what you need but the handling is very simple.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?param=$1 [L,NC]
Url Example :
domain.com/login/account/settings
The treatment is in the script PHP
$param = $_GET['param'];
echo $param;
Return
login/account/settings
Or then
$param = $_SERVER['REQUEST_URI'];
// (PHP 5 >= 5.2.0)
// $param = filter_input(INPUT_SERVER, 'RESQUEST_URI');
$param = explode('/',$param);
print_r($param);
Return
Array( [0]=> [1]=>login [2]=>account [3]=>settings)
With this method it is possible to accomplish several goals.
I have been trying to do a htaccess URL rewrite (R=302) for a website which uses a $_get function to pull the content pages by use of strings. For instance, I am trying to change the following:
http://www.site.com/index.php?page=about
Into this:
http://www.site.com/about/
However, I have managed to get it set up to the point where it will redirect the header and footer data within the index.php file if I use query the second URL, but it will not grab the CSS or the $_get information for the content. Here are the htaccess entries and the $_get information:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*?)/$ /index.php?page=$1 [NC,QSA,L]
RewriteRule ^(.*?)/$ $1.html [NC,L,QSA,R=302]
<?php
$folder = '';
$ext = 'html';
if (!$_GET['page'] || strpos($_GET['page'], '/') === true)
$path = 'home.' . $ext;
else $path = $_GET['page'] . '.' . $ext;
if (!file_exists($path)) {
$messages = array(
'404: Page does not exist; Please check the URL you entered.',
'404: Error 404, please check the URL of the page.'
);
print $messages[ rand(0, (count($messages) - 1)) ];
}
elseif (!#include($path)) print '300: couldn\'t get the page';
?>
Any help would be appreciated. I have been trying to modify the php code unsuccessfully thinking that is the issue.
First off, your two rewrite rules have the same condition, so only the first one will ever be met. I think you probably only want to rewrite if the requested uri doesn't exist on the server.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ /index.php?x=$1 [L]
I've currently got a web application that I need optimizing, and one of methods or something I'm trying to achieve is such:
http://myweb/dept/app
from
http://myweb/?dept=dept&app=app
I've currently this as the PHP code for it:
if(empty($_REQUEST['dept'])) {
$folder = "apps/";
} else {
$folder = str_replace("/", "", $_REQUEST['dept']) . "/"; }
if(empty($_REQUEST['n'])) {
include('user/home.php');
} else {
$app = $_REQUEST['n'];
if(file_exists($folder.$app.".php")) {
include($folder.$app.".php");
} else {
include("global/error/404.php");
}
}
How do I do this?
I'm currently half there with:
RewriteRule ^([A-Za-z]+)$ /index.php?app=$1
but that only rewrites part of it.
Thanks
The way many frameworks do this is with one of the following rules:
RewriteRule ^(.*)$ /index.php?q=$1
RewriteRule ^(.*)$ /index.php
In the 1st case you get the query string in $_GET["q"].
In the 2nd case you have to get the query string from $_REQUEST or something. (just do some var_dumps till you find what you need).
Then you explode("/") this and you're all set.
Have a look at how TYPO3, eZPublish, Drupal do this.
You should also add the following conditions to allow the site to open your static files (like images/css/js/etc). They tell apache to not do the rewrite if the URL points to a location that actually matches a file, directoy or symlink. (You must do this before the RewriteRule directive)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
This should work:
RewriteRule ^([A-Za-z]+)/([A-Za-z]+)$ index.php?dept=$1&app=$2 [QSA]
You need the QSA part in order for any GET parameters to be appended to the rewritten URL.
You might find that it can be more flexible to rewrite everything to index.php, and then handle splitting up the url there, e.g.
.htaccess:
#only rewrite paths that don't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
PHP:
<?php
$parts = explode('/', $_SERVER['PATH_INFO']);
$dept = isset($parts[0]) ? $parts[0] : 'someDefault';
$app = isset($parts[1]) ? $parts[1] : 'anotherDefault';