homepage Welcome to WebmasterWorld Guest from 54.226.21.57
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Help needed with mod_rewrite
3 questions from a newbie
Philarmon

10+ Year Member



 
Msg#: 1040 posted 3:12 am on Feb 9, 2004 (gmt 0)

Hi folks!

I want to mod_rewrite my dynamic URLs, just started and tried a bit with it. It took me some time to get it to work, but now i'm stuck on three problems. I hope you can help me out with that:

1. Usually, my url looks like this:
mysite.com/index.php?t=sub_pages&cat=200

Now, the "t" (template) is involved in each URL, and the rest of the parameters can vary. So i did the rewrite this way:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ([^-]*)/([^-]*)/([^-]*)/$ /index.php?t=$1&$2=$3

Works fine. The new URL would be mysite.com/sub_pages/cat/200

But sometimes, i have only the "t" in the URL and no more vars, like this: mysite.com/index.php?t=new
Now, when i try to add this case to the .htaccess, the second case stop working:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ([^-]*)/$ /index.php?t=$1
RewriteRule ([^-]*)/([^-]*)/([^-]*)/$ /index.php?t=$1&$2=$3

I also need to ad one more case because sometimes i have 2 variables attached after the template var.
I understand that there is some interference between the two rules, but how do i fix it?

2. I have noticed that if i do
RewriteRule ([^-]*)/$ /index.php?t=$1
i can't access the real directories on the server anymore, coz Apache thinks that i am trying to access a template. How do i exclude some strings from the rule, so i can access mysite.com/admin , for example?

3. And the last one: I have learned too late about the mod_rewrite and Google has spidered some of my pages with the old (dynamic) URL. Can i get in trouble if i rewrite my URLs to static because of the duplicate content? Should i set up a permanent redirect for the pages that was spidered already?

I know that is a lot, but thank you for your time and any help :)

Best wishes,
Philarmon

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 1040 posted 4:57 am on Feb 9, 2004 (gmt 0)

Philarmon,

1) Put your rules in the order 3 variables, 2 vars, 1 var, and end each with the [L] flag. URLs with 2 vars will fail the initial 3-var test, and fall through to the next RewriteRule, etc. However, once a pattern matches in a rule with the right number of vars, the URL will be rewritten, and then the [L] flag will stop mod_rewrite at that point and the script will be called.

2) I can't tell you what to do about this. Your query-string-format URLs have the "t=" in them that you can key off of, but you haven't shown the format of your static URLs. Therefore, I don't know what you'd use as a key for them.

3) I wouldn't sweat the duplicate content issue too much, unless you have external (outside your site) links to both old and new-format URLs. If so, then yes, 301-redirect the old ones that are externally-linked-to.

Just as a side note, and you're probably already on this track, but your opening statement, "I want to mod_rewrite my dynamic URLs" prompts me to include this:

mod_rewrite runs after an HTTP request is received, but before any content is served or any script is activated. So the usual method to make your site search-engine-friendly is to have your script output static URLs as links and object references (e.g. embedded images SRC links). When these static links are subsequently requested by spiders and browsers, you then use mod_rewrite to convert them internally to the query string format with which you'll call your script. So, actually, it is more usual to rewrite static links to dynamic (from mod_rewrite's perspective). That way, the outside world sees only static links, but your script still receives parameters the way it always has.

There is a solution for the problem of automatically changing dynamic links output by the script to static links, but it involves putting a "wrapper" (another script) around the existing script, so that its output is intercepted and modified before being returned to the requesting client. However, this dynamic-to-static URL conversion function cannot be performed by mod_rewrite after the script has been activated, because mod-rewrite is "finished" before the script ever runs.

Jim

Philarmon

10+ Year Member



 
Msg#: 1040 posted 3:46 pm on Feb 9, 2004 (gmt 0)

1. Thanks a lot, jdMorgan - that works fine :)

2. >but you haven't shown the format of your static URLs
Ok, i mean if i include
RewriteRule ([^-]*)/$ /index.php?t=$1
then my URL would look like mysite.com/new/ and that would mean mysite.com/index.php?t=new
But then, if i would try to access a real folder on my server, eg the admin folder mysite.com/admin , because of this mod_rewrite the server thinks that i am trying to access mysite.com/index.php?t=admin . so how do i exclude some folder names from being mod_rewrited?

>So, actually, it is more usual to rewrite static links to dynamic
Yeah, you're right, i should write "I want to convert my dynamic URL into static" or something :) But the search engines will see only the static URLs, right?

Thanks again,
Philarmon

Philarmon

10+ Year Member



 
Msg#: 1040 posted 3:56 pm on Feb 9, 2004 (gmt 0)

Oh, and in the same go:

I also noticed that if i use mysite.com/new/ it works, but if i try mysite.com/new (without the slash at the end) it doesnt. How can i use both styles? I have tried to do it like this:

RewriteRule ([^-]*)/$ /index.php?t=$1 [L]
RewriteRule ([^-]*)$ /index.php?t=$1 [L]

but it doesnt work.

Also, how do i make a permanent redirect of the old linked dynamic URl to the new static one? For example, i need to point the mysite.com/index.php?t=sub_pages&cat=469 to mysite.com/tutorials/ and then mod_rewrite it to static again to avoid duplicated content. I tried it like this:

RewriteRule ^index\.php?t=sub_pages&cat=469$ /tutorials/ [R=301,L]

before my other RewriteRules with no luck.

Oh man, that are a lot of questions :)
Thanks a lot!

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 1040 posted 5:14 pm on Feb 9, 2004 (gmt 0)

Philarmon,

> then my URL would look like mysite.com/new/ and that would mean mysite.com/index.php?t=new

You're going to have to put something in the static URL that can be used to tell mod_rewrite whether to rewrite the URL or not. The alternative is to create a list including all URLs to be rewritten, or a list including all URLs *not* to be rewritten.

For example, you could make your static URLs include something innocuous such as mysite.com/pages/new and rewrite that to mysite.com/index.php?t=new
In this case, the presence of "pages" in the URL can be used by mod_rewrite to know that the URL is to be rewritten:
RewriteRule ^pages/(.*) /index.php?t=$1 [L]

Alternatively, you can create either an inclusion list or an exclusion list:

Inclusion:

RewriteCond %{REQUEST_URI} ^/page1 [OR]
RewriteCond %{REQUEST_URI} ^/screen1 [OR]
RewriteCond %{REQUEST_URI} ^/sheet33 [OR]
RewriteCond %{REQUEST_URI} ^/leaf9
RewriteRule (.*) /index.php?T=$1 [L]

Exclusion:

RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_URI} !\.(jpe?g¦gif¦css¦js¦php)
RewriteCond %{REQUEST_URI} !^index
RewriteCond %{REQUEST_URI} !^mailex\.cgi$
RewriteRule (.*) /index.php?T=$1 [L]

In order for mod_rewrite to know which URLs to rewrite and which to leave alone, you will have to tell it using one of these methods. Before trying to implement code, it will be best to think through these methods from the standpoint of *your* site's structure, and take into account the maintenance burden associated with each method. Usually, putting an identifier in the static URL itself (as shown in the first rule above) is the easiest long-term solution.

Jim

Philarmon

10+ Year Member



 
Msg#: 1040 posted 8:46 pm on Feb 9, 2004 (gmt 0)

Cool, thanks a lot JD!

I am still missing two things:

1. if i try mysite.com/new instead of mysite.com/new/, i am getting an error. How do i make it to work too?

2. How do i make a 301 redirect for an dynamic URL - the only one where external sites link to.
i have tried this:

RewriteRule ^index\.php?t=sub_pages&cat=469$ /sub_pages/cat/469/ [R=301,L]

but somehow it doesnt work. Also, will .htaccess rewrite it back to the dynamic URL in the same go?

Anyone?

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 1040 posted 10:24 pm on Feb 9, 2004 (gmt 0)

The usual cure for the trailing slash problem is to set UseCanonicalName to off, or to ask your host to do it if you don't have access to the httpd.conf file.

You can also fix it with additional rewrites if necessary, but it is inelegant.

Rewrites for URLs with query strings must use RewriteCond %{QUERY_STRING} to test and back-reference query strings - see the mod_rewrite documentation and other threads here for examples.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved