Forum Moderators: phranque

Message Too Old, No Replies

Problem with mod rewrite

         

jcmiras

9:32 am on May 4, 2005 (gmt 0)

10+ Year Member



I have a website with pages having the following URL,
[mydomain.com...]
[mydomain.com...]

where $cat is a number from 0 to any value.
I rewrite the URL using the following code at .htaccess,

RewriteEngine on
RewriteBase /
RewriteRule ^phil(.*).htm index.php?cat=$1 [L]
RewriteRule ^addurl(.*).htm index.php?action=reclink&cat=$1 [L]

my pages will now have the following URL,
[mydomain.com...]
[mydomain.com...]

My problem is that, in the recent index of google, google indexed my pages as it was not "rewrited". What i mean is that, instead of having,

[mydomain.com...]
[mydomain.com...]

google index it as,

[mydomain.com...]
[mydomain.com...]

I have noticed it in my cpanel.

Question:
Is there a problem with google or in my code?

Thanks.

[edited by: jdMorgan at 4:29 pm (utc) on May 4, 2005]
[edit reason] Removed specifics per TOS. [/edit]

jd01

9:54 am on May 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi jcmiras,

The problem is you're going the wrong way, and maybe a couple of other minor things...

Start here:
[webmasterworld.com...]

If you still have questions, post again.

Justin

BTW if you can still get in you should remove the URL for your site... 'TOS'

jcmiras

1:25 pm on May 4, 2005 (gmt 0)

10+ Year Member



OK, so what do you think is the problem with this code;

RewriteEngine on
RewriteBase /dir/
RewriteRule ^phil(.*).htm index.php?cat=$1 [L]
RewriteRule ^addurl(.*).htm index.php?action=reclink&cat=$1 [L]

to produce these;

[mydomain.com...]
[mydomain.com...]

from these;

[mydomain.com...]
[mydomain.com...]

sorry for posting my website, I though it would help you analyze my problem. how can i delete it?

jd01

6:45 pm on May 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the problem is this line from your original post:
>> my pages will now have the following URL

It sounds as though you are wanting Apache to change the URL displayed in the browser, but display the information from the originally requested page...

If this is the case, the problem is not the .htaccess code per se, but rather the links on your page. For a silent redirect to happen, the URL you would like to have displayed must be requested... EG some one must click on a link to, type in, or first be externally redirected to the URL you are internally or 'silently' redirecting. If none of these has happened the neither the URL, nor the information will be served in the way you would like.

If this is not the case, please post again, with (generalized) detailed information about your linking structure and any (generalized) specifics on how your .htaccess and site navigation is set up.

Hope this helps.

Justin

jcmiras

10:20 pm on May 4, 2005 (gmt 0)

10+ Year Member



OK here`s the delemna,

first, i have a website with the following URL patern for all pages:

(1) www.domain.com/dir/index.php?cat=4

I use mod rewrite so that if i requested,

(2) www.domain.com/phil4.htm

(note: 4 can be any number)

the browser will display, URL (1),

There is no problem on how URL (2) is requested in all my pages. Actually, i havent encounter any problem before. That is, google and other SE crawl my pages as (2). It was only yesterday that i`ve notice that even if all of my pages are linked as (2), the log on my cpanel shows that google sees my pages as (1) even if it is linked as (2). In the first place, there is no page that linked to other pages with pattern No. 1.
Then, the possible problem, i thought is, there is a problem in my code at .htaccess AND/OR google sees the original URL and index it as the pattern (1) even if the URL is requested as in pattern (2). I dont have any problem with other SE, with google only.

jdMorgan

11:32 pm on May 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jcmiras,

Welcome to WebmasterWorld!

You'll need to add more rules that perform the opposite function to those that you already have, and generate a 301 permanent redirect -- but only for external client requests for the dynamic URLs. Example:


# Existing rewrite
RewriteRule ^phil(.*)\.htm$ /index.php?cat=$1 [L]
# New external redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index\.php\?cat=([0-9]+)\ HTTP/
RewriteRule ^index\.php$ /phil%1.htm [R=301,L]

Note that THE_REQUEST is the original request header received from the browser, in the form:

GET /index.php?cat=4 HTTP/1.1

Note also the corrections to your original rule (added literal character escaping and end-anchoring).

Jim

jcmiras

3:52 am on May 5, 2005 (gmt 0)

10+ Year Member



Thanks,
Could you please explain what does these additional rule do. And if my existing code is insufficient, why is it working fine, except for this google indexing thing?

Please bear with me because i`m new in this mod rewrite stuff.

Thanks again.

jdMorgan

4:09 am on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let's work through an example:

You have a link on your page to www.example.com/phil4.htm

Somebody clicks that link, so their browser requests www.example.com/phil4.htm from your server.

You use mod_rewrite to convert (internally rewrite) that to a call to your script, using the path /dir/index.php?cat=4

So, the correct content is delivered as if it came from a static page at www.example.com/phil4.htm

However, in the past, you used to have direct links such as www.example.com/dir/index.php?cat=4 on your pages, so these URLs have been listed in the search results, and now you want to get rid of them.

So, a search engine requests www.example.com/dir/index.php?cat=4 from your server.

The new rule redirects this request using a 301-Moved Permanently redirect to www.example.com/phil4.htm

So, the search engine re-requests the page from www.example.com/phil4.htm

Then the sequence shown for the user clicking on a static link is executed as above.

The result is that the search engine sees a 301 redirect, which tells it to update the URL it is using -- from your old dynamic link to the new static link.

Jim

jcmiras

5:15 am on May 5, 2005 (gmt 0)

10+ Year Member



thanks, now i understand what you are wanted to point out but when i paste your code, it causes a server error. Maybe there`s some minor mismatch with my URL pattern to your code. So to be more straightforward and i will be very glad if you will grant my request, the following is the URL that i am working to;

(www.mydomain.kom/dir/index.php?cat=143).

what shoud be the exact code to make it,

(www.mydomain.kom/phil143.htm)

note: 143 can be any number

without having any problem again.

Thanks in advance.

jd01

6:39 am on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi jcmiras,

Sorry I misunderstood your original question.

You should simply need to add the /dir/ to the path in the rewrite condition (and most likely the rule), like this:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /dir/index\.php\?cat=([0-9]+)\ HTTP/

This is how your entire file would look after making this change:

# Existing rewrite
RewriteRule ^phil(.*)\.htm$ /index.php?cat=$1 [L]
# New external redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /dir/index\.php\?cat=([0-9]+)\ HTTP/
RewriteRule ^dir/index\.php$ /phil%1.htm [R=301,L]

If your .htaccess file is in the root directory, you will need the full path dir/index\.php as is in the rule now. If your .htaccess file is in the directory /dir/ you will need to remover the preceding dir/ from the rule.

Something to note in looking at your original request.

This rewrite will only work for phil$cat, *not* for addurl, or any other requested URL, because every request that ends in?cat=something, will be written to phil$cat.html, and there is no condition that can account for a request that ends in?action=reclink&cat=$cat. You can make adjustments to this either through the use of regular expressions, or unique individual conditions... which is better will depend on your specific situation.

Hope this helps.

Justin

* Please, note I do not see the reason for the server error... the condition not being exact *should* have caused it to fail, but not generate a server error. If you still have the server error after making the adjustment, please continue posting.

Added: You may also need to prepend the original rewrite rule with dir/ on the right side. I am assuming you will not, but I do not know your exact structure, so I cannot say for certain... Obviously if it works now, don't make any changes.

jcmiras

9:30 am on May 5, 2005 (gmt 0)

10+ Year Member



Thanks but its so sad to say that it doesnt work. It again causes an internal server error.

Do you think there is a problem in the syntax? anyway, what does, ^[A-Z]{3,9}, do in;

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} dir/index\.php\?cat=([0-9]+)\ HTTP/

jd01

10:05 am on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are still using the rewrite base that you noted in your 2nd post, I believe that is the issue...

If I am understanding correctly (this is not something I use) the syntax for the base should be /dir not /dir/.

I also believe the rewrite condition must contain the / prior to dir (or index).

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /dir/index\.php\?cat=([0-9]+)\ HTTP/

This [A-Z]{3,9} matches any capital letter from A to Z where there are 3 to 9 letters in a string. It is used to match the from of the request. EG GET, POST, etc.

As far as the error goes, I am just trying to give you some ideas... Either Jim or sitz can give you more detailed information, because RewriteBase is *not* something I use.

Hope this helps.

Justin

jcmiras

1:49 pm on May 5, 2005 (gmt 0)

10+ Year Member



Take a look at this,

No more internal server error when i remove, (^[A-Z]{3,9}) in
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /dir/index\.php\?cat=([0-9]+)\ HTTP/

however, instead of having

(www.domain.kom/phil4.htm)

from
(www.domain.kom/dir/index.php?cat=4),

I got,

(www.domain.kom/phil4.htm?cat=4)

it seems that it only overwrites the first few terms.

jdMorgan

1:57 pm on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I left out a required escape character ("\") preceding the space:

# Existing rewrite
RewriteRule ^phil(.*)\.htm$ /index.php?cat=$1 [L]
# New external redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,[b]9}\[/b] /dir/index\.php\?cat=([0-9]+)\ HTTP/
RewriteRule ^dir/index\.php$ /phil%1.htm [R=301,L]

When you get a server error, examine your server error log -- It will often tell you exactly what is wrong.

Jim

jcmiras

2:24 pm on May 5, 2005 (gmt 0)

10+ Year Member



OK, we`re almost there,
no more server error BUT i still have this problem,

instead of having

(www.domain.kom/phil4.htm)

from
(www.domain.kom/dir/index.php?cat=4),

I got,

(www.domain.kom/phil4.htm?cat=4)

it seems that it only overwrites the first few terms.

It justs replace /dir/index.php with phil4.htm.

what i want is replace dir/index.php?cat=4 with
phil4.htm.

Thanks for the help guys.

jdMorgan

8:31 pm on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not my day for accuracy, I guess. Add a "?" to the substitution URL to clear the query string:

# Existing rewrite
RewriteRule ^phil(.*)\.htm$ /index.php?cat=$1 [L]
# New external redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /dir/index\.php\?cat=([0-9]+)\ HTTP/
RewriteRule ^dir/index\.php$ /phil%1.ht[b]m?[/b] [R=301,L]

Jim

jcmiras

10:24 pm on May 5, 2005 (gmt 0)

10+ Year Member



Your great guys!

It works!

Thanks a lot!