Ignore last string in URL after the last slash - php

for example: when a user types in www.website.com/site/blue, I want them to go to www.website.com/site/index.php in the web browser. The reason for this is that I want to use php to grab blue from the URL and place it in www.website.com/site/index.php
So when someone types in www.website.com/site/blue ... they should see "Hello blue!!!" in www.website.com/site/index.php

It sounds like you need to use an htaccess file and Apache's mod_rewrite. If you are allowed to do this on your server, make a file called .htaccess in the site directory.
RewriteEngine on
RewriteRule ^(.*)$ index.php?color=$1
That will rewrite every request as a query string to index.php for you to parse.

Basically, you can split the page URL at the "/" and it will give you an array of parts of the URL. The last of these parts will be "blue" in your example. Try something like the following:
var parts = document.URL.split("/");
var foo = parts.pop();
var url = parts.join("/") + "/index.php";
window.location.href = url;
And then the variable foo is the variable "blue" that you're looking for.

try this htaccess config:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Related

Secure url parameter

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?

Pretty URL via htaccess using any number of parameters

Actually i have this URL:
http://www.example.com/index.php?site=contact&param1=value1&param2=value2&param3=value3
But i want to have this URL format:
http://www.example.com/contact/param1:value1/param2:value2/param3:value3
So the "contact" goes to variable $_GET["site"] and rest of parameters should be able to access via $_GET["param1"], $_GET["param2"] etc. The problem is, it has to work with any number of parameters (there could be param4 or even param50 or any other name of parameter). Is it possible via htaccess to cover all these cases?
Mod_rewrite has a maximum of 10 variables it can send:
RewriteRule backreferences:
These are backreferences of the form $N (0 <= N <= 9), which provide access to the grouped parts (in parentheses) of the pattern, from the RewriteRule which is subject to the current set of RewriteCond conditions.
mod_rewrite manual
so what you desire is NOT possible with htaccess only. a common way is to rewrite everything to one file and let that file determine what to do in a way like:
.htaccess
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,NC]
index.php
$aUrlArray = explode('/',str_ireplace(',','/',$_SERVER['REQUEST_URI'])); // explode every part of url
foreach($aUrlArray as $sUrlPart){
$aUrlPart = explode(':',$sUrlPart); //explode on :
if (count($aUrlPart) == 2){ //if not 2 records, then it's not param:value
echo '<br/>paramname:' .$aUrlPart[0];
echo '<br/>paramvalue' .$aUrlPArt[1];
} else {
echo '<br/>'.$sUrlPart;
}
}
Garytje's answer is almost correct.
Actually, you can achieve what you want with htaccess only, even if this is not something commonly used for that purpose.
Indeed, it would be more natural to delegate the logic to a script. But if you really want to do it with mod_rewrite, there are a lot of techniques to simulate the same behaviour. For instance, here is an example of workaround:
# Extract a pair "key:value" and append it to the query string
RewriteRule ^contact/([^:]+):([^/]+)/?(.*)$ /contact/$3?$1=$2 [L,QSA]
# We're done: rewrite to index.php
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^contact/$ /index.php?site=contact [L,QSA]
From your initial example, /contact/param1:value1/param2:value2/param3:value3 will first be rewritten to /contact/param2:value2/param3:value3?param1=value1. Then, mod_rewrite will match it again and rewrite it to /contact/param3:value3?param1=value1&param2=value2. And so on, until no pair key:value is found after /contact/. Finally, it is rewritten to /index.php?site=contact&param1=value1&param2=value2&param3=value3.
This technique allows you to have a number of parameters greater than 9 without being limited by mod_rewrite. You can see it as a loop reading the url step by step. But, again, this is maybe not the best idea to use htaccess only for that purpose.
This is entirely doable using some creative htaccess and PHP. Effectively what you are doing here is telling Apache to direct all page requests to index.php if they are not for a real file or directory on the server...
## No directory listings
IndexIgnore *
## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes
## Mod_rewrite in use.
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
After this all you need to do is go into PHP and access the full user requested URL structure using the $_SERVER['REQUEST_URI'] superglobal and then break it down into an array using explode("/", $_SERVER['REQUEST_URI']).
I currently use this on a number of my sites with all of the sites being served by index.php but with url structures such as...
http://www.domain.com/forums/11824-some-topic-name/reply
which is then processed by the explode command to appear in an array as...
0=>"forums", 1=>"11824-some-topic-name",2=>"reply"
Try this..
.htaccesss
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L,QSA]
index.php
$uri = $_SERVER['REQUEST_URI'];
$uri_array = explode( "/", $uri );
switch ( $uri_array[0] ) {
case '':
/* serve index page */
break;
case 'contact':
// Code
break;
}
This is doable using only htaccess with something along the lines of...
([a-zA-Z0-9]+):{1}([a-zA-Z0-9]+)
([a-zA-Z0-9]+) will match alpha-numeric strings.
:{1} will match 1 colon.
Expanding from there will probably be required based on weird URLs that turn up.

Constructing a .htaccess file to hide a string in certain cases

I'd like to be able to hide a $_REQUEST variable name in a visitor's address bar, and also automatically append the variable name to all data requests, but only when the .php extension is not included.
Currently requests look like this:
example.com/?page=request
I'd like them to look like this
example.com/request
The problem is, domains like this still need to work:
example.com/mail.php
So I figure I'd like all requests to files that don't end in the extension .php to invisibly forward to the contents of
example.com/?page=*
While actually displaying the address:
example.com/*
Here's what I have so far:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=-
RewriteRule ^(.*)$ /$1? [L,R]
But this doesn't even replace the string when it's entered.
I wouldn't mind actually having to add the name of each accessible .php file to the .htaccess file, this would probably build on security.
You probably want to forward /request URI to /?page=request. If thats the case use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^].]+)$ /?page=$1? [L,QSA]

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...

Categories