Forum Moderators: phranque

Message Too Old, No Replies

Redirect with rewriterule/rewritecond help?

         

MTKilpatrick

10:50 am on Oct 8, 2010 (gmt 0)

10+ Year Member



Hello all,

I would very much appreciate a bit of help with using rewriterule/cond, if anyone in the know can spare a minute.

My jazz band website www.harmonyinharlem.co.uk is mapped to a subdomain of another site so is also accessible as hih.ellington-music.co.uk, and also without the www at the front. So the website has three different means of entry.

I understand how this works:

rewritecond %{http_host} !^www.harmonyinharlem.co.uk [nc]
rewriterule ^(.*)$ [harmonyinharlem.co.uk...] [r=301,nc]

so that the URL is always rewritten to be www.harmonyinharlem.co.uk regardless of the means of entry. So far so good.

However, I would like, at the same time as doing the above, to make the site always appear in the form www.harmonyinharlem.co.uk/gallery instead of index.php?page=gallery.

I don't understand how to do multiple or complex rewrite statements to achieve this.

For example, if I enter via hih.ellington-music.co.uk/index.php?page=contact, I would like the URL to appear in the browser navigation bar as www.harmonyinharlem.co.uk/contact

Also, my site loads the css using @import url("css/php_main.css"); Would a rewriterule make a mess of this and try to convert it to index.php?page=css/php_main.css, or something mad like that?

Can anyone please assist?

Michael

jdMorgan

12:47 pm on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to change the URLs that appear in links and in the browser address bar, then you must edit your HTML pages -- or the scripts that produce those pages, to link to /gallery instead of /index.php?page=gallery

Once you have published a link on an HTML page, and a visitor clicks on it, it is too late for the server to do anything to change that URL appearing in the browser address bar.

One can only use mod_rewrite *after* that request arrives at the server, so by then it's too late. You must stop the old/incorrect links being published in the first place.

Then and only then can you use mod_rewrite code on the server to create an external redirect to help speed up the removal of the old/incorrect URLs from search listings, and to intervene to "repair" requests made using obsolete links on third-party sites or old bookmarks, etc.

You will also use an internal rewrite rule to re-connect requests for the new /gallery URLs to the script filepath inside the server, which is still /index.php?page=gallery.

Jim

MTKilpatrick

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

10+ Year Member



Thanks Jim. I appreciate that I need to change the links on my menu bar and so on to replace index.php?page=#*$! with /#*$!. I can change my PHP code to do that in a couple minutes, that's no problem.

Assuming I've done that, I'm not sure exactly what .htaccess code I need, and whether I need to use rewriterule, mod_rewrite, or both!

Obviously I can't get rid of any other third-party's links to my site or google listings to the old version of the site with index.php?page=something, and I would like those to be rewritten as "something/"

Michael

sublime1

2:20 pm on Oct 8, 2010 (gmt 0)

10+ Year Member



I read Jim's reply: I concur with the sentiment and the advice, but I was a little confused at first, so thought I might expand.

There are three issues to resolve.

One is getting the rewrite condition/rule to capture and redirect externally ("R=301") the response to a URL that looks as you want, but returns the proper page. If you do a 301 redirect, the browser will not return the page, but instead will return a new URL (the pretty one) that the browser will request again in a second try. Assuming your site knows how to return a page using that new URL format (see below), the second response will result in a page the user can see.

And a 301 redirect as described will change the URL displayed in the address bar, and will cause search engines to eventually change what they use as links to your site. However, it's only part of the problem.

You must also make sure that your website knows how to respond properly to the new URL format once it comes in. If you do not want to change anything in your site, then an internal rewrite from the new (external) URL format to the existing (internal) URL format is an option.

However, you may want to consider whether there's an alternative to an internal, since (as Jim points out), there's one more issue.

The last is making sure that the pages with links to other pages on your site start returning those URL links in the new format. Without doing this, every click on your site to an old-style URL will result in a 301 redirect, which is pointless, time-consuming and poor practice.

I would have a slight disagreement with Jim's phasing "You must stop the old/incorrect links..." -- I would change "must" to "should" because even if it's terrible practice to produce links in your pages that are different than the ones you want users and search engines to see, as long as you address the first two items, the site will work ... technically.

It will take a long time (months, years?) for all the links to your site out on the web to get updated, so you have to support both old and new formats when the request is received, but if you're already changing the way the links in your site are presented, maybe you can also change how the site responds to URL requests in the first place. Internal rewrites have a couple tricky aspects that have tripped up many (like me).

As for the CSS, you'll need to make sure your conditions know how to handle that case as well -- another reason to see if you can change the way the site generates and responds to URLs in the first place.

So some additional questions for you:
  • How are your pages being generated now? Are you using software like WordPress, Blogger, Drupal, etc. or a custom php program. If the former, check to see if the software has some means of handling this kind of URL transformation, sometimes known as "pretty URLs" or similar. Wordpress allows this (almost) automatically, for example.
  • You can add rewriting directives in two different contexts. If you have access to the web server configuration files, you can do all of this in a
    <VirtualHost>
    container. In either case, you can also do these in a .htaccess file, typically located in the document root of your site. There are subtle but important distinctions between how they work, so please specify which option(s) are available to you.
  • If your current publishing software doesn't allow pretty URLs or you cannot change the scripts, are you sure you need to do this change? Or could you just move the site to another platform that allows such a change?
  • Some examples of specific URLs in old and new format would help. If you surround them with the "code", "url", or "pre" style codes the URLs won't get mangled by the forum software. See the link for Style Codes for more info.


Hopefully this is not confusing Jim's response, but instead amplifying the message. With this information I am sure we can do a good job of helping.

Tom

sublime1

2:33 pm on Oct 8, 2010 (gmt 0)

10+ Year Member



OK, so we were writing responses at the same time :-)

Answers to some questions:
  • mod_rewrite is an Apache module that will need to be enabled in order to use various features of the rewriting engine. If your original rewrite example is working, you're good to go.
  • You'll need to use RewriteCond and RewriteRule. The condition allows you to filter which URLs should trigger the subsequent RewriteRule. (Actually, the RewriteRule also needs to match, but if you're using .htaccess, and cannot use a VirtualHost instead, you'll need both.
  • You'll need external redirects (rewrite rules having the flags [R=301,L]) and list them first.
  • Then you'll need to have internal rewrite rules (they do not have the R=301 flag). Be very cognizant of the recursive nature of internal rewrites when used in the .htaccess context -- there have been several threads in this forum that address this issue over the last week.
  • Read the forum library for examples and make sure you have a pretty decent understanding of the Apache rewrite mechanism. It is devilishly tricky, and can completely mess up your site. Having a way to test is highly recommended.


Tom

jdMorgan

6:57 pm on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# Externally redirect direct client requests for dynamic URL+query to static SEO-friendly URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?page=gallery\ HTTP/
RewriteRule ^index\.php$ http://www.harmonyinharlem.co.uk/gallery? [R=301,L]
#
# Externally redirect non-blank non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.harmonyinharlem\.co\.uk)?$
RewriteRule ^(.*)$ http://www.harmonyinharlem.co.uk/$1 [R=301,L]

By testing THE_REQUEST, we ensure that the dynamic path is being requested as a URL directly by the HTTP client, and not as a result of a preceding internal URL-to-filepath rewrite used to 'deliver' the SEO-friendly URL requests to your script. Without this test, the two rewriterules would contermand each other in the .htaccess context, resulting in an 'infinite' rewrite/redirect loop.

This and more is part of Changing Dynamic URLs to Static URLs -- Implementing search engine friendly URLs with mod_rewrite [webmasterworld.com] in our Apache Forum Library [webmasterworld.com].

Jim

MTKilpatrick

10:16 am on Oct 9, 2010 (gmt 0)

10+ Year Member



Thanks very much for the replies everyone, and I'll have a look at the forum library and resources you pointed me to.

Jim, thanks for the example code. My site isn't going to change or grow very much so it's a fairly well constrained problem, and it would take a while to read through all the syntax and rules for rewritecode to get to understand all of the options available, so that was very helpful.

Michael