Variables as www.example.com/var1/var2/ [duplicate] - php

This question already has answers here:
URL rewriting with PHP
(5 answers)
Closed 8 years ago.
I want to make something like:
www.example.com/var1/var2/
where I can take the value of the variables, but it also need to be compatible with GET like:
www.example.com/var1/var2/?feat=1234
How can I do this?
Thanks

<?php
$url = $_SERVER['REQUEST_URI'];
$url_splitter = explode('/', $url);
echo '<pre>';
print_r($url_splitter);
?>
Now you can access the variables by taking $url_splitter[0], $url_splitter[1],....

Check out symfony or codeigniter
The documentation will explain how to do a mod rewrite to get the url you want.
A partial re write can be done in this way (have not tested)
http://forums.devshed.com/apache-development-15/partial-mod-rewrite-454669.html
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?var1=$1&var2=$2 [QSA,L]
That should turn a url like /foo/bar/?var3=baz into /index.php?var1=foo&var2=bar&var3=baz I am not sure it'll work though because I don't know how the regexp in the mod rewrite works.

Related

What is URL Morphing? [duplicate]

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 6 years ago.
I want to pass a variables from one php file to other php file by GET method only without showing in URL.
for example: http://www.mysite/test.php?user_id=1
Instead of this just need URL should be like http://www.mysite/test
How it can be possible?
Note: I am aware about $_SESSION, $_POST but all of them are also will show URL like test.php, that I do not need actually..
This should solve your purpose:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=1$
RewriteRule (.*) $1? [R=permanent]
Use this for further reference: remove query string from end of url URL using .htaccess

How to make simple url rewriting [duplicate]

This question already has answers here:
URL rewriting with PHP
(5 answers)
Closed 8 years ago.
I'd like to know how to make a url rewriting?
Basically, if I go to www.site.com/announce/123456 I'd like to actually use the page www.site.com/annouce.php?id=123456
I can't get any simple documentation about it.
Thanks
Based on Gecko's answer I made a .htaccess which works fine :
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^announce/([0-9]+)$ announce.php?id=$1 [L]
You must use mod_rewrite into .htaccess file like this :
RewriteEngine On
RewriteRule ^/announce/([0-9]+)$ announce.php?id=$1 [QSA]
[QSA] flag allow you to pass other arguments in your url, like this :
www.site.com/announce/123456?arg2=test
Have fun :)

How to rewrite URL with hash in it? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can PHP read the hash portion of the URL?
I use hash in my url like this,
http://mysite.com/home/#/page/manage/
I want to get /page/manage/ via $_REQUEST so I try url rewrite mod like this below,
RewriteRule ^home/#/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ index.php?url=home&system_url=$1&system_name=$2 [L,QSA]
system_url=$1&system_name=$2 are not sent to $_REQUEST of course.
How can I get the result below via $_REQUEST?
array([url]=>'home',[system_url] => 'page',[system_name] => 'manage')
The fragment part of a URL (the part after #) is not actually sent to the webserver.
Thus, it is not possible to use rewrite to change it

PHP - Efficient way to check if subdomain exists [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP function to get the subdomain of a URL
Im looking for an efficient way to tell if a subdomain exists within a URL.
The domain name will always be fixed e.g. mydomin.com, however if a subdomain is not set, I need to redirect.
One thought I had is that if there is more than one period (.), a subdomain is set, but im not sure how to implement this.
Many thanks.
EDIT: Sorry for not being too clear. I do not have a current solution, but looking at other posts I can see several examples of how to get the subdomain e.g.
array_shift(explode(".",$_SERVER['HTTP_HOST']));
But i do not want the subdomain, just to check if one exists. The subdomain could be anything.
You can get the hostname with parse_url. Then you break it down with explode():
function hasSubdomain($url) {
$parsed = parse_url($url);
$exploded = explode('.', $parsed["host"]);
return (count($exploded) > 2);
}
On google you can find really easily how to redirect someone.
I would rather use apache rewrite engine (or any webserver rewrite process) :
# .htaccess file
RewriteCond %{HTTP_HOST} ^yourdomin\.com$
RewriteRule (.*) http://sub.yourdomin.com/$1 [R=301,L]

PHP global variable doesn't work when using mod_rewrite [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
global variables in php not working as expected
I have a php function that runs on every page of a website, it use a global variable, for example:
$var = "test";
function test() {
global $var;
echo $var;
}
This works fine when accessing /anyFile.php directly, but the website uses an htaccess file to rewrite urls, something like:
RewriteRule ^action/(.*)$ /index.php?action=$1 [L]
When an url is rewrited by the htaccess, the function doesn't work, the $var is not set.
What could be this happening and how can I fix it? (I NEED to use "global", otherwise I'd need to recode a lot of things.
You need to use [QSA,L] instead of [L]:
RewriteRule ^action/(.*)$ /index.php?action=$1 [QSA,L]
QSA stands for Query String Append, and forwards the query string (the part after the ? in the url) to the PHP script.
By the way, you should not use register_globals (deprecated as of PHP 5.3 and removed as of PHP 5.4), but use the $_GET superglobal instead.
--- edit ---
Following your comment below (you can't modify the .htaccess), you're out of luck. Your only solution is to parse the query string in the request URI, and use that as you would use the $_GET superglobal:
$queryString = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
parse_str($queryString, $query);
echo $query['action'];
I strongly advise you to get the .htaccess modified for you, though.

Categories