Htaccess Revert URL Encoding - php

Facebook, and twitter rewrite urls and replace characters(like ? or /) with code.
Original URL - http://example.com/article.php?title=hi/how-are-you?
URL after(social network redirect): http://example.com/article.php?title=hi%2Fhow-are-you%3F
I use a hosted comment system(Disqus) which uses the URL as the thread identifier, which means I will get two threads for one page because of this variation.
Is there anyway that I can revert the characters through htaccess or another method?

You could use rawurldecode.

You can get the right line back with php-function:
$_GET['title'] = urldecode($_GET['title']);
So you can pass all urls thorough some rewriter.php with this function via .htaccess.
.htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ /rewriter.php [QSA]
rewriter.php:
<?php
$_GET['title'] = urldecode($_GET['title']);
include('index.php');
?>
This method allows to use some statistics, redirects or other manipulations.

Related

Rewrite site with first slash to index

I'm sorry about the title but I can't find a good one to explain what I try to do.
I have my website : http://domaine.com .
The index.php file in it redirect to http://domaine.com/views/index.php .
Now I want to Rewrite Rule so that any url like :
http://domaine.com/something display the site http://domaine.com so that :
http://domaine.com/php redirect http://domaine.com/php/views/index.php
http://domaine.com/java redirect http://domaine.com/java/views/index.php
http://domaine.com/ruby redirect http://domaine.com/ruby/views/index.php
etc.
EDIT : I'm working on localhost so it's supposed to:
localhost/formation/php => localhost/formation/php/views/index.php
localhost/formation/java => localhost/formation/java/views/index.php
localhost/formation/ruby => localhost/formation/ruby/views/index.php
EDIT : The content of my htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ views/index.php [L,QSA]
When I access to localhost/formation/ruby/ for exemple it's working but after the loggin it look like it's always redirecting from the home page to login page to home page etc.
EDIT : I create a repertory called "ruby" and create in it a htaccess file with that content.
RewriteEngine On
RewriteRule ^(.*)$ ../$1 [L]
And it does exactly what I want. So I want to find a way to do that without having to create the repertory "ruby".
http://domaine.com/php redirect http://domaine.com/php/views/index.php
http://domaine.com/java redirect http://domaine.com/java/views/index.php
http://domaine.com/ruby redirect http://domaine.com/ruby/views/index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(php|java|ruby)/$
RewriteRule ^.*$ /%1/views/index.php
I suppose by "redirect" you mean "serve as". In case if you want a 301 redirect, use the appropriate flags for RewriteRule, e.g. [R=301,L].
Wrapper
In case if you are trying to implement a wrapper script which is supposed to handle different request URIs, you should process the requests with a single script and pass the URI path parts as query string parameters. For instance:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(formation|another_type)/(php|java|ruby)/$
RewriteRule ^.*$ /wrapper.php?type=%1&lang=%2
With this configuration, you can handle requests like http://your-site.com/formation/ruby/ with a single script. You can access the query string parameters using the $_GET superglobal variable, e.g. echo $_GET['lang'];.
Ultimately, you can process all requests with a single wrapper:
RewriteRule ^.*$ %{DOCUMENT_ROOT}/wrapper.php
Within the wrapper you will have to examine the contents of the $_SERVER superglobal. The REQUEST_URI and REQUEST_URI values are particularly useful.

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?

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