Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite not working as expected!

         

youngbobby

7:47 am on Jun 5, 2010 (gmt 0)

10+ Year Member



Hi,
I'm quite new to this forum and still a noob with mod_rewrite. I've used the code below to try setup friendly url(s) but for some reasons, it doesn't work as expected. Once page loads fully, it still displays "non-friendly" url. Is there anything i am not doing right?

Thanks for reading.

Code Below:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteRule ^categories/([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)/?$ http://example.net/Site/viewCategory.php?page=$1&id=$2&catname=$3 [NC,L]
RewriteRule ^monitor,([A-Za-z0-9-]+)\.php$ http://example.net/Site/track.php?id=$1 [NC,L]
RewriteRule ^Banner,_([^-]+)\.php$ http://example.net/Site/Banner.php?id=$1 [NC,L]
RewriteRule ^Count,_([^-]+)\.php$ http://example.net/Site/Counter.php?id=$1 [NC,L]
RewriteRule ^search,([^-]+)\.php$ http://example.net/Site/search.php?q=$1 [NC,L]
RewriteRule ^view/([0-9]+)/(.*)/?$ http://example.net/Site/viewSiteDetails.php?id=$1&sitename=$2 [NC,L]
#RewriteRule ^view/([0-9]+)/(.*)/?$ http://example.net/Site/viewSiteDetails.php?id=$1 [NC,L]
RewriteRule ^Directory([^-]+)\.php$ http://example.net/Site/Directory.php?page=$1 [NC,L]
RewriteRule ^index/([^-]+)\.php$ http://example.net/Site/index.php?&id=$1 [NC,L]
RewriteRule ^eXit/([^-]+)\.php$ http://example.net/Site/eXit.php?&id=$1 [NC,L]

jdMorgan

6:13 pm on Jun 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you hover over a link on one of your pages, what does it say? -- Is it a 'friendly' URL or not?

If not, that is the problem. This is a two-step process, consisting of changing the links on your pages to be the 'friendly' ones, followed by implementing code like you posted above to 're-connect' the friendly link URLs back to your actual script_filepath_plus_query_strings inside the server.

A third and optional step is to detect direct client requests for non-friendly URLs, and redirect them to the friendly ones. This is not possible using mod_rewrite with URLs like the ones you show above, but can be done by rewriting such requests to a script, which can then use the unfriendly URL to look up the friendly URL in your database and generate a 301 redirect to that friendly URL.

The code above could use some improvement, but let's take care of the major issue first: Have you edited your pages or modified the script that generates those pages to publish 'friendly' URLs, or not? This will be required, unless you want to get/purchase an off-the-shelf plug-in for your CMS to do it for you.

Jim

youngbobby

8:39 pm on Jun 5, 2010 (gmt 0)

10+ Year Member



Hey JD

Many thanks for your reply. Like i said earlier, i'm still only a newbie with all this but this seems to be the major issue with my site's functionality. I've done a toplist/traffic manager (self programmed)

Now back to the main issue. With that code on my .htaccess file and uploaded under the Site directory. I linked to the pages (in a friendly way) following the rule i have above. When i hover over the link, the url appears as friendly but when the page is on the verge of displaying it returns the url back to the unfriendly way. I hope you get my point?

Thanks.

jdMorgan

8:44 pm on Jun 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then the major porblem may be that you have coded 302 redirects (because the syntax of your rules includes the protocol and hostname). For example, your first rule

RewriteRule ^categories/([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)/?$ [b]http://example.net[/b]/Site/viewCategory.php?page=$1&id=$2&catname=$3 [NC,L]

should be

RewriteRule ^categories/([0-9]+)/([0-9]+)/([A-Za-z0-9\-]+)/?$ /Site/viewCategory.php?page=$1&id=$2&catname=$3 [L]

Jim

youngbobby

11:24 pm on Jun 5, 2010 (gmt 0)

10+ Year Member



Hi jD,

it returns a 404 error :(

g1smd

11:28 pm on Jun 5, 2010 (gmt 0)

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



You might need to remove /Site/ from the target filepath?

You'll need to make the same alterations to all of your rules which perform an internal rewrite function.

youngbobby

11:41 pm on Jun 5, 2010 (gmt 0)

10+ Year Member



Hi,

sorry JD; Page was cached the first time after typo error. Your advice works like magic.

Thanks alot JD I appreciate.

ThumbsUp!

youngbobby

11:44 pm on Jun 5, 2010 (gmt 0)

10+ Year Member



Thank you so much g1smd,

You guys have really relieved me alot. I was actually getting frustrated! Now i can smile :)

g1smd

11:57 pm on Jun 5, 2010 (gmt 0)

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



Now install Live HTTP Headers for Firefox, and access each type of URL in turn and check the first HTTP response for each and every one is "200 OK".

If any return "302" or "301" then there is still an error in your code.

Please also confirm you removed NC from your rules and now only have [L] in each one.

jdMorgan

1:16 am on Jun 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's also the issue of that optional trailing slash in some of your rewriterule patterns.

Allowing both "trailing-slash" and "non-trailing-slash" URLs to resolve to the same content creates a duplicate-content issue. You should pick one style of URL or the other. Rewrite only the correct URL to the script, and 301-redirect the incorrect URL to the correct URL.

This prevents having more than one URL return the same page content, effectively making two URLs compete against each other for links and for search ranking.

Jim

youngbobby

4:43 am on Jun 6, 2010 (gmt 0)

10+ Year Member



g1smd, thanks i'm working on that now.

jdMorgan, hey thanks again. Please could you advice on what to edit and maybe what to add?

Thanks.

g1smd

6:13 am on Jun 6, 2010 (gmt 0)

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



^pattern/?$
allows for URL requests for both
example.com/pattern
and
example.com/pattern/
to return the same content.

Change this to
^pattern$
so that only
example.com/pattern
can return content.

Add a preceding rule to redirect only direct client requests for
example.com/pattern/
to
example.com/pattern
to complete the code.

youngbobby

12:27 pm on Jun 6, 2010 (gmt 0)

10+ Year Member



forgive me and what preceeding rule should i add when i want to direct client requests for example.com/pattern/ to example.com/pattern ?

g1smd

4:38 pm on Jun 6, 2010 (gmt 0)

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



It'll be a 301 redirect from
^pattern/$
to
www.example.com/pattern
here.

Use a
RewriteRule
for this, with
[R=301,L]
flags.

youngbobby

5:07 pm on Jun 6, 2010 (gmt 0)

10+ Year Member



Gosh! What a world.

I am trying real hard to implement it wit this code and i keep getting a 404 error. Could you implement it with the code below. Sorry i am becoming more of a pest this days lol.


RewriteRule ^Categories/([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)/?$ /Site/viewCategory.php?page=$1&id=$2&catname=$3 [L]

jdMorgan

3:17 pm on Jun 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Spend the time needed to learn what all of this means. Otherwise, you do a disservice to yourself, and risk the welfare of your own site -- How can you know that your code is correct if you don't understand it? Useful resources are cited in our Forum Charter, with examples and tutorials in our Forum Library.

# Externally redirect to remove unwanted trailing slash from "Categories" URL-paths
RewriteRule ^Categories/([0-9]+/[0-9]+/[A-Za-z0-9-]+)/$ http://www.example.com/Categories/$1 [R=301,L]
#
# Internally rewrite Category URL-path requests to "viewCategory" script filepath with query parameters
RewriteRule ^Categories/([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)$ /Site/viewCategory.php?page=$1&id=$2&catname=$3 [L]

Jim

youngbobby

1:58 pm on Jun 8, 2010 (gmt 0)

10+ Year Member



Many thanks jD and g1smd. I'm 100% okay now.

Guess it's time to study more on mod_rewrite.

Thanks again :-)

g1smd

6:21 pm on Jun 8, 2010 (gmt 0)

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



There's one more thing you might want to add.

After the redirect and before the rewrite, you could add the standard non-www to www canonical redirect code.

It's one more level of ensuring searchengines index your site just right.

youngbobby

5:16 am on Jun 14, 2010 (gmt 0)

10+ Year Member



Okay! I'll add that. Thanks again.