When I click on a comment section for a given entry on a site I have, the URL looks like this:
http://www...com/.../comments/index.php?submission=Portugal%20Crushes%20North%20Korea&submissionid=62&url=nytimes.com/2010/06/22/sports/soccer/22portugalgame.html?hpw&countcomments=3&submittor=johnjohn12&submissiondate=2010-06-21%2019:00:07&dispurl=nytimes.com
I want to make it look like this URL:
http://www...com/.../comments/Portugal-Crushes-North-Korea-62
I understand that this involves adding rules to the .htaccess file. I have two questions:
Since I am using the GET method in PHP, the ugly URL has a bunch of variables appended to it. I don't want all of these variables to appear in the clean URL. Is it possible to only include a few of the variables in the clean URL but still have a rule directing it to an ugly URL with all of the variables?
Once I have the .htaccess rules written, do I go back and change the links in the source code to direct to the clean URLs? If so, how do I do this using the GET method when the clean URL does not have all of the variables that I want to pass along?
Thanks in advance,
John
I'm not sure why you need all that data in the URL. You should be storing things like the submission title, its date and author in a database and then refer to it with an ID. That way, your URLs will be shorter and prettier:
http://www.example.org/article.php?id=1
http://www.example.org/article/1/
You can accomplish this with a simple RewriteRule in your .htaccess file, like so:
RewriteEngine On
RewriteRule ^articles/([0-9]+)/ article.php?id=$1
No, you can not leave variables out and expect them to be passed anyway. If you do this, the information is no longer in the URL, so you don't have a way to get it.
You can use post instead of get if you want to pass variables without them showing up in the URL.
I join the word of Sjoerd, but there are a lot of ways how you can rewrite your url like you want to!
Apache and (IIS too) supports url-s like this one: http://example.com/index.php/my-rewritten-url_62
function URISegment($segment)
{
$uri_array = explode('/',$_SERVER['REQUEST_URI']);
$uri_count = count($uri_array);
$returning_uri = array();
for($i = 0;$i<$uri_count;$i++)
{
if(empty($uri_array[$i]) || $uri_array[$i] == "index.php")
unset($uri_array[$i]);
else
array_push($returning_uri,$uri_array[$i]);
}
if($segment < count($returning_uri))
return $returning_uri[$segment];
else
return false;
}
This works, but you need to define the base url too, and this needs to be called at the beginning of the file, and implemented at every image, script, etc. call.
function BaseURL()
{
if(isset($_SERVER['HTTP_HOST']))
{
$base = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
$base .= '://'. $_SERVER['HTTP_HOST'];
$base .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
}
else
{
$base = 'http://localhost/';
}
return $base;
}
After this you can use instead of this:
// http://example.com/?MyKey=Some-data
$MyKey = $_GET['MyKey']; //which is the first item
echo $MyKey;
// results: Some-data
This:
// http://example.com/?MyKey=Some-data
$MyKey = URISegment(0);
echo $MyKey;
// results: Some-data
You've got the same result by each one.
PS:
I like this solution because I can mix url types as I need them like:
example.com/index.php/index/evaled-article?some=db-stored&code=snipplet
And of course you can rewrite your url like FRKT said :)
And of course, if you want to hide the index.php you need to use mod_rewrite, because there's no way
Related
how can i take the link of the page that i am on with its variables ?
example i have the page link is
article.php?article_id=10&article_title=title&lang=ar
when i use the $_SERVER['SCRIPT_NAME'] variable it takes only article.php
and im rewriting the url as well so it looks like this
article/10/title/ar
what i want to do is just make a link that is to an English page so im trying to make it look like this
article/10/title/en
how can i do that?
Since the data looks like its passed with a HTTP GET method, you can use this
$_GET["lang"];
This returns the value assigned to "lang"
$_SERVER['QUERY_STRING'] will have all the parameters. You can also check $_SERVER['REQUEST_URI'] and that should contain the whole url, file and parameters.
Something like:
$params = $_GET;
$params['lang'] = 'en';
$link = basename($_SERVER['SCRIPT_NAME']) . implode('/', $params);
I have a bit of an odd question, I'm sure most of you realise that these sorts of questions arise out of certain situations that a developer has no control over!
I would like to work out how to keep a querystring parameter in the URL at all times. If the parameter is not set, I'd like a default to be appended to the URL ?param=something
I asked a previous question relating to this and have been able to use htaccess to add a default query - but this only works fir the initial request whereas I need to ensure it is always present in the address.
I am thinking of using a cookie - set with PHP and then queried with .htaccess.
So, I am asking if this is possible and if there is a better way of doing this?
Without changing all the URLs I can't see this being possible and as discussed it is a very hacky way to do it.
I would suggest you give them a "special" button that they use for copying a link. Make them use this button to copy the link instead of the URL. You can then control the data properly without hacking the website.
Edit: You "could" add some JQuery in to append ALL links with your param. Have you thought about this?
$(document).ready(function() {
$('a[href]').each(function() {
this.href = this.href + '?something=<?php echo $_SESSION["myparam"]; ?>'
});
});
You could get the param from the initial page load / session but this would require your users to have JS enabled (Which most people do now).
p.s Untested semi-pseudo code. I can test it properly if you decide to go down this route.
$_GET and $_POST superglobal variable those are not meant to used like this. And I don't know why is it required but anyway you can get the required functionality using Session or Cookies. What you are trying to do is not practical. Industry standard is to use session and cookie. Try them out I'm sure it will helps you out for sure. If you have any issues let me know.
If you are using Apache 2, then you could add a rewrite rule that appends a GET parameter to each request that doesn't have "param=..." in its URL. Something like (untested):
RewriteCond %{QUERY_STRING} !(\A|&)param=
RewriteRule (.*) $1?param=defaultvalue [QSA]
See modrewrite 's doc for details. You can add another RewriteCond on %{REQUEST_URI} if you want to limit this to some URL.
I only learned php about 3 months ago so I hope I am not leading you down a path, this is what I would try.
Edit: It has been about 5 months now since I learned php. My Previous version before this edit only "built" the get to append (and it probably didn't work).
if (!isset($_GET['param'])) {$_GET['param'] = 'something';}
$var = '';
while ($other_gets = $_GET)
{
foreach ($other_gets as $key=>$value)
{
if ($key = 'something') {}//do nothing we want the other gets
else {$var .= '&'.$key.'='.$value;}
}
}
$get = '?'.$user_param.$other_gets;
preg_replace your extensions
$string = 'your webpage before output';
$patterns = array();
$patterns[0] = '/.html/';
$patterns[1] = '/.php/';
$patterns[2] = '/yoursite.com /';//with space
$replacements = array();
$replacements[0] = '.html'.$get;
$replacements[1] = '.php'.$get;
$replacements[2] = 'yoursite.com/index.php'.$get;
Then print your string
echo preg_replace($patterns, $replacements, $string);
I am validating a form on a page and I need to redirect back to the same page with the same url query and a additional &valid=false at the end of the query when redirected.
How would you do this so that the url is not repeated on redirect:
http://test.com/index.php?a=1&b=2&valid=false&a=1&b=2&valid=false
I am using $_SERVER['REQUEST_URI']
There are many ways to do it, here is a simple one:
$url = $_SERVER['REQUEST_URI'];
if (!isset($_GET['valid'])) {
$url.='&valid=false'; //assuming there are always additional parameters, otherwise you must check whether to use '?' or '&'
}
But you can also use parse_url(), strpos(), regular expressions etc
You can use of http_build_query() after modifying parameters:
<?php
$_GET['a'] = 1;
$_GET['b'] = 2;
$_GET['valid'] = 'true';
$url = $_SERVER['SCRIPT_NAME'].'?'.http_build_query($_GET);
?>
Like that, if $_GET['valid'] is not defined then it will be created, else it will be modified.
example:
I have a webpage abc.com/index.php
I want to detect if user connect to abc.com/index.php/bbdh,
abc.com/index.php/bb/saggfd/gjasgd/hsahgd, abc.com/index.php/bb/saggfd/gjas and so on without having a page like that on my host.
I'm using php and need some way to do that without using .htaccess file.
Thanks for any help!
have a look at $_SERVER['REQUEST_URI'], every thing you need will be accessible here. E.g. for:
abc.com/index.php/bb/saggfd/gjas
it would be:
/index.php/bb/saggfd/gjas
strip the /index.php with something like:
echo substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
and you're set.
Update on how to handle GET-parameters:
// get the full request uri
$requestUri = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
// let php parse that url (though it's just a part of the full url)
$chunks = parse_url($requestUri);
// let php parse the query string
parse_str(isset($chunks['query']) ? $chunks['query'] : '', $chunks['query']);
// debug output
print_r($chunks);
by having an URL like this: mysite.com/subfolder/helloworld - Is it possible to read the "helloworld" from within a PHP page?
I would like to use the string as a part to load some content.
end( explode( '/', $_SERVER['REQUEST_URI'] ) )
without the end() call it will give you all the parts of the URL
You should read about URL rewriting.
This is more clean than "reading" the current URL. Basically you can redirect your URL (transparently) to something like :
mysite.com/index.php?folder=subfolder&category=helloworld
Then in PHP, you can access the URL parameter with :
$folder = $_GET['subfolder']
$category = $_GET['category']
This is maybe not the kind of answer you were expecting, but it can be interesting to know.
The request url (everything from the first / after the domain name) can be found in $_SERVER["REQUEST_URI"]
Hello have u used 'strstr' keyword of php?
please try like this
if(strstr($_SERVER["REQUEST_URI"], "helloworld"))
return true;
else
return false;
May be this will be helpful to you.