Rewrite-dynamic URL .htaccess - php

I am trying to re-write the dynamically generated URL. But the rules i have written is not affecting the URL even a single bit.
The URL currently shows up like: http://ukfurniturespecialist.co.uk/county.php?c=Oxfordshire&%20t=Faringdon
And i would like it to look like: http://ukfurniturespecialist.co.uk/county-c-Oxfordshire-t-Faringdon.html
This is what i have tried:
Options +FollowSymLinks
RewriteEngine on
RewriteRule county-c-(.*)-%20t-(.*)\.html$ county.php?c=$1&%20t=$2
Any help is much appreciated,Thanks.

For your URL following rule should work:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+county\.php\?c=([^&]*)&.*?t=([^&\s]*) [NC]
RewriteRule ^ county-c-%1-t-%2.html? [R=302,L]
RewriteRule ^county-c-([^-]+)-t-([^.]+)\.html$ county.php?c=$1&\%20t=$2 [L,QSA,NE]

%20 represents a space character in url. when writing rewrite rule the url is already decoded and handled using "\ " instead of %20
For your URL the rewrite rule should be
Options +FollowSymLinks
RewriteEngine on
RewriteRule county-c-(.*)-\ t-(.*)\.html$ county.php?c=$1&\ t=$2
I dont think you need to put space character in your url. If you remove the %20 from your URL the rewrite rule should be
RewriteRule county-c-(.*)-t-(.*)\.html$ county.php?c=$1&t=$2

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.

Rewrite htaccess foo.com/username

I am trying to have it so foo.com/userinfo.php?user="username" will be re-written as foo.com/username. I have gotten this to work using RewriteRule ^([a-zA-Z0-9_-]+)$ userinfo.php?user=$1 in my htaccess. However, when a slash is added to the end of the username such as foo.com/username/ it just redirects it to foo.com/userinfo.php?user="username". While this does pull up the profile it doesn't look as well in the url bar. Thank you for any help!
Here is my .htaccess code now
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ userinfo.php?user=$1
You can use `/?' as the expression "the character / or nothing".
RewriteRule ^([a-zA-Z0-9_-]+)/?$ userinfo.php?user=$1

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]

$_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]

Rewrite URL containing spaces

I have an url with the following format:
domain/product.php?name=product a
Now I need to rewrite this to:
domain/product a (including the space in the word) like:
http://www.directline-holidays.co.uk/Costa Blanca
How do I do this? The closest result I got so far is the following:
domain/p/product-a
With the following code in .htaccess
RewriteEngine On
RewriteRule ^p/([^/]*)/$ destination.php?name=$1
I could not even use the name without the "-". I need the product name just as it is in the database. Is this possible?
This should work: It will simply give you the rest of the URL string after the /p/ directory to the end of the string, which in your case should be the end of the URL, correct?
RewriteRule ^p/(.*)$ destination.php?name=$1
For the pages that are not product pages, if you know they will end in a .php file extension you can filter for those pages with the following rule:
RewriteCond %{REQUEST_URI} !^.*(destination\.php).*$
RewriteRule ^([^\.php]+)$ destination.php?name=$1
EDIT: Fixed for infinite loop condition by adding RewriteCond for destination.php
add "%20" to the URL, such as:
http://www.directline-holidays.co.uk/Costa%20Blanca
20 in hexadecimal base is the ASCII number for space.
Edit:
In addition to powtac's comment:
Use the JS encodeURIComponent() function to encode a value that should be used in a URL:
http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp
Try this rules..
Options +FollowSymLinks
RewriteEngine on
RewriteRule product-name-(.*)\.htm$ product.php?name=$1
or
Options +FollowSymLinks
RewriteEngine on
RewriteRule product/name/(.*)/ product.php?name=$1
RewriteRule product/name/(.*) product.php?name=$1
do you mean if destination is a php file , doesn't rewirte it , else rewrite any thing to destination.php ? if so , this should work
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ destination.php?name=$1

Categories