$_GET URL from ?url=http://google.com - php

I'm making a redirect script for my site, I use htaccess to rewrite the URL so it looks nicer.
eg. http://localhost/r/http://google.com is the URL, but when I printing the value it shows up like this http:/google.com.
One / is missing, how can I fix that?
Edit:
Rewrite rule:
RewriteRule ^r/(.*)/$ /system/offsite/redirect/index.php?url=$1 [L]
Thanks for any help :)

This behavior is due to Apache that removes empty path segments before mapping it. But you can access the original requested URI path via THE_REQUEST:
RewriteCond %{THE_REQUEST} ^GET\ /r/([^\ ]+)
RewriteRule ^r/ /system/offsite/redirect/index.php?url=%1 [L]

Use php urlencode function
EDIT:
//echo your url
echo 'http://localhost/r/'. urlencode('http://google.com');
and in your index.php file
//get your url
$url = urldecode($GET['url']);

I think REQUEST_URI variable will have correct text. Use it like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/r/(.*)$
RewriteRule ^r/ /system/offsite/redirect/index.php?url=%1 [L,QSA,NC]

Related

How to get variable in query string without using "?"

I'm trying to use GET to capture member IDs in a URL without using the ?name=variable. For instance, instead of using mydomain.com/folder?id=1234 I would like to use mydomain.com/folder/1234 and pick up the id "1234" in the code so I can pass it to another URL.
This page simply redirects by using: <iframe src="http://redirect_domain.com/folder/index.php" />
I have no control over the server I'm redirecting to, but need the variable passed to it. I can append the redirect URL with ?id=1234 ie <iframe src="http://redirect_domain.com/folder/index.php?id=1234" />
Their code will pick up the id by using <?php echo $_GET["id"]; ?>
Thanks for your help.
You'll want to use .htaccess for this. Create a file called .htaccess and put it in the same folder mydomain.com is referencing. Put something like the following in there
RewriteEngine On
RewriteRule ^folder/(.*)$ folder?id=$1 [L]
Note: mod_rewrite must be enabled on apache, but it usually is.
Update: I've updated the .htaccess to reflect a local URL since you've made your desired functionality more clear. Using this .htaccess in conjunction with <iframe src="http://redirect_domain.com/folder/index.php?id=<?=$_GET['id']?>" /> should do the trick.
Create a .htaccess file and use something like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I feel the need to write my own answer down. The following two lines in your .htaccess should do exactly what you are asking for:
RewriteEngine On
RewriteRule ^folder/(.*)$ redirect_domain.com/folder/index.php?id=$1 [L]
According to the rewrite rule test site http://martinmelin.se/rewrite-rule-tester/ , when I use an input of folder/1234, it is redirected to redirect_domain.com/folder/index.php?id=1234 which is exactly what you asked for. There is no need for the <iframe... or any actual files in the /folder/ other than the .htaccess ...

How to use htaccess code for URL rewrite in PHP?

I have a link like below:
In /country/search.php
<a href="<?php echo 'index.php?departments='.$value['department_id'].'&towns='.$value['town_id'].'&type='.$value['type_id'].'&id='.$value['id'] ?>">
When I use the below .htaccess code, it does nothing:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L]
It does in the tool:
http://example.com/0/0/4/122
... but when I click that link it shows old URL again.
What is the problem here?
I'm generating that .htaccess code using this tool: http://www.generateit.net/mod-rewrite/
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php|)\?departments=([^\s&]*)&towns=([^\s&]*)&type=([^\s&]*)&id=([^\s&]*) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=301,L]
# internal forward from pretty URL to actual URL
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L,QSA]
I think you misunderstood what the rewrite rules are trying to achieve in this context.
F.e. this rewrite rule
RewriteRule ^([^/]*)/([^/]*)$ /app.php?method=$1&arg=$2
Will ensure that a request to http://example.com/mymethod/myarg will be rewritten to
http://example.com/app.php?method=mymethod&arg=myarg
When you generate links from within your application you should know how you contstructed the rewrite rules and you have to build the link accordingly.
Original URL:
href="news.php?state="<?php echo $sql_res['state']?>"&country="<?php echo $sql_res['country']?>
Ideal URL:
/India/andhra%20Pradesh
And .htaccess code:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /news.php?country=$1&state=$2 [L]

.htaccess file not redirecting - guidance wanted

I have this url:
details
the url becomes in addressbar:
http://web228.sydney.webhoster.ag/soputnik/drive_to_london/?procid=12
but I want to process the logic in details.php.
How can I process the data in details.php in background and keep the first url in the address?
I tried this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^drive_to_(.*)$ http://web228.sydney.webhoster.ag/soputnik/details.php [R=301,L]
but it is not working, error is: NOT FOUND
please help
Substitute your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^test/drive_to_(.*?)/?$ /test/details.php [L,NC]
If you don't want the URL to change, then it's not a redirect, just a rewrite.
Change:
RewriteRule ^/test/drive_to_(.*)$ /test/details.php [R=301,L]
To something like:
RewriteRule ^/test/drive_to_(.*)$ /test/details.php?city=$1 [L]
dont forget to pass any url params to details.php.
Rewriterule ^/drive_for_(.*)/\?(.*)$ http://domain.de/test/details.php/?$1 [R=301,L]
it seems relevant that you want to pass the city name as well, otherwise that context gets lost in the rewrite. If that is another url param you could pass it through to details.php as such
Rewriterule ^/drive_for_(.*)/\?(.*)$ http://domain.de/test/details.php/?$2&city=$1 [R=301,L]

mod rewrite, htaccess and gettign the values

the incoming URL
domain.com/12/3
should be re-written to
domain.com/?w=12&h=3
htaccess
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?w=$1&h=$2 [QSA]
the php
<?php
echo $_GET['h'];
?>
Result
404 page
I've tried using htaccess to change the result of the url and then retrieve the value from the URL, could someone help me out?
Maybe you need to escape - like:
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_\-]+)$ index.php?w=$1&h=$2 [QSA]
PS. I hope the above URL /12/3 is just for example because your regex accepts only a-z
RewriteEngine On
RewriteRule ^[a-z]{2,2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Essentially, I'm pretty sure you need a / in front of index.php
I like to use this generator for my mod-rewrite rules.
http://www.generateit.net/mod-rewrite/
Try changing your rewriterule to the following:
RewriteEngine on
RewriteRule ^([a-z0-9]{2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Your example has two digits as the first parts of the path, which won't be matched by [a-z].
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?w=$1&h=$2 [QSA]
The above sorted it. Thanks for the replies

How to get URL string into PHP variable

Let's start with an example:
So requested URL would be: www.domain.com/product/hdd/samsung
I want to have a rewrite rule to get index.php and save everything after www.domain.com into php variable. So, substituted page would be for example www.domain.com/index.php?query=product/hdd/samsung. But I'am unable to write correct rule.
I tried this:
RewriteRule ^/(.*)$ index.php?query=$1 [NC,L]
The code above isn't working. I want to use the query in PHP code in the index.php:
if(isset($_GET["query"] )) {
$query=$_GET["query"];
}
I find out, there is possibility to get $query via $_SERVER['QUERY_STRING'] instead of mod_rewrite, am I right?
For my PHP application is variable $query critical, because it's used for loading templates.
Questions:
What solution would you recomand me? To use $_SERVER variable or mod_rewrite?
If mod_rewrite, how to write correct RewriteRule?
Thank you
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?query=$1 [NC,L]
If your site is not in your server's root dir, change RewriteBase to /some/subdir/.

Categories