Rewrite URL containing spaces - php

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

Related

Rewrite-dynamic URL .htaccess

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

Can't get mod_rewrite to work

I am trying to get url from:
192.168.0.1/movie-page.php?id=123
to:
192.168.0.1/movie/movie-name
or even (for now):
192.168.0.1/movie/123
I've simplified it by using this url (to get something working):
192.168.0.1/pet_care_info_07_07_2008.php TO 192.168.0.1/pet-care/
my .htaccess file:
RewriteEngine On
RewriteRule ^pet-care/?$ pet_care_info_07_07_2008.php [NC,L]
What am I doing wrong? I've tried many combinations but no luck and my patience is running out...
I am running this on my local NAS which should have mod_rewrite enabled by default. I have tested .htaccess by entering random string in .htaccess file and opening the page, I got 404 error. I assume this means that .htaccess is being used since the page stops functioning if the file is malformed.
If you want to rewrite:
192.168.0.1/movie-page.php?id=123 too
192.168.0.1/movie/movie-name or 192.168.0.1/movie/123
Then you would do something like, but will require you manually add a rewrite for any new route (fancy url) you want, and eventually you may want your script to create routes dynamically or have a single entry point to sanitize:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^movie/([a-zA-Z0-9-]+)$ movie-page.php?id=$1 [L]
So a better method is to route everything through the rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
Then handle the route by splitting the $_GET['route'] with explode()
<?php
//192.168.0.1/movie/movie-name
$route = (isset($_GET['route'])?explode('/',$_GET['route']):null);
if(!empty($route)){
//example
$route[0]; //movie
$route[1]; //movie-name
}
?>
You want something like this:
RewriteRule ^movie/([0-9]*)$ /movie-page.php?id=$1 [L,R=301]
That will give the movie ID version with a numeric ID.

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

.htaccess redirect or PHP change

The main navigation of my site is coded like this:
<li>'.$value.'</li>'."\n";
But I would like the URL of the links to look like domain.co.nz/pagename, not domain.co.nz/index.php?pageId=pagename
How would you recommend I accomplish this?
Something like this should work:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?pageID=$1
First line turns on mod_rewrite. Second line sets the base URL to / (it's annoying, but you have to set it to the base path you're dealing with). Third and fourth lines make sure the request doesn't exist as a file, or as a directory. And the last line is the actual magic; basically it searches for "anything", captures what it finds in $1, and "rewrites" the URL to index.php?pageID=$1. If you learn to use regexes, you can do much more complicated things as well.
Yes, you can accomplish this with a .htaccess RewriteRule. In your .htaccess file, include:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?pageId=$1
This means:
If the REQUEST_FILENAME is not a valid file, redirect the entire URL to index.php?pageId=the entire URL
You'd then change your navigation to:
echo "<li>'.$value.'</li>'."\n";
Edit: I moved Trivikrtam's edit inline, see above. The RewriteRule should be index.php?pageId=$1 not /index.php?pageId=$1. Thanks #Trivikrtam!

URL Mod-Rewrite

I currently have a working URL:
http://example.com/security-services.php?service=fixed-camera-surveillance
and then I have PHP say, $_REQUEST['service'] to do some stuff...
But I'd like to achieve the same function if the URL looked like this:
http://example.com/security-services/fixed-camera-surveillance
Thanks!
An .htaccess file with something like this should do it.
Options +FollowSymLinks
RewriteEngine On
RewriteRule security-services/(.*)/? security-services.php?service=$1 [L]
The part that says security-services/(.*)/? matches the URL in the browser and rewrites it to security-services.php.
The key part is the (.*) which captures that portion of the URL and passes it to the PHP script as a GET value.
Try this rule:
RewriteEngine on
RewriteRule ^security-services/([^/]+)$ security-services.php?service=$1 [L]
The [^/]+ describes the next path segment after /security-services, (…) forms a group that’s match then can referenced to with $1 in the substitution.
And if you want a more general for any kind of …-service.php file:
RewriteEngine on
RewriteRule ^([^/-]+)-services/([^/]+)$ $1-services.php?service=$2 [L]
You can create a rule like this:
RewriteRule ^security-services/(.*)/? /security-services.php?service=$1
If you're using a .conf file, change ^ to ^/

Categories