Connecting Tech Pros Worldwide Help | Site Map

creating nice url but not with slashes

mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#1: Sep 28 '09
Almost for all thing I use my index.php with the value of 'page' as a criterion. However when I view my products which is something like this
www.mysite.com/index.php?page=2&kwd=1.0096&description=Love to give&type=album
to be viewed
www.mysite.com+album+1.0096+Love to give

note that I do not have a problem with the slashes as seperators but then I have to change all the src's attributes of the images and I have read somewhere that woth anything else like '+' or ':' as a separator will work, is it true (in a working example with slashes tried to replace them in the .htaccess file with + and got object not found both when I commented the + and when not);
the is also the point of having in my description spaces, in an example of two with multiwork get worked ok, but read somewhere I have to pass them through urlencode() or something like that, is it necessery and changes this anything in the .htaccess file?
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#2: Sep 28 '09

re: creating nice url but not with slashes


The plus symbol in a htaccess statement means one or more of the preceding character/range. So you need to escape the symbol. I haven't ever tried to have + in my urls, so give it a go, but I suspect that's why it's not working. I hope that a simple "\" character will be enough, but you might have to play around with a {QUERY_STRING} condition??
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#3: Sep 30 '09

re: creating nice url but not with slashes


tried but not working, anyone could give an example?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#4: Sep 30 '09

re: creating nice url but not with slashes


Quote:

Originally Posted by mikek12004 View Post

www.mysite.com+album+1.0096+Love to give

this doesn’t look like a good choice, since all seems to be part of the domain and I’m not sure, whether this has a query string at all…

Expand|Select|Wrap|Line Numbers
  1. www.mysite.com/album+1.0096+Love to give
at least should give you a query string to operate on
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#5: Sep 30 '09

re: creating nice url but not with slashes


Didn't quite understand what you said, regardless the original url is
http://www.mysite.com/index.php?page...scription=Love to give&type=album
and want to modify to nice url so that the user can type
www.mysite.com/album/Love to give
however with slashes as seperator since I have given relative directories to the images (which would be good to keep them aftear all I use a demo folder all the time) they get broken up, so read somewhere to use some other symbol as the + or : ,that is the one thing. The other was my consern about the description variable having symbols like space, question marks etc. will it be a problem and if so how to handle it?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#6: Sep 30 '09

re: creating nice url but not with slashes


Quote:

Originally Posted by mikek12004 View Post

The other was my consern about the description variable having symbols like space, question marks etc. will it be a problem and if so how to handle it?

usually a browser escapes invalid characters before sending the request. (? is a valid character, see RFC 1738 for more info)
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#7: Sep 30 '09

re: creating nice url but not with slashes


and about the issues with the slashes? Would you used it even if it screws your relative directory in the images or would you use some other symbol (+, - , : etc. )?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#8: Sep 30 '09

re: creating nice url but not with slashes


what issue? if mod_rewrite works as expected, the server is not requested of invalid directories.
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#9: Sep 30 '09

re: creating nice url but not with slashes


suppose we have an www.aaa.com/index.php?id=1&user=mike which has an image with src='img/a1.jpg'
when I used htmod so that that url works www.aaa.com/1/mike the image appeared broken, read somewhere that this is because of the URL being like this and a possible solution if I didn't want to use absolute paths would be to replace the '/' with another symbol. Tried the www.aaa.com/1+mike
(with the '+' escaped and not) in the rewrite rule but always got 'not found' when return to the slashes the htaccess worked fine
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#10: Sep 30 '09

re: creating nice url but not with slashes


Quote:

Originally Posted by mikek12004 View Post

when I used htmod so that that url works www.aaa.com/1/mike the image appeared broken,

then you need an additional filter for the image urls.

the rule I use…
Expand|Select|Wrap|Line Numbers
  1.     # remove the fake directories for internal files (CSS, JS)
  2.     RewriteRule ^\w+/(.+)$ $1 [NC]    
  3.  
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#11: Sep 30 '09

re: creating nice url but not with slashes


So in my htaccess would look like this
Expand|Select|Wrap|Line Numbers
  1. RewriteEngine On # Turn on the rewriting engine
  2. # Handle product requests 
  3.     RewriteRule ^products/([0-9]+)/?$ index.php?id=$1 [L] 
  4. # remove the fake directories for internal files (CSS, JS) 
  5.     RewriteRule ^\w+/(.+)$ $1 [NC]     
  6.  
right?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#12: Sep 30 '09

re: creating nice url but not with slashes


not sure if it works for you, since you have to go 2 directories back (I use more than one such line and our cases may not be compareable). you could even make it more specific to images…
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#13: Sep 30 '09

re: creating nice url but not with slashes


would the 2 levels back be
Expand|Select|Wrap|Line Numbers
  1. # remove the fake directories for internal files (CSS, JS)  
  2.     RewriteRule ^\w+/(.+)/(.+)/$ $1 [NC] 
  3.  
'cause right now even with the original I do not see the picture going back one level
I realise that I would face the same issue with the CSS/JS also so a rule which could make them work OK is much desirable.
Your method works ok even if you use the normal url and not the nice one?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#14: Sep 30 '09

re: creating nice url but not with slashes


you’re going down 3 directories which, if I remember right, does not match your URL.
Quote:

Originally Posted by mikek12004

Your method works ok even if you use the normal url and not the nice one?

hard to tell, I normally use a separate domain for images, CSS and Javascript (but what’s relative defined, works)
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#15: Sep 30 '09

re: creating nice url but not with slashes


Just to get the hang of I have an index.php which has this:
Quote:
<?php
echo $id=$_GET['id'];

echo 'none';
?>
<img src="imgs/bemember.jpg" />
and the htaccess file
Quote:
RewriteEngine On # Turn on the rewriting engine
# Handle product requests
RewriteRule ^products/([0-9]+)/([0-9]+)/?$ index.php?id=$1&b=$2
RewriteRule ^\w+/(.+)/(.+)/(.+)/$ $1 [NC]
now whether I use
http://127.0.0.1/cleanURLs/products/1/2/
or
http://127.0.0.1/cleanURLs/products/1/2
I get redirected to the correct page but the image (when I choose right click+properties) regardless the rule shows
http://127.0.0.1/cleanURLs/products/...s/bemember.jpg
how would you make it in this situation?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#16: Sep 30 '09

re: creating nice url but not with slashes


Expand|Select|Wrap|Line Numbers
  1. RewriteRule ^cleanURLs/products/([0-9]+)/([0-9]+)/?$ cleanURLs/index.php?id=$1&b=$2 [L]
  2. RewriteRule ^cleanURLs/products/\d+/\d+/(.+)$ cleanURLs/$1
looks more sensible
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#17: Sep 30 '09

re: creating nice url but not with slashes


worked only like this
Quote:
RewriteEngine On # Turn on the rewriting engine
# Handle product requests

#RewriteRule ^products/([0-9]+)/([0-9]+)/?$ index.php?id=$1&b=$2
RewriteRule ^products/([0-9]+)/([0-9]+)/?$ index.php?id=$1&b=$2 [L]
RewriteRule ^products/\d+/\d+/(.+)$ $1
for some reason when I added the 'cleanURLs/' at any of those rules they didn't work like this work ok it's pretty equivalent right?

PS for the image to work even when I put
http://127.0.0.1/cleanURLs/products/1/2 (without the final slash) how to modify the rule?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#18: Sep 30 '09

re: creating nice url but not with slashes


Quote:

Originally Posted by mikek12004 View Post

for some reason when I added the 'cleanURLs/' at any of those rules they didn't work like this work ok it's pretty equivalent right?

as long as it works…

PS: you should use code tags instead of quote tags
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#19: Oct 1 '09

re: creating nice url but not with slashes


In our previous example tried greek charcters in the query string like .
http://127.0.0.1/cleanURLs/products/μιχάλης/2
having the below htaccess
Quote:
RewriteEngine On # Turn on the rewriting engine
# Handle product requests

RewriteRule ^products/([0-9a-z]+)/([0-9a-z]+)/?$ index.php?id=$1&b=$2 [L]
RewriteRule ^products/\d+/\d+/(.+)$ /$1
and got a nice (ok not that nice) 'Object not found', modrewrite doesn't allow UTF-8 characters?
P.S Yeah saw it too, meant to put code tags but slipped my attention, sorry for that
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#20: Oct 1 '09

re: creating nice url but not with slashes


Quote:

Originally Posted by mikek12004 View Post

modrewrite doesn't allow UTF-8 characters?

nope, though the reason is that URLs don’t allow UTF-8 characters (see RFC 1738)
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#21: Oct 1 '09

re: creating nice url but not with slashes


tough luck oh well, in a regular expression matter the I wanted to have alphanumeric so
Quote:
RewriteRule ^products/([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ index.php?a=$1&b=$2&c=$3 [NC,L]
RewriteRule ^products/\w+/\w+/\w+/(.+)$ $1
a)how can I include the space e.g a link like
http://127.0.0.1/cleanURLs/products/...100930/flowers and star/
will not work because the third acquirement have spaces
b) how can I also include in the last arquements the symbols ; / - . ' " \ !
(it will be the title of the album so I wish to be well covered is there a more generic definition to include something like all printable characters)?
PS tried
Quote:
RewriteRule ^products/([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9.]+)/?$ index.php?a=$1&b=$2&c=$3 [NC,L]
To support the . in the last parameter but then the last rule stopped working and the image appeared broken
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#22: Oct 1 '09

re: creating nice url but not with slashes


Quote:

Originally Posted by mikek12004 View Post

a)how can I include the space
b) how can I also include in the last arquements the symbols ; / - . ' " \ !

if you have a valid URL, you only need to add % (see RFC 1738)

. in RegEx is any character except newline

[A-Za-z0-9] is nearly the same as \w (which only adds _)
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#23: Oct 1 '09

re: creating nice url but not with slashes


Quote:
[A-Za-z0-9] is nearly the same as \w (which only adds _)
meaning that leaving the 2nd rule alone will be fine? well... with
Quote:
RewriteRule ^products/([A-Za-z0-9]+)/([A-Za-z0-9.]+)/(.+)/?$ index.php?a=$1&b=$2&c=$3 [NC,L]
RewriteRule ^products/\w+/\w+/\w+/(.+)$ $1
but when I enter
http://127.0.0.1/cleanURLs/products/...930/hi world!/
(with the final slash or without)
the image is broken
this is also the case when
http://127.0.0.1/cleanURLs/products/albums/1.00930/hi/
aslo tried unsuccessfully
Expand|Select|Wrap|Line Numbers
  1. RewriteRule ^products/([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9.]+)/?$ index.php?a=$1&b=$2&c=$3 [NC,L]
  2. RewriteRule ^products/.+/.+/.+/(.+)$ $1
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#24: Oct 1 '09

re: creating nice url but not with slashes


OK after a bit of trial and error made it work like this
Quote:
RewriteRule ^products/([A-Za-z0-9]+)/([0-9]+\.[0-9]+)/([^/]+)/?$ index.php?a=$1&b=$2&c=$3 [NC,L]
RewriteRule ^products/[A-Za-z0-9]+/[0-9]+\.[0-9]+/[^/]+/(.+)$ $1
for URLS like
http://127.0.0.1/cleanURLs/products/...20description/
well the in the description I do not care about encoded chars since I do not take this as a variable at all all I need is the product number (11.0987) so even if there "album description" I do not care it's there just for completion reasons (my only worry is when the search engines see the ? or & if they responce negative, what do you think?)
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#25: Oct 1 '09

re: creating nice url but not with slashes


Quote:

Originally Posted by mikek12004 View Post

(my only worry is when the search engines see the ? or & if they responce negative, what do you think?)

what question mark?

________
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#26: Oct 1 '09

re: creating nice url but not with slashes


in a nice URL like
http://www.mysite.com/products/album...0/album_title/
if the "album title" is something like "me & you" or "Do U Love Me?" where symbols like '&' and '?' appear uncoded in the URL will the search engines react negatively?
I do not encode these symbol (eg. ' ' to %20) because I do not want to decrease the readability of the URL, and I do not care to take this as a variable since I can take all my data with the product code (1.00920) but what about the search engines
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#27: Oct 1 '09

re: creating nice url but not with slashes


Quote:

Originally Posted by mikek12004 View Post

if the "album title" is something like "me & you" or "Do U Love Me?" where symbols like '&' and '?' appear uncoded in the URL will the search engines react negatively?

not only search engines, but pretty much every user Agent*.

Quote:

Originally Posted by mikek12004 View Post

I do not encode these symbol (eg. ' ' to %20) because I do not want to decrease the readability of the URL

readability is nothing the Specs (resp. the validaty) care about. (besides that invalid characters are converted anyway by the browser)

* – for instance "&" will trigger an "undefined entity" error in XHTML
mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#28: Oct 1 '09

re: creating nice url but not with slashes


so the only good solution is to use urlencode() right? (actually which would you use rawurlencode() or urlencode() ? )
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#29: Oct 1 '09

re: creating nice url but not with slashes


I’d prefer rawurlencode().
Reply