Forum Moderators: phranque

Message Too Old, No Replies

Problem with rewrite rules

Problem with rewrite rules

         

wicksoft

2:07 pm on Aug 23, 2011 (gmt 0)

10+ Year Member



I have a problem in after creating SEO friendly links in my site.
here is the .htaccess file codes.

RewriteEngine On
RewriteRule ^www/category/([0-9]+)/(.*?).html$ www/product/productlist.php?category=$1 [L]


preciously i have this link
http ://www.mysite.com/www/product/productlist.php?category=00136
now SEO link is this
http ://www.mysite.com/www/category/00136/aprons.html

this is working okay.

and the issue im having is after the page loads all other site links also changed.
and those links are having these extra lines(www/category/) in the url now
how to avoid this thing.

site normal links like
http ://www.mysite.com/www/content/index.php
now link shows
http ://www.mysite.com/www/category/content/index.php


how to avoid adding these folder names - www/category/


other links are having short url like this-
<a href='../../www/content/index.php'

Thanks
im realy appreciate valuable response for this

lucy24

9:25 pm on Aug 23, 2011 (gmt 0)

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



Before anything else: in all htaccess examples, use the exact domain name www.example.com so you can show exactly what your code says. All other domain names turn into auto-links unless you intentionally mess with them-- and then it becomes impossible to tell if your code is wrong or if you changed it on purpose.

Right now, your htaccess appears to be doing exactly what you are telling it to do. You may have left out some RewriteConditions.

There are three different things:

the real, physical path to a page
the address the user sees in their browser's address bar
the address the user typed or clicked to get there

Sometimes two or more of these are the same. Sometimes all three are different. So start by figuring out which one you're talking about.

g1smd

12:44 am on Aug 24, 2011 (gmt 0)

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



Use example.com in this forum

This is not a particularly friendly "SEF" URL:

http://www.example.com/www/category/00136/aprons.html 


There are several redundant parts, not least the .html extension.

Ensure that on the pages of your site all links begin with a leading slash.
Never use the ../../ notation.

Escape literal periods in patterns.

Only use (.*) in a pattern if there is nothing else following it. The (.*) means "grab the entire input from here to the very end".

wicksoft

9:50 am on Aug 24, 2011 (gmt 0)

10+ Year Member



ok thanks for the feedback..
anyway now i realize all shortened static links of the site need to change from the beginning. so as a starting guidance what things i need to do first for those link starting with ../../ notation.
how to add leading slash means how to know the starting point
i think this rule is same for the image path aswell..

wicksoft

9:54 am on Aug 24, 2011 (gmt 0)

10+ Year Member



@lucy24..

you right i don’t know about RewriteConditions. Please help me to do that..
actually i want to work only the link appeared on the browser address bar. which is the second one in the list u mentioned.
(the address the user sees in their browser's address bar )

Thanks again

lucy24

11:02 am on Aug 24, 2011 (gmt 0)

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



and the issue im having is after the page loads all other site links also changed.
and those links are having these extra lines(www/category/) in the url now
how to avoid this thing.

This is only indirectly an htaccess problem. There's a pun lurking in there, if only I could drag it out. The problem is that you are rewriting, not redirecting. The moment you do this, you have created two urls.

One is the original url, from before the rewrite. This is what appears in the browser's address bar. And it is used by all relative links-- not just links to other pages, but to images, css and so on.

The other url is the page the user is physically looking at. If it's in a different directory than the one they originally requested, links are the least of your problem. The images, styles and everything else have also gone missing-- unless you use absolute links for all of them.

  * * *
This is why links should always be domain-absolute. Do as I say, not as I do.

#1. A domain-absolute link has a leading slash / and means "start from the top". So <a href = "/blahblah.html"> will always take you to www.example.com/blahblah.html, no matter where in the domain you are when you click on the link.

#2. Second choice is the relative link beginning with a name. <img src = "images/blahblah.png"> is the equivalent of "down the hall and to your left". Saves clutter and is safe so long as each directory is kept as a package and you don't rewrite anything.

#3. The Truly Dangerous Link is the relative link beginning in ../ especially multiple layers like ../../ Then you have to keep track of both your current location and the location of the file you're linking to. Great for viewing local files on your browser, though.

wicksoft

11:37 am on Aug 24, 2011 (gmt 0)

10+ Year Member



okay so...
for #1. when giving absolute path with leading slash from the root. site is not working on local development environment. Because its root path is different. and pages not linking to proper path like in live server. this is a big problem im having…

so for that i start to use html Base tag in the header. because I want to keep variable this root location in same package when I run the site in my development local path when doing site updates. so Im thinking to change value in this Base tag only when I move into live site from the development local site and wise versa. is that a good thing? so far im changin the static path accordingly and also matching for the absolute path stating from the value having on HTML Base tag. is this what u r talking in #2 ?

Thanks again

wicksoft

11:49 am on Aug 24, 2011 (gmt 0)

10+ Year Member



Hi lucy24,
I got little confused here …

This is only indirectly an htaccess problem. There's a pun lurking in there, if only I could drag it out. The problem is that you are rewriting, not redirecting. The moment you do this, you have created two urls.


you mean I need to do page redirection work not rewriting? if so please tell me how to do it in htaccess file

I want to redirect url comming like
http ://www.example.com/www/category/00136/aprons.html

into this location
http ://www.example.com/www/product/productlist.php?category=00136

g1smd

7:11 pm on Aug 24, 2011 (gmt 0)

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



I want to redirect URL coming like
http://www.example.com/www/category/00136/aprons.html
into this location
http://www.example.com/www/product/productlist.php?category=00136
I suspect that you don't want to do that at all.

You need to think about URLs (used out on the web) and paths and files (used inside the server).

I think you want to rewrite requests for this URL:
http://www.example.com/www/category/00136/aprons.html

to this internal path:
/www/product/productlist.php?category=00136

to serve the content.

lucy24

8:33 pm on Aug 24, 2011 (gmt 0)

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



when giving absolute path with leading slash from the root. site is not working on local development environment.

Yes, that's the insurmountable problem. (How do grownups deal with it? I have no idea.) To make a leading slash work locally on my computer, I would have to put each top-level directory or file at the very top level of the HD, alongside "Applications" and "Library" and so on, instead of inside /users/{me}/Documents/et cetera. Technically this should not be a problem because nobody else has access to the computer, but the idea still gives me the willies. And it would make a ### mess.

Same goes for trailing slashes marking directories. The server appends /index.html, but my browser used locally can only deal with explicit filenames. You can see the result in your logs, where newly arrived robots always visit your front page twice: once as www.example.com, and later (following links) as www.example.com/index.html

:: wandering off to experiment with aliases ::

wicksoft

7:33 am on Aug 25, 2011 (gmt 0)

10+ Year Member



when giving absolute path with leading slash from the root. site is not working on local development environment.

Hi lucy24,
thanks for the details u gave...im not well in server coding and linking..
To distinguish the different web root environment I used html Base tag in html header as mentioned in another forum.
so far it is working okay. cos in my site I have only single header block file calling in all pages. so when I move into local environment I just change this Base tag value to proper path. then site start to work.

so im removing ../../ these tags and adding absolute path for all static links now so far it is good. lets see if this gimmik works or not for all links...

----------------------
I think you want to rewrite requests for this URL:
http://www.example.com/www/category/00136/aprons.html
to this internal path:
/www/product/productlist.php?category=00136
to serve the content.

Hi g1smd,
yes that’s wht I need to do...

so i add this htaccess code and start working if it is wrong pls correct me..
RewriteEngine On
RewriteRule ^www/category/([0-9]+)/(.*?).html$ www/product/productlist.php?category=$1 [L]


thanks for all.....

g1smd

8:38 am on Aug 25, 2011 (gmt 0)

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



You need to escape the literal period in the RegEx pattern.

wicksoft

12:08 pm on Aug 25, 2011 (gmt 0)

10+ Year Member



ok so what is the RewriteCondition code for this i need to use ?
also anyone know a good tutorial link need to folow up?

lucy24

7:43 pm on Aug 25, 2011 (gmt 0)

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



Start by bookmarking this page [httpd.apache.org] and reading it slowly. Note that the page TOC is in alphabetical order, not conceptual. You should start with the RewriteRule and RewriteCond sections.

There's really no alternative to testing unfamiliar* rules. Add a temporary extra Condition so innocent bystanders won't get pulled into the Rule before it is ready. (I do it by simply constraining the UA to Camino. But you can do it just as easily by constraining the rule to some specific element in the request, like saying it has to contain the sequence "qaj". Or anything you make up that wouldn't really occur.)


* Sometimes even familiar ones. How was I to guess that two utterly benign Deny From {IP} directives would interact with each other, causing my logs to go haywire?

g1smd

8:00 pm on Aug 25, 2011 (gmt 0)

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



If you use a browser that allows the User Agent to be customised then you set it to anything you want, and you can then be absolutely sure that no-one else will ever see things before they are ready.