How to use htaccess code for URL rewrite in PHP? - 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]

Related

Using Mod Rewrite to change URL structure

I want to use mod rewrite via htaccess for a PHP website.
URL structure is the following:
example.com/ex.php?ez=DF004AE
It must become:
example.com/ex/DF004AE.htm
What is the correct script to add to my .htaccess in order to do that?
Use this:
RewriteEngine On
RewriteRule ^ex/([^/]*)\.htm$ /ex.php?ez=$1 [L]
It will give you the following URL:
example.com/ex/DF004AE.htm
If you meant it to be .html (not .htm) Just add the l in the RewriteRule.
You can use the following rules :
RewriteEngine on
# Redirect from "/ex.php?ez=foobar" to "/ex/foobar.htm"
RewriteCond %{THE_REQUEST} /ex\.php\?ez=([^\s]+) [NC]
RewriteRule ^ /ex/%1.htm? [L,R]
####################
# internally map "/ex/foobar.htm" to "/ex.php?ez=foobar"
RewriteRule ^ex/([^.]+)\.htm$ /ex.php?ez=$1 [NC,L]
This will convert /ex.php?ez=foobar into /ex/foobar.htm .
RewriteEngine On
RewriteRule ^ex/([^/]*)\.html$ /ex.php?ez=$1 [R=301,NE,L]
If you don't want the address bar to reflect this change, remove R=301. The NE parameter is there to make sure that the request is not escaped.

How to Clean URL using .htaccess

I am very frustrated looking for working clean URL using .htaccess:
My .htaccess code:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ $1.php [L,QSA]
RewriteRule ^/([0-9]+)$ product.php?id=$1
The first rule is percfectly working in hiding the .php extension of my URL but the second rule is not working. The URL of my product after clicking the product ID no on the product list. I'm using free host for now. freehost.16mb.com/product?id=156
My Code of my product list link is:
link 1 array
So I'll try to clean the URL using .htaccess into freehost.16mb.com/product/156.
I am using the .htaccess code above but it doesn't work for me. I need some help.
Your second rule does not make the provision for the leading /product before the ID, and your HTML anchor is using the format that you are rewriting to - this needs to be swapped around.
Change your anchor tag to the following:
<a href="/<?php echo $row['ID']; ?>
You also need to fix your rule up a bit by adding the QSA and L flags, and by removing the leading slash from the rule's pattern:
RewriteRule ^([0-9]+)$ product.php?id=$1 [QSA,L]
Once you've done that, /1234 will rewrite to /product.php?id=1234.
However, if you wish for the /product segment to be included, then use these instead:
<a href="/product/<?php echo $row['ID']; ?>
RewriteRule ^product/([0-9]+)$ product.php?id=$1 [QSA,L]
Just Use this in your .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule product/id/(.*)/ product?id=$1
RewriteRule product/id/(.*) product?id=$1
This will turn your URL into:
freehost.16mb.com/product/id/156
To have freehost.16mb.com/product/156 just remove the id/ from the RewriteRule, so it will be:
Options +FollowSymLinks
RewriteEngine on
RewriteRule product/(.*)/ product?id=$1
RewriteRule product/(.*) product?id=$1
---- ALTERNATIVE ----
RewriteCond %{QUERY_STRING} (^|&)id=156($|&)
RewriteRule ^freehost\.16mb\.com/product$ /freehost.16mb.com/product/156?&%{QUERY_STRING}

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

Using Mod Rewrite to rewrite a url?

I have the following format:
http://www.mydomain.com/view/product/ID/CAT/TITLE
ID and CAT are both numeric. So, some real examples:
http://www.mydomain.com/view/product/1/2/ipod
http://www.mydomain.com/view/product/3/4/40-inch-tv
etc
I want to know if I should try to use mod rewrite, and how, or if I should rather fix the PHP to handle the paths correctly. I want to map the above to something like:
http://www.mydomain.com/CATEGORY/SUB_CATEGORY/TITLE
Any ideas?
RewriteEngine on
RewriteBase /
RewriteRule ^([A-Za-z0-9]+)$ /$1/ [R]
RewriteRule ^([A-Za-z0-9]+)$ index.php?cat=$1
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$2/ [R]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?cat=$1&subcat=$2
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$2/$3/ [R]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?cat=$1&subcat=$2&title=$3
in php you can get the variables as you would normally do, also you will need a rule for all variables entered and/or only category or sub-category or you will end up with a 404 when someone tries to access those pages
I would say that your question is not totally clear to me. Going by your examples what do you want your target URL to become from http://www.mydomain.com/view/product/1/2/ipod.
Will it be: http://www.mydomain.com/2/1/ipod ?
If yes then enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^view/product/([0-9]+)/([0-9]+)/(.*)$ $2/$1/$3 [L,R,NC]

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

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]

Categories