Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule Problem

how can resolve images,js,css correctly affer redirecting

         

FlashPack

2:42 am on Apr 28, 2010 (gmt 0)

10+ Year Member



Hello
i have a simple Rewrite rule that converts for example :


site.com/articles/5 to site.com/index.php?view=articles&art_id=5

RewriteRule ^articles/([0-9]+)$ index.php?view=article&art_id=$1 [L]

the problem is after redirecting... all the images and css .... redirects to /articles/
eg : <img src="images/img.gif"> but the browser reads it like this : articles/images/img.gif

how can i make the browser redirect only the header

jdMorgan

3:38 am on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Link to <img src="/images/img.gif"> and not to <img src="images/img.gif">

It is the client (e.g. the browser) that resolves page-relative links on your pages to the full URLs that it needs to request in order to load included objects such as images, css, and external JavaScript files. It does so based upon the "directory-path" that it sees in its address bar.

So since you have introduced a "fake" /article subdirectory into the page's URL as seen by the browser, it is correctly making requests for the relatively-linked objects on that page in that subdirectory.

So basically, you need to change those page-relative included-object links to use server-relative links (add a leading slash) or use canonical links (e.g. <img src="http://www.example.com/images/img.gif"> ).

You could also use links like <img src="../images/img.src"> or add a rewriterule to 'strip' any subdirectory paths off of incoming image and css requests, but I cannot recommend either of these methods as they do not fix the real problem and can cause additional problems themselves.

[added] P.S. You are not "redirecting" here, as stated in the title. You are doing an internal URL-to-filepath translation (an internal rewrite), not an external URL-to-URL translation (an HTTP client redirect). Be sure you understand the distinction, as this is important to avoid errors that can be fatal to your business because of their effects on search engine indexing and ranking. [/added]

Jim

FlashPack

12:45 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



Thanks Jim .. your tip worked
but
do search engines consider this as double content ?
also .. wouldn't "redirection" affect the loading speed ?

that's confusing ....
could you please tell me what webmasterworld do to achieve this webmasterworld.com/apache/4123342.htm without any harm

thanks

g1smd

1:35 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



They use a rewrite exactly like the one posted here.

One additional step you can take to remove the Duplicate Content problem, is simply to redirect incoming external client requests for URLs containing parameters to the new URL without parameters. This alerts searchengines to update the list of valid URLs they hold for your site.

The redirect complements the rewrite. Be sure you understand what they each do.

jdMorgan

4:23 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So to prevent duplicate-content problems between the 'static' SEO-friendly URL-path and the old dynamic URL-path (which should now be consdered and treated only as an internal filepath), you'd use an additional rewriterule to implement a *redirect* if/when a client requests the dynamic URL:

# Externally redirect direct client requests for dynamic article URL-paths to SEO-friendly URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index\.php)?\?view=article&art_id=([0-9]+)(#[^\ ]*)?\ HTTP/
RewriteRule ^(index\.php)?$ http://www.example.com/articles/%2 [R=301,L]
#
# Internally rewrite requests for SEO-friendly URL-paths to the index.php script with query string
RewriteRule ^articles/([0-9]+)$ index.php?view=article&art_id=$1 [L]

Note that I made "index.php" optional in the redirect: Client requests for both /index.php?view=article&art_id=<article-number> and /?view=article&art_id=<article-number> URL-paths will be redirected to http://www.example.com/articles/<article-number>

The sub-pattern following the query-string-matching subpattern in the RewriteCond allows for "URL fragments" or "named anchors" on your HTTP pages, as in '<a href="#TOP">Back to top of page</a>'

THE_REQUEST is the entire client request line, as seen in quotes in your raw server logs file, e.g.
GET /index.php?view=article&art_id=123 HTTP/1.1


Testing THE_REQUEST as done here prevents this redirect and the rewrite from countermanding each other and resulting in an 'infinite' rewrite/redirect loop.

Jim

FlashPack

5:56 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



sorry Jim but the dynamic url doesn't redirect to the static one

jdMorgan

11:18 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure exactly what you mean by that terse post, but in the interest of saving time...

1) Delete your browser cache and re-test.
2) Check my spelling of your URLs.

Jim

FlashPack

1:09 am on Apr 29, 2010 (gmt 0)

10+ Year Member



index.php?view=articles&art_id=123 doesn't redirect to /articles/123
the spelling is correct
what's wrong ?

jdMorgan

1:02 pm on Apr 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know. That's the bog-standard code there, and works on all my sites (and doubtless thousands more whose Webmasters have found it in previous threads posted here -- or copied from here).

If you did indeed completely delete your browser cache to prevent your browser from showing you stale previously-cached server responses, then it's likely a silly typo in what I posted. And hopefully, g1smd or another contributing member will come along shortly and spot it... :)

You might also try disabling MultiViews and AcceptPathInfo, unless your site requires them to work.
 Option -MultiViews
AcceptPathInfo Off

Note that the Options setting can be combined with any existing Options already set in your file, e.g.
 Options +FollowSymLinks -Indexes -MultiViews 


Jim

FlashPack

9:22 pm on Apr 30, 2010 (gmt 0)

10+ Year Member



RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index\.php)?\?view=articles&art_id=([0-9]+)(#[^\ ]*)?\
RewriteRule ^(index\.php)?$ http://www.example.com/articles/%2 [R=301,L]

i hope someone find what's wrong on these lines
the spelling is correct
and i tried also the following but it didn't work

RewriteRule ^index.php?view=articles/([0-9]+)$ http://www.example.com/articles/%1 [R=301,L]

jdMorgan

9:41 pm on Apr 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What URL are you testing the code above with? What are you typing into the address bar to test it?

Jim

FlashPack

9:48 pm on Apr 30, 2010 (gmt 0)

10+ Year Member



i type :
http://localhost/site/index.php?view=articles&art_id=123

my .htaccess :

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index\.php)?\?view=articles&art_id=([0-9]+)\ HTTP/
RewriteRule ^(index\.php)?$ http://localhost/site/articles/%2 [R=301,L]

RewriteRule ^articles/([0-9]+)(.*)\.html$ index.php?view=articles&art_id=$1 [L]

[edited by: jdMorgan at 11:35 pm (utc) on Apr 30, 2010]
[edit reason] De-linked [/edit]

g1smd

10:18 pm on Apr 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your test URL includes "site/" which is not in your pattern matching tests.

It will not match and the rule will not run.

FlashPack

10:59 pm on Apr 30, 2010 (gmt 0)

10+ Year Member



thanks g1smd .. by adding "site/" it redirects but not correctly
it redirects to
http://localhost/site/article/?view=article&art_id=1



RewriteCond %{THE_REQUEST} ^[A-Z]+\ /site/(index\.php)?\?view=article&art_id=([0-9]+)(#[^\ ]*)?\ HTTP/
RewriteRule ^(index\.php)?$ http://localhost/site/article/$2 [R=301,L]

[edited by: jdMorgan at 11:26 pm (utc) on Apr 30, 2010]
[edit reason] De-linked [/edit]

g1smd

11:26 pm on Apr 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Clear the query string from being re-added by adding a question mark to the end of the target URL.

jdMorgan

11:31 pm on Apr 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... and the back-reference should be to %2, not $2

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /site/(index\.php)?\?view=article&art_id=([0-9]+)(#[^\ ]*)?\ HTTP/
RewriteRule ^(index\.php)?$ http://localhost/site/article/[b]%2?[/b] [R=301,L]

From the form of this rule, I assume that the code is located in /site/.htaccess.

Jim

FlashPack

1:04 am on May 1, 2010 (gmt 0)

10+ Year Member



thanks guys .... finally it worked