Forum Moderators: phranque
I want to have:
viewlisting.php?state=NY&city=Syracuse&category=restaurants&subcategory=italian
to look like:
/NY/Syracuse/Restaurant/Luigi
What I have for code is this in the .htaccess file:
RewriteEngine on
RewriteRule ^viewlistings/(.*)/(.*)/(.*)/(.*).php /viewlistings.php?province=$1&city=$2&category=$3&subcategory=$4
This does not work thought. Any Thoughts?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
Then, on the line that you have that reads:
RewriteRule ^viewlistings/(.*)/(.*)/(.*)/(.*).php /viewlistings.php?province=$1&city=$2&category=$3&subcategory=$4
remove the .php at the end of the first expression, so it to reads:
RewriteRule ^viewlistings/(.*)/(.*)/(.*)/(.*)$ /viewlistings.php?province=$1&city=$2&category=$3&subcategory=$4
My .htaccess file looks like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^viewlistings/(.*)/(.*)/(.*)/(.*)$ /viewlistings.php?province=$1&city=$2&category=$3&subcategory=$4
And what I want it to rewrite is this:
[site.zzz...]
Thank you again so much for your help.
the .htaccess file now reads:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^viewlistings/(.*)/(.*)/(.*)/(.*)$ /viewlistings.php?province=%1&city=%2&category=%3&subcategory=%4 [L]
viewlistings.php?province=Ontario&city=Toronto&category=Bob&subcategory=Wilson
/NY/Syracuse/Restaurant/Luigi
That is not the same as what was listed in your original rewriterule. It contains a request for viewlisting.php and then the subdirectories.
I want to have:viewlisting.php?state=NY&city=Syracuse&category=restaurants&subcategory=italian
to look like:
/NY/Syracuse/Restaurant/Luigi
With this in mind, here is a new rewriterule to cover the alias form "alias-name/province/city/business-type/business-name" - using "vl" to represent viewlistings.php:
RewriteRule ^vl/(.*)/(.*)/(.*)/(.*)$ viewlistings.php?province=%1&city=%2&category=%3&subcategory=%4 [L]
You can try it like that, or with the $ signs instead of % signs and see if either one works for you. Also, try with and without the leading forward slash in the rewriterule, before vl/. This assumes that viewlistings is in your web root directory. If not, add the actual path to it before viewlistings.php.
You would create a hyperlink to it like this: vl/NY/Syracuse/Restaurant/Luigi
Wiz
Also, as I've pointed out before, using multiple ".*" patterns in mod_rewrite directives leads to very poor mod_rewrite performance and pattern ambiguity.
It will indeed probably be necessary to use a unique URL prefix, such as the 'vl/' suggested by WizCrafts, to avoid rewriting URLs you don't want rewritten.
I'd suggest:
RewriteRule ^vl/([^/]+)/([^/]+)/([^/]+)/([^./]+)/?$ /viewlistings.php?province=$1&city=$2&category=$3&subcategory=$4 [L]
Note also that this rule will rewrite URLs only if they have the following characterstics:
This code rewrites the /vl/NY/Syracuse/Restaurant/Luigi URL to /viewlistings.php?province=NY&city=Syracuse&category=Restaurant&subcategory=Luigi URL, and not the other way around. In order to use this code, you need to change all links on all of your pages to the 'static' or 'search-engine-friendly' /vl/NY/Syracuse/Restaurant/Luigi form, so that search engines and visitors will see them.
Jim
My .htaccess file looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^vl/([^/]+)/([^/]+)/([^/]+)/([^./]+)/?$ /viewlistings.php?province=$1&city=$2&category=$3&subcategory=$4
[L]
I am now generating 500 server errors.
in the error log it has the following:
[Tue Mar 21 20:38:53 2006] [alert] [client XX.XX.XX.XX] /path/to/webspace/.htaccess: RewriteRule: bad argument line '^([^/]+)/([^/]+)/([^/]+)/([^./]+)/?$'\n
Any thoughts?
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ /viewlistings.php?province=$1&city=$2&category=$3&subcategory=$4
and when i go to [site.#*$!...] it works, same if I have: [site.xxx...] - but if it goes to [site.xxx...] or [site.xxx...]
I get a 404 error: I'd like to have it so the last two work as well. Ideas?
Phil
The RewriteRule I posted should work fine, as long as all of the code is on one line, you are running a recent version of Apache (1.33 or above), and you are using a plain-text editor to create the file, and not a word-processing program.
To handle 'missing' parameters, you'll need several lines, and you'll have to figure out how to apportion the URL-path-parts to the script variables. If they are always in hierarchical order, then it would be something like this:
RewriteRule ^vl/([^/]+)/([^/]+)/([^/]+)/([^./]+)/?$ /viewlistings.php?province=$1&city=$2&category=$3&subcategory=$4 [L]
RewriteRule ^vl/([^/]+)/([^/]+)/([^/]+)/?$ /viewlistings.php?province=$1&city=$2&category=$3 [L]
RewriteRule ^vl/([^/]+)/([^/]+)/?$ /viewlistings.php?province=$1&city=$2 [L]
RewriteRule ^vl/([^/]+)/?$ /viewlistings.php?province=$1 [L]
Jim
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^./]+)/?$ /viewlistings.php?province=$1&city=$2&category=$3&subcategory=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /viewlistings.php?province=$1&city=$2&category=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /viewlistings.php?province=$1&city=$2 [L]
This works for : www.domain.#*$!/province/city ¦ www.domain.xxx/province/city/category ¦ www.domain.xxx/province/city/category/subcategory
however when i throw the last line in:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^./]+)/?$ /viewlistings.php?province=$1&city=$2&category=$3&subcategory=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /viewlistings.php?province=$1&city=$2&category=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /viewlistings.php?province=$1&city=$2 [L]
RewriteRule ^([^/]+)/?$ /viewlistings.php?province=$1 [L]
I get a 500 server error.
Anythoughts?
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^./]+)/?$ /viewlistings.php?province=$1&city=$2&category=$3&subcategory=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^./]+)/?$ /viewlistings.php?province=$1&city=$2&category=$3 [L]
RewriteRule ^([^/]+)/([^./]+)/?$ /viewlistings.php?province=$1&city=$2 [L]
RewriteRule ^([^./]+)/?$ /viewlistings.php?province=$1 [L]
Jim