Forum Moderators: phranque

Message Too Old, No Replies

Why wont these URLS rewrite?

Why wont these URLS rewrite?

         

and1c

11:01 am on Aug 31, 2007 (gmt 0)

10+ Year Member



ok guys, following on from a previous question about URL rewriting where I ended up having to modify URL generation on my php script based site.

I have a couple of types of URL Im trying to rewrite, the only 2 types on the site now to make all URLs SEO friendly!

It has proven to be a very big job to recode the URL generation for these URLs (due to uncommented inept coding by original author) so I have decided only possible to go is with URL rewrites.

But they are not rewriting and I cant understand why!all my other rewrites still work as normal.

Here is the URLS and the rewrites I tried:

RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteRule view/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ /view.php?$1=$2&$3=$4&$5=$6

//I was thinking the above rule would do the first 2 types of URL, and the second rule would do the longer dynamic URL (could I just use the second rewrite rule alone?)

RewriteRule view/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ /view.php?$1=$2&$3=$4&$5=$6&$7=$8

This Dynamic URL

http://www.example.com/view.php?company_id=45&auth_sess=j67jh364ptml78edibg6t3isb3&ref=c37710bb2e45e56f85393d61f

I was trying to rewrite to

http://www.example.com/view/company_id/45/auth_sess/j67jh364ptml78edibg6t3isb3/ref/c37710bb2e45e56f85393d61f

*************************************************
This Dynamic URL

http://www.example.com/view.php?job_id=60&type=search&auth_sess=j67jh364ptml78edibg6t3isb3&ref=41575ba4e2aa2a28713cd8ea7

I was trying to rewrite to

http://www.example.com/view/job_id/60/type/search/auth_sess/j67jh364ptml78edibg6t3isb3/ref/41575ba

*************************************************

and
This Dynamic URL

http://www.example.com/view.php?company_id=49&type=search&auth_sess=um11p5lbif0f53s1h2dicge550&ref=82b7c88f2f71a78c40df5088
b

I was hoping to rewrite to

http://www.example.com/view/company_id/49/type/search/auth_sess/um11p5lbif0f53s1h2dicge550/ref/82b7c88f2f71a78c40df5088b/

It is important to note that these URLS or items are never linked to but instead are generated when a user searches for a company or job through a roundabout and not normally used method!

Can it be done? I have a feeling Im missing something
...but I need an expert to tell me what the heck it is please!

thanks

[edited by: and1c at 11:12 am (utc) on Aug. 31, 2007]

[edited by: jdMorgan at 1:48 pm (utc) on Aug. 31, 2007]
[edit reason] example.com [/edit]

and1c

11:04 am on Aug 31, 2007 (gmt 0)

10+ Year Member



I meant to add that I have already got a pretty significant htaccess with some rewrite rules in so I know that position of the rule is important. I have tried it in all the locations of htaccess that I thought were viable..and the URLS are still the session string style.

jdMorgan

1:57 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Consider this:

The pattern "(.*)" matches as many characters of any kind as possible.
Therefore, "(.*)/" will match
foo/
foo/bar/
foo/bar/bat/
equally.

Therefore, your first (shorter) rule will also match the longer URL, so the second rule will never be applied.

Advice: Never use "(.*)/(.*)" or any pattern like it, unless there is no other choice. It is horribly inefficient to process, and causes exactly the kind of problem you're seeing.

Re-code both of your rules like this, and your results will probably be much better:


RewriteRule view/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /view.php?$1=$2&$3=$4&$5=$6&$7=$8

The "([^/]+)/" pattern means, "Match one or more characters not equal to a slash, followed by a slash."

This pattern can quickly and efficiently find the "boundaries" between your parameters, unlike ".*" which will have to try dozens, hundreds, or even thousands of times, because the ".*" pattern matches the "/" characters, which is not what you want.

Jim

and1c

2:37 pm on Aug 31, 2007 (gmt 0)

10+ Year Member



Consider this:
The pattern "(.*)" matches as many characters of any kind as possible.
Therefore, "(.*)/" will match
foo/
foo/bar/
foo/bar/bat/
equally.

Therefore, your first (shorter) rule will also match the longer URL, so the second rule will never be applied.

Advice: Never use "(.*)/(.*)" or any pattern like it, unless there is no other choice. It is horribly inefficient to process, and causes exactly the kind of problem you're seeing.

Re-code both of your rules like this, and your results will probably be much better:

RewriteRule view/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /view.php?$1=$2&$3=$4&$5=$6&$7=$8

The "([^/]+)/" pattern means, "Match one or more characters not equal to a slash, followed by a slash."

This pattern can quickly and efficiently find the "boundaries" between your parameters, unlike ".*" which will have to try dozens, hundreds, or even thousands of times, because the ".*" pattern matches the "/" characters, which is not what you want.

Jim

Many Thanks for the input Jim

I corrected the rule and am still not seeing the URLs rewritten possibly because....

RewriteEngine On
RewriteBase /
Options +FollowSymLinks

RewriteRule ^jobs/([0-9]+)-([^/]*)\.html /view.php?job_id=$1 [L]
RewriteRule ^company/([0-9]+)-([^/]*)\.html /view.php?company_id=$1 [L]
RewriteRule ^view/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /view.php?$1=$2&$3=$4&$5=$6&$7=$8 [L]
RewriteRule ^jobs/(.*).php /$1.php [L]
RewriteRule ^company/(.*).php /$1.php [L]

Above are the current rewrite rules at the top of my htaccess (none of the others below are related to these).
It seems the rules conflict?!

going on past experience the URL just tends to not work if a rewrite is conflicting or half working though?

in this case there is nothing except no rewrite? although Im sure I detected an unusaul slowness when searching for a few companies that went once I removed the rule?!

http://www.mysite.com/view.php?company_id=78&type=search&auth_sess=iltjareas39vt2gi1maem9ee4&ref=c102e7eda354332e887b90d7b

Can I confirm I am correct in thinking that no matter how many actions you have to perform to get to these URLs, they are still passed through htaccess regardless so it should be attempting to rewrite them?

The file view.php is in the root of my site and the other rewrites involving it work fine!

Is there a better way to structure the rule for the URLS in post one bearing in mind I already have 2 rewrite rules for view.php related to company and advert ID's?

jdMorgan

10:07 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see a "guaranteed" conflict anywhere in your code.

Did you completely flush you browser cache before testing? If not, your browser will serve the requested page from cache, and it will not send any request to your server. If your server doesn't receive any request, then of course your code can have no effect.

Jim

and1c

6:35 pm on Sep 2, 2007 (gmt 0)

10+ Year Member



Thanks Jim

Alas... I had emptied the cache and refreshed my browser before trying the new rewrite rule.
This is a bit annoying as I cannot think what could be causing the lack of re write?!
Can anyone tell me if there is a logging function available for mod rewrite please? Maybe by enabling this I can find out what the problem is?
thanks