Forum Moderators: phranque

Message Too Old, No Replies

Simple? mod-re-write issue

         

pblancher

5:19 am on Mar 20, 2006 (gmt 0)

10+ Year Member



I am trying to do a simple mod-rewrite in an .htaccess file.

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?

Wizcrafts

3:16 pm on Mar 20, 2006 (gmt 0)

10+ Year Member



Try adding these lines to the beginning of the mod_rewrite section:

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

pblancher

3:52 pm on Mar 20, 2006 (gmt 0)

10+ Year Member



I tried the changes suggested. I know the .htaccess file does work because I was able to try 404 re-direct and it worked, however the changes did not.

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.

Wizcrafts

4:21 pm on Mar 20, 2006 (gmt 0)

10+ Year Member



Try this instead (% instead of $ and added [L]):

RewriteRule ^viewlistings/(.*)/(.*)/(.*)/(.*)$ /viewlistings.php?province=%1&city=%2&category=%3&subcategory=%4 [L]

Wiz

pblancher

4:31 pm on Mar 20, 2006 (gmt 0)

10+ Year Member



unfortunately it did not work. I am running apache 1.3.34.

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

Wizcrafts

5:43 pm on Mar 20, 2006 (gmt 0)

10+ Year Member



This might require a RewriteCond %{QUERY_STRING} solution.

Something like this:

RewriteCond %{QUERY_STRING} (.+)
RewriteRule <snip> ..... </snip>

Perhaps somebody else can elaborate more on this.

BarryStCyr

5:58 pm on Mar 20, 2006 (gmt 0)

10+ Year Member



Check with your ISP that Rewrite is allowed in .htaccess. The ErrorDocument directive can be enabled differently from the Rewrite Engine.

Hope this helps.

Barry

pblancher

6:24 pm on Mar 20, 2006 (gmt 0)

10+ Year Member



mod-rewrite is allowed on my webhost. I called and double checked before doing anything. :)

Wizcrafts

7:42 pm on Mar 20, 2006 (gmt 0)

10+ Year Member



I just looked over you OP and saw that you want this path as the alias:

/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

jdMorgan

3:30 am on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you wish to back-reference patterns created in a RewriteRule, you *must* use variables $1 through $9. If you wish to back-reference patterns in the last-matched RewriteCond, you *must* use variable %1 through %9. So, $1 through $9 is appropriate here.

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 that URLs for all included objects (images, stylesheets, external JavaScripts, etc.) on pages generated by viewlistings.php will need to be server-relative or canonical, that is <img src="/logo.gif"> or <img src="http://www.example.com/logo.gif>", and not <img src="logo.gif">. Otherwise, the image links will break because the browser will resolve them --in the example URL given here-- to /vl/NY/Syracuse/Restaurant/logo.gif

Note also that this rule will rewrite URLs only if they have the following characterstics:

  • Local URL-path starts with "vl/".
  • Local URL-path does not contain a "." in the last part, i.e. no 'filetype'.
  • Local URL-path contains four 'parts' delimited by "/" characters.
  • A trailing slash is optional (some browsers or mod_dir may add one).

    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

  • pblancher

    8:53 pm on Mar 21, 2006 (gmt 0)

    10+ Year Member



    Hi All,

    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?

    pblancher

    9:34 pm on Mar 21, 2006 (gmt 0)

    10+ Year Member



    ok ignore the last one. I reverted to the following:

    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

    jdMorgan

    10:30 pm on Mar 21, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    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]

    But I strongly suggest that you get one rule working first, and then add another, etc.

    Jim

    pblancher

    3:01 am on Mar 22, 2006 (gmt 0)

    10+ Year Member



    OK. Now we are cooking.

    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?

    jdMorgan

    6:42 am on Mar 22, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Yes, your last rule is looping, rewriting /viewlistings.php to /viewlistings.php, ad-infinitum. That's because I mis-copied the code as I composed it. Here's the fix:

    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]

    Note that the *last* negative-pattern group in each line needs the "." in it to prevent rewriting URLs that have filetypes in them.

    Jim

    pblancher

    6:58 am on Mar 22, 2006 (gmt 0)

    10+ Year Member



    Perfect! That works just as I needed it to do.

    Thank you all very much for your help:

    Phil