double page load with mod_rewrite

New to mod_rewrite? This is a good place to start.

double page load with mod_rewrite

Postby pushpinderbagga » Thu Jul 02, 2009 10:28 am

hello... I am currently facing an issue here...

the pages on my site whose url's are re written with mod_rewrite tend to load two times each times that page is requested...

I am using firefox and this is the website

Code: Select all
http://www DOT okidda DOT com


please help... this might be hurting my SEO by giving the search engine double pages for single link
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

probable cause

Postby pushpinderbagga » Thu Jul 02, 2009 10:33 am

I have the below code in my htaccess... is this the cause of the problem mentioned ?

Code: Select all
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]


what does the above code actually do ?
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Postby richardk » Fri Jul 03, 2009 9:17 am

what does the above code actually do ?

Code: Select all
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

redirects example.com/index.php and www.example.com/index.php to www.example.com.

Code: Select all
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

redirects example.com/* to www.example.com/*.

They should not cause this problem. Do you have more mod_rewrite?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

my htaccess file pasted below

Postby pushpinderbagga » Fri Jul 03, 2009 12:36 pm

hi richardk ... below pasted is my htaccess file... please tell me if there is any mistake that I am doing


Code: Select all
# Set up mod_rewrite
Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteRule ^.+(/images/.+)$ $1 [L]
RewriteRule ^.+(/thumb/.+)$ $1 [L]
RewriteRule ^.+(/pictures/.+)$ $1 [L]
RewriteRule ^.+(/js/.+)$ $1 [L]
RewriteRule ^.+(/files/.+)$ $1 [L]
RewriteRule ^.+(/css/.+)$ $1 [L]

RewriteRule ^login/?$ login.php [NC]
RewriteRule ^submit/?$ submit.php [NC]
RewriteRule ^approve/?$ approve.php [NC]
RewriteRule ^redirect/?$ redirect.php [NC]
RewriteRule ^admin/commentsRej/?$ commentsRej.php [NC]
RewriteRule ^admin/comments/?$ comments.php [NC]
RewriteRule ^sitemap\.html$ sitemapHTML.php [NC]
RewriteRule ^faq/?$ faq.php [NC]
RewriteRule ^policy/?$ policy.php [NC]
RewriteRule ^about/?$ about.php [NC]
RewriteRule ^all/latest/?$ latest.php [NC]
RewriteRule ^contact/?$ contact.php [NC]

RewriteRule ^page([0-9]+)$ index.php?page=$1[NC]
RewriteRule ^page([0-9]+)/?$ index.php?page=$1[L,NC]

RewriteRule ^([a-zA-Z0-9_-]+)$ category.php?category=$1 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ category.php?category=$1 [L,NC]

RewriteRule ^user/([a-zA-Z0-9_-]+)$ user.php?user=$1 [NC]
RewriteRule ^user/([a-zA-Z0-9_-]+)/?$ user.php?user=$1 [L,NC]

RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/([a-zA-Z0-9_-]+)\.html$ story.php?id=$2 [L,NC]

RewriteRule ^tag/([a-zA-Z0-9_-]+)$ tag.php?tag=$1 [NC]
RewriteRule ^tag/([a-zA-Z0-9_-]+)/?$ tag.php?tag=$1 [L,NC]

RewriteRule ^rss/category/([a-zA-Z0-9_-]+)\.xml$ categoryRss.php?category=$1 [L,NC]

RewriteRule ^example.xml$ rss.php [L,NC]

RewriteRule ^rss/user/([a-zA-Z0-9_-]+)\.xml$ userRss.php?user=$1 [L,NC]

RewriteRule ^([a-zA-Z0-9_-]+)/page([0-9]+)$ category.php?category=$1&page=$2[NC]
RewriteRule ^([a-zA-Z0-9_-]+)/page([0-9]+)/?$ category.php?category=$1&page=$2[L,NC]

RewriteRule ^all/latest/page([0-9]+)$ latest.php?page=$1[NC]
RewriteRule ^all/latest/page([0-9]+)/?$ latest.php?page=$1[L,NC]

RewriteRule ^admin/comments/page([0-9]+)$ comments.php?page=$1[NC]
RewriteRule ^admin/comments/page([0-9]+)/?$ comments.php?page=$1[L,NC]

RewriteRule ^admin/commentsRej/page([0-9]+)$ commentsRej.php?page=$1[NC]
RewriteRule ^admin/commentsRej/page([0-9]+)/?$ commentsRej.php?page=$1[L,NC]

RewriteRule ^user/([a-zA-Z0-9_-]+)/page([0-9]+)$ user.php?user=$1&page=$2[NC]
RewriteRule ^user/([a-zA-Z0-9_-]+)/page([0-9]+)/?$ user.php?user=$1&page=$2[L,NC]

RewriteCond %{REQUEST_FILENAME} .*jpg$ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !google\. [NC]
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteCond %{HTTP_REFERER} !live\. [NC]
RewriteCond %{HTTP_REFERER} !yahoo\. [NC]
RewriteCond %{HTTP_REFERER} !msn\. [NC]
RewriteCond %{HTTP_REFERER} !bing\. [NC]
RewriteCond %{HTTP_REFERER} !answers\. [NC]
RewriteCond %{HTTP_REFERER} !blogger\. [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC]
RewriteRule \.(jpg)$ http://www.example.com/thumb_default.gif [R,L]
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

double reload problem

Postby pushpinderbagga » Fri Jul 03, 2009 12:39 pm

if you see there

the category tag and story page are giving a double reload
if you use firefox and open my website http://www DOT example DOT com browse through story pages and you will notive that the pages are pinged once the link is clicked and then again the load on to the same page

it happens for the category page story page and the tag page
Last edited by pushpinderbagga on Fri Jul 03, 2009 3:30 pm, edited 1 time in total.
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

javascript file not working

Postby pushpinderbagga » Fri Jul 03, 2009 3:29 pm

also

my javascript file is not working on the page given below

http://www.example.com/movies/123/storytitle.html

its story.php?id=911 where it works but not on the rewritten url which was done as below

Code: Select all
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/([a-zA-Z0-9_-]+)\.html$ story.php?id=$2 [L]


I have put a base href and also added the below code in my htaccess file

Code: Select all
RewriteRule ^.+(/js/.+)$ $1 [L]


no matter how I put the js file reference in my story.php code its doesnt work... amazingly the css files are picked up also the images etc... its jus this js file that is giving an error...

Code: Select all
<script type="text/javascript" src="/js/functions.js"></script>
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Postby richardk » Sat Jul 04, 2009 11:50 am

below pasted is my htaccess file... please tell me if there is any mistake that I am doing

You are missing a few spaces before the flags.
You do not need two rules if your have /?, it means an optional slash.
You should use the L flag when a rule matches and needs no further mod_rewrite processing.
You can combine many of the rules.

None of that should cause this problem, though.

Try
Code: Select all
Options All
# Disable Directory Browsing
Options +FollowSymLinks -Indexes

# Set up mod_rewrite
RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

RewriteCond %{HTTP_REFERER} !^(http://(www\.)?example.com/.*)?$ [NC]
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteCond %{HTTP_REFERER} !(answers|bing|blogger|google|live|msn|yahoo)\. [NC]
RewriteRule \.(jpg)$ http://www.example.com/thumb_default.gif [R,L]

RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
RewriteRule .* http://www.%1%{REQUEST_URI} [R=301,L]

RewriteRule ^.+(/(css|files|images|js|pictures|thumb)/.+)$ $1 [L]

RewriteRule ^(about|approve|contact|faq|login|policy|redirect|submit)/?$ /$1.php [NC,L]
RewriteRule ^admin/comments(Rej)?/?$ /comments$1.php [NC]
RewriteRule ^sitemap\.html$ /sitemapHTML.php [NC,L]
RewriteRule ^all/latest/?$ /latest.php [NC,L]
RewriteRule ^page([0-9]+)/?$ /index.php?page=$1 [NC,L]
RewriteRule ^([-a-z0-9_]+)/?$ /category.php?category=$1 [NC,L]
RewriteRule ^user/([-a-z0-9_]+)/?$ /user.php?user=$1 [NC,L]
RewriteRule ^[-a-z0-9_]+/([0-9]+)/[-a-z0-9_]+\.html$ /story.php?id=$1 [NC,L]
RewriteRule ^tag/([-a-z0-9_]+)/?$ /tag.php?tag=$1 [NC,L]
RewriteRule ^rss/category/([-a-z0-9_]+)\.xml$ /categoryRss.php?category=$1 [NC,L]
RewriteRule ^example\.xml$ /rss.php [NC,L]
RewriteRule ^rss/user/([-a-z0-9_]+)\.xml$ /userRss.php?user=$1 [NC,L]
RewriteRule ^([-a-z0-9_]+)/page([0-9]+)/?$ /category.php?category=$1&page=$2 [NC,L]
RewriteRule ^all/latest/page([0-9]+)/?$ /latest.php?page=$1 [NC,L]
RewriteRule ^admin/comments(Rej)?/page([0-9]+)/?$ /comments$1.php?page=$2 [NC,L]
RewriteRule ^user/([-a-z0-9_]+)/page([0-9]+)/?$ /user.php?user=$1&page=$2 [NC,L]


the category tag and story page are giving a double reload
if you use firefox and open my website http://www DOT example DOT com browse through story pages and you will notive that the pages are pinged once the link is clicked and then again the load on to the same page

it happens for the category page story page and the tag page

I see the problem, but i'm not sure mod_rewrite is the cause. Does this happen in any other browser?

my javascript file is not working on the page given below

It seems to work fine for me. Relative paths is not the problem, as /js/functions.js is only relative to the domain, not the browser URL.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

thanks and corrections

Postby pushpinderbagga » Sat Jul 04, 2009 4:13 pm

@ richardk ... many thanks.

everything is working fine with the code you supplied... cheers !

I was wondering about if we have to put and end tag to the below code ?

Code: Select all
<filesmatch "\.(jpg|gif|png|css|js)$">


also weather the below redirect is a Permanent, Temp or Seeother

Code: Select all
RewriteRule ^(about|approve|contact|faq|login|policy|redirect|submit)/?$ /$1.php [NC,L]


as in... should'nt it be written as

Code: Select all
RewriteRule Permanent ^(about|approve|contact|faq|login|policy|redirect|submit)/?$ /$1.php [NC,L]


pardon me if I am asking silly questions :-(
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Postby richardk » Mon Jul 06, 2009 12:00 pm

everything is working fine with the code you supplied... cheers !

It still seems to double load for me.

I was wondering about if we have to put and end tag to the below code ?
Code: Select all
<filesmatch "\.(jpg|gif|png|css|js)$">

Yes, it's like XML.
Code: Select all
<FilesMatch "\.(jpg|gif|png|css|js)$">
  # Do stuff to css, image and JavaScript files.
</FilesMatch>


also weather the below redirect is a Permanent, Temp or Seeother
Code: Select all
RewriteRule ^(about|approve|contact|faq|login|policy|redirect|submit)/?$ /$1.php [NC,L]

It's none. The processing happens inside Apache (an internal "rewrite"), the browser knows nothing about it.

as in... should'nt it be written as
Code: Select all
RewriteRule Permanent ^(about|approve|contact|faq|login|policy|redirect|submit)/?$ /$1.php [NC,L]

Don't confuse mod_rewrite and mod_alias (Redirect permanent). In mod_rewrite redirects have the R flag (R=301 for permanent), for example
Code: Select all
RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
RewriteRule .* http://www.%1%{REQUEST_URI} [R=301,L]

and the address bar of the browser changes from example.com to www.example.com.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby pushpinderbagga » Mon Jul 06, 2009 12:43 pm

It still seems to double load for me.


richardk... I know I am loosing SEO for this double load for me... any clue why its happening...

surely you can try

example.com/story.php?id=123

instead of

example.com/photos/123/storytitle.html

SURPRISINGLY BOTH GIVE A DOUBLE RELOAD
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 116 guests

cron