Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite problems

showing the url

         

penfold25

9:49 am on Jul 5, 2003 (gmt 0)

10+ Year Member



ok the problem is this, ive read the introduction and some other tutorials about mod_rewrite but for some particular reason cannot find the answer to this question

eg
rewriteRule ^subcat1/subcat25/$ /search.php?categorry-25

when i go through the directory structure firstly through homepage. I click on subcat1, and it displays other subcatergorys.

But the URl i observe in this directory till /search.php?categorry-25,this because it is run on the fly dynamically with phP, how can i make it appear subcat1/subcat25 instead?

it works fine if i type in directly
www.domain/subcat1/subcat25/
that goes to /search.php?categorry-25

however when i do a search engine look up, the catergorys appear as /search.php?categorry-25?

Is there a way so that if it senses /search.php?categorry-25 anywhere it changes it refreshes or changes it straight away to subcat1/subcat25 without the user knowing.

i hope you can help

killroy

10:26 am on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think it should read:

rewriteRule ^/subcat1/subcat25/$ /search.php?categorry-25

matching the leadign slash.

SN

killroy

10:31 am on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what I did to replace old addresses with new ones(which internally were still teh old ones was this:

rewriteRule ^subcat1/subcat25/$ /search.php?categorry-25

#First make sure you don't already have the new URLs
RewriteCond %{REQUEST_URI} !^/subcat1/(.*)$
#Then turn any remaining old URLs into new ones via a 301 redirect
RewriteRule ^/search.php?category-([0-9]+)$ /subcat1/subcat$1 [R=301,L]

#Finally turn newm URLs internally back into the old ones:
RewriteRule ^/subcat1/subcat([0-9]+)$ /search.php?category-$1 [L]

This should change all old addresses in the browser window and slowly cause Google to pick up th enew URLs, while internally the system works as usual.

SN

penfold25

11:12 am on Jul 5, 2003 (gmt 0)

10+ Year Member



hmmm ,not doing anyting at all just, not changing url in browser.
only doing just the simple mod re write, nothing from the info u gave me.

eg, on home page

16 directorys
all having /search.php?catergory1 or 2 or 3 or 4 to 16

can i make all those change to corresponding /catergory/ on the homepage w\so it loads up without ppl realising.?

RewriteCond %{REQUEST_URI}!^/subcat1/(.*)$

is this the main catergory
or subcatergory?

do catergory1/subcat1/

do backslashes make a difference

am using .htaccess
im doing this through php, what could be wrong.

COuld you explain a bit clearer what could be wrong?

killroy

11:18 am on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



couly ou perhaps explain clearer what you are tryign to do?

Can you lsit hte possible groups of URLs, and how you would like them to show?

Thanks,

SN

penfold25

11:30 am on Jul 5, 2003 (gmt 0)

10+ Year Member



ok for example
on the homepage there is listed business as the major catergory and under in smaller writing, the subcatergorys of it which are economics and finance.

Now if u go over "Business" it with the mouse you see the url at the bottom of explorer as /search/search.php?search=cat&category=1

now i would like this mouseover to appear as /business/ and /business/economics/ etc.

Then when i click at "business", it shows /search/search.php?search=cat&category=1 in the browser on the new page.
I want this too appear as /business/

On this same page, there are subcatergorys listed which are econimics and finance. These catergorys when u go over the mouse are shown as /search/search.php?search=cat&category=2 and /search/search.php?search=cat&category=3

I want these to also appear as /business/economics/

I hope its a little clearer what i want to do, can u help?

killroy

11:52 am on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well to cahnge the mouseover you have to change the URL sued in teh script and written to the page. You do have access to the scripts?

SN

penfold25

12:00 pm on Jul 5, 2003 (gmt 0)

10+ Year Member



yes i do have access.

its a off the shelf directory software i bought, so i can do whatever really cos i got it hosted remotely.

but i have a search facility and when it comes up with results i have the catergories listed for the results, i want those catergorys to give old results to not be inconsistent.

So ye what could i do?

penfold25

1:06 pm on Jul 5, 2003 (gmt 0)

10+ Year Member



any other help would be appreciated?

killroy

2:32 pm on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



first you have to set up the mod_rewrite to convert new urls to old urls, as I've shown above. Then you have to change teh scripts to use the new urls.

SN

penfold25

2:37 pm on Jul 5, 2003 (gmt 0)

10+ Year Member



ok
but how do i know whether its working or not

rewriteRule ^subcat1/subcat25/$ /search.php?categorry-25
that works fine.

what about these, how do i know these are working fine?

#First make sure you don't already have the new URLs RewriteCond %{REQUEST_URI}!^/subcat1/(.*)$ #Then turn any remaining old URLs into new ones via a 301 redirect RewriteRule ^/search.php?category-([0-9]+)$ /subcat1/subcat$1 [R=301,L] #Finally turn newm URLs internally back into the old ones: RewriteRule ^/subcat1/subcat([0-9]+)$ /search.php?category-$1 [L]

jdMorgan

2:43 pm on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



penfold25,

I'm off for the day to do other things, but you need to know a couple of things. First, in this rule:

RewriteRule ^/search.php?category-([0-9]+)$ /subcat1/subcat$1 [R=301,L]

the leading slash in the pattern will only match if this code is in httpd.conf. The rule won't work in .htaccess.

Second, if you want an external redirect - one that changes the URL in the visitor's browser and causes robots to note a change for their search index, then you must use the [R] flag, and you should use a full URL:

RewriteRule ^search.php?category-([0-9]+)$ http://www.yourdomain.com/dir/subcat1/subcat$1 [R=301,L]

In all cases, the [L] flag stops further processing for the request-in-progress, which reduces the chances for an infinite loop and saves processing time. It is most likely that this is what you want to do. I note this only in case you want one request to activate multiple rewriterules in sequence, with each rule changing the URL one step at a time.

HTH,
Jim

killroy

2:46 pm on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry yabout this. I've never used .htaccess as I have full access to all my sites and server config. You'll have to check the docs for differences.n fact I highly recommend readign the mod_rewrite docs, they're not nearly as arcane as many seem to think.

SN

penfold25

1:29 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



still having problems in htaccess

i have two rules wrriten up

Options +FollowSymLinks
rewriteEngine on

rewriteRule ^search/sample.php?search=cat&category=31([0-9]+)$ /search/engineering$1 [R=301,L]

rewriteRule ^engineering/$ /search/hyperseek-sample.php?search=cat&category=31

The 2nd rule works why doesnt the first?

the url i want to show on the user browser should be
domain.com/engineering instead of domain.com/search/sample.php?....

should it be

rewriteRule ^www.domain/search/sample.php?search=cat&category=31([0-9]+)$ /search/engineering$1 [R=301,L]

?

jdMorgan

8:59 pm on Jul 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Two problems... First, you should provide a full URL with [R=301], and second, the QUERY_STRING is probably not visible as part of the URL in .htaccess context. So, you have to use RewriteCond [httpd.apache.org] with {QUERY_STRING} to test it and grab it. Something like this:

RewriteCond %{QUERY_STRING} ^search=cat&category=31([0-9]+)$
RewriteRule ^search/sample\.php$ http://www.domain.com/search/engineering%1 [R=301,L]

Jim

penfold25

4:53 pm on Jul 17, 2003 (gmt 0)

10+ Year Member



thanx for your help
but i been trying so many times, maybe there was a little syntax error that u put?
this is all i have
can someone make sure for this is for htaccess which is located in the
www.domain.com/search/

here it is the code in the search directory for .htaccess

Options +FollowSymLinks
rewriteEngine on
RewriteCond %{QUERY_STRING} ^search=cat&category=31([0-9]+)$
RewriteRule ^search/sample\.php$ [domain.com...] [R=301,L]
rewriteRule ^engineering/$ /search/sample.php?search=cat&category=31

when i type i want it to go from
www.domain/search/sample.php?search=cat&category=31
this is on my homepage to go to when i click it to go to
www.domain/engineering

help anyone please

jdMorgan

5:20 pm on Jul 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



penfold25,

If you want to put this code in subdirectory "search", then "search" cannot be included in the 1st RewriteRule, as it will have been stripped off by then:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^search=cat&category=31([0-9]+)$
RewriteRule ^sample\.php$ http://www.domain.com/search/engineering%1 [R=301,L]
RewriteRule ^engineering/$ /search/sample.php?search=cat&category=31

The second rule may not work as expected if you place this .htaccess code in subdirectory "search." It will only rewrite domain.com/search/engineering to domain.com/search/sample/php?search=cat&category=31
The solution is to put the code in post #15 in your web root directory .htaccess.

I encourage you to read the documentation and experiment with this until you get comfortable with it.

Ref: Introduction to mod_rewrite [webmasterworld.com]

HTH,
Jim

penfold25

5:54 pm on Jul 17, 2003 (gmt 0)

10+ Year Member



i am sorry jdmorgan, the htaccess is actually in the public_html directory
it is actually in the root directory

i have tried it seriously over 50 times, and it only brings up

www.domain/search/sample.php?search=cat&category=31

doesnt change it at all

creating the directory is no problem just the first rule part seems to do nothing at all

is the order of the rules correct
?

jdMorgan

7:20 pm on Jul 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



penfold25,

For the root directory:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^search=cat&category=31([0-9]+)$
RewriteRule ^search/sample\.php$ http://www.domain.com/search/engineering%1 [R=301,L]
RewriteRule ^engineering/$ /search/sample.php?search=cat&category=31 [L]

Let's take this first rule apart:

RewriteCond %{QUERY_STRING} ^search=cat&category=31([0-9]+)$

IF the querystring is "?search=cat&category=310" through "?search=cat&category=319" ...

RewriteRule ^search/sample\.php$ http://www.domain.com/search/engineering%1 [R=301,L]


AND the requested page is "www.domain.com/search/sample.php"
THEN redirect to [domain.com...] whatever category number was requested in the query string >

It strikes me that perhaps the rule you wrote with the categories was not what you wanted. Do your categories run from 310-319, or from 31-39? If the latter, then you need:


RewriteCond %{QUERY_STRING} ^search=cat&category=(3[1-9]+)$

instead.

The second rule unconditionally and transparently redirects from "www.domain.com/engineering/" to "www.domain.com/search/sample.php?search=cat&category=31"
It will not redirect if the trailing slash is missing.

Jim

penfold25

4:56 am on Jul 23, 2003 (gmt 0)

10+ Year Member



hmm someone told me about adding
rewriteBase / in there somewhere
?
would that make it work?