creating nice url but not with slashes  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | |
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?
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 913
| | | 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??
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | re: creating nice url but not with slashes
tried but not working, anyone could give an example?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: creating nice url but not with slashes Quote:
Originally Posted by mikek12004 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… - www.mysite.com/album+1.0096+Love to give
at least should give you a query string to operate on
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | 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?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: creating nice url but not with slashes Quote:
Originally Posted by mikek12004 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)
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | 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. )?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: creating nice url but not with slashes
what issue? if mod_rewrite works as expected, the server is not requested of invalid directories.
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | 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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: creating nice url but not with slashes Quote:
Originally Posted by mikek12004 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… - # remove the fake directories for internal files (CSS, JS)
-
RewriteRule ^\w+/(.+)$ $1 [NC]
-
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | re: creating nice url but not with slashes
So in my htaccess would look like this -
RewriteEngine On # Turn on the rewriting engine
-
# Handle product requests
-
RewriteRule ^products/([0-9]+)/?$ index.php?id=$1 [L]
-
# remove the fake directories for internal files (CSS, JS)
-
RewriteRule ^\w+/(.+)$ $1 [NC]
-
right?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | 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…
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | re: creating nice url but not with slashes
would the 2 levels back be -
# remove the fake directories for internal files (CSS, JS)
-
RewriteRule ^\w+/(.+)/(.+)/$ $1 [NC]
-
'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?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | 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)
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | 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?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: creating nice url but not with slashes - RewriteRule ^cleanURLs/products/([0-9]+)/([0-9]+)/?$ cleanURLs/index.php?id=$1&b=$2 [L]
-
RewriteRule ^cleanURLs/products/\d+/\d+/(.+)$ cleanURLs/$1
looks more sensible
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | 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?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: creating nice url but not with slashes Quote:
Originally Posted by mikek12004 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
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | 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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: creating nice url but not with slashes Quote:
Originally Posted by mikek12004 modrewrite doesn't allow UTF-8 characters? nope, though the reason is that URLs don’t allow UTF-8 characters (see RFC 1738)
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | 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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: creating nice url but not with slashes Quote:
Originally Posted by mikek12004 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 _)
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | 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 - 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/.+/.+/.+/(.+)$ $1
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | 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?)
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: creating nice url but not with slashes Quote:
Originally Posted by mikek12004 (my only worry is when the search engines see the ? or & if they responce negative, what do you think?) what question mark?
________
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | 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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: creating nice url but not with slashes Quote:
Originally Posted by mikek12004 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 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
|  | Familiar Sight | | Join Date: Sep 2008 Location: Athens, Greece
Posts: 186
| | | 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() ? )
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: creating nice url but not with slashes
I’d prefer rawurlencode().
|  | Similar Apache Web Server bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,376 network members.
|