more .htaccess tips and tricks..

<ifModule>
more clever stuff here
</ifModule>

redirecting and rewriting

"The great thing about mod_rewrite is it gives you all the configurability and flexibility of Sendmail. The downside to mod_rewrite is that it gives you all the configurability and flexibility of Sendmail."

- Brian Behlendorf, Apache Group

One of the more powerful tricks of the .htaccess hacker is the ability to rewrite URLs. This enables us to do some mighty manipulations on our links; useful stuff like transforming Very Long URL's into Short, Cute URLs, transforming dynamic ?generated=page&URL's into /friendly/flat/links, redirect missing pages, preventing hot-linking, performing automatic language translation, and much, much more.

Make no mistake, mod_rewrite is complex. This isn't the subject for a quick bite-size tech-snack, probably not even a week-end crash-course, I've seen guys pull off some real cute stuff with mod_rewrite, but with kudos-hat tipped firmly towards that bastard operator from hell, Ralf S. Engelschall, author of the magic module itself, I have to admit that a great deal of it still seems so much voodoo to me.

The way that rules can work one minute and then seem not to the next, how browser and other in-between network caches interact with rules and testing rules is often baffling, maddening. When I feel the need to bend my mind completely out of shape, I mess around with mod_rewrite!

After all this, it does work, and while I'm not planning on taking that week-end crash-course any time soon, I have picked up a few wee tricks myself, messing around with web servers and web sites, this place..

The plan here is to just drop some neat stuff, examples, things that have proven useful, and work on a variety of server setups; there are Apache's all over my LAN, I keep coming across old .htaccess files stuffed with past rewriting experiments that either worked; and I add them to my list, or failed dismally; and I'm surprised that more often these days, I can see exactly why!

Very little here is my own invention. Even the bits I figured out myself were already well documented, I just hadn't understood the documents, or couldn't find them. Sometimes, just looking at the same thing from a different angle can make all the difference, so perhaps this humble stab at URL Rewriting might be of some use. I'm writing it for me, of course. but I do get some credit for this..

# time to get dynamic, see..
RewriteRule (.*)\.htm $1.php

beginning rewriting..

Whenever you use mod_rewrite (the part of Apache that does all this magic), you need to do..

..before any ReWrite rules. note: +SymLinksIfOwnerMatch must be enabled for any rules to work, this is a security requirement of the rewrite engine. Normally it's enabled in the root and you shouldn't have to add it, but it doesn't hurt to do so, and I'll insert it into all the examples on this page, just in case*. The next line simply switches on the rewrite engine

for that folder. if this directive is in you main .htaccess file, then the ReWrite engine is theoretically enabled for your entire site, but it's wise to always add that line before you write any redirections, anywhere.

* Although highly unlikely, your host may have +SymLinksIfOwnerMatch enabled at the root level, yet disallow its addition in .htaccess; in which case, adding +SymLinksIfOwnerMatch will break your setup (probably a 500 error), so just remove it, and your rules should work fine.

Important: While some of the directives on this page may appear split onto two lines in your browser, in your .htaccess file they must exist completely on one line. If you drag-select and copy the directives on this page, they should paste just fine into any text editor.

simple rewriting

Simply put, Apache scans all incoming URL requests, checks for matches in our .htaccess file and rewrites those matching URLs to whatever we specify. something like this..

all requests to whatever.htm will be sent to whatever.php:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [NC]

Handy for anyone updating a site from static htm (you could use .html, or .htm(.*), .html?, etc.) to dynamic php pages; requests to the old pages are automatically rewritten to our new urls. No one notices a thing, visitors and search engines can access your content either way. Leave the rule in; as an added bonus, this enables us to easily split php code and its included html structures into two separate files, a nice idea; makes editing and updating a breeze. The [NC] part at the end means "No Case", or "case-insensitive"; more on the switches, later.

Folks can link to whatever.htm or whatever.php, but they always get whatever.php in their browser, and this works even if whatever.htm doesn't exist! But I'm straying..

As it stands, it's a bit tricky; folks will still have whatever.htm in their browser address bar, and will still keep bookmarking your old .htm URL's. Search engines, too, will keep on indexing your links as .htm, some have even argued that serving up the same content from two different places could have you penalized by the search engines. This may or not bother you, but if it does, mod_rewrite can do some more magic..

this will do a "real" external redirection:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^(.+)\.htm$ https://corz.org/$1.php [R,NC]

This time we instruct mod_rewrite to do a proper external rewrite, aka, "redirection". Now, instead of just background rewriting on-the-fly, the user's browser is physically redirected to a new URI, and whatever.php appears in their browser's address bar - search engines and other spidering entities will automatically update their links to the .php versions; everyone wins. You can take your time with the updating, too.

Note: if you use [R] alone, it defaults to sending an HTTP "MOVED TEMPORARILY" redirection, aka, "302". But you can send other codes, like so..

this performs the exact same as the previous example RewriteRule.
RewriteRule ^(.+)\.htm$ https://corz.org/$1.php [R=302,NC]

Okay, I sent the exact same code, but I didn't have to. For details of the many 30* response codes you can send, see here. Most people probably want to send 301, aka, "MOVED PERMENENTLY".

Note: if you add an "L" flag to the mix; meaning "Last Rule", e.g. [R=302,NC,L]; Apache will stop processing rules for this request at that point, which may or may not be what you want. Either way, it's useful to know.

not-so-simple rewriting ... flat links and more

You may have noticed, the above examples use regular expression to match variables. What that simply means is.. match the part inside (.+) and use it to construct "$1" in the new URL. In other words, (.+) = $1 you could have multiple (.+) parts and for each, mod_rewrite automatically creates a matching $1, $2, $3, etc, in your target (aka. 'substitution') URL. This facility enables us to do all sorts of tricks, and the most common of those, is the creation of "flat links"..

Even a cute short link like https://mysite/grab?file=my.zip is too ugly for some people, and nothing less than a true old-school solid domain/path/flat/link will do. Fortunately, mod_rewrite makes it easy to convert URLs with query strings and multiple variables into exactly this, something like..

a more complex rewrite rule:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]

would allow you to present this link as..

  https://mysite/files/games/hoopy.zip

and in the background have that transparently translated, server-side, to..

  https://mysite/download.php?section=games&file=hoopy

which some script could process. You see, many search engines simply don't follow our ?generated=links, so if you create generating pages, this is useful. However, it's only the dumb search engines that can't handle these kinds of links; we have to ask ourselves.. do we really want to be listed by the dumb search engines? Google will handle a good few parameters in your URL without any problems, and the (hungry hungry) msn-bot stops at nothing to get that page, sometimes again and again and again…

I personally feel it's the search engines that should strive to keep up with modern web technologies, in other words; we shouldn't have to dumb-down for them. But that's just my opinion. Many users will prefer /files/games/hoopy.zip to /download.php?section=games&file=hoopy but I don't mind either way. As someone pointed out to me recently, presenting links as standard/flat/paths means you're less likely to get folks doing typos in typed URL's, so something like..

an even more complex rewrite rule:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^blog/([0-9]+)-([a-z]+) https://corz.org/blog/index.php?archive=$1-$2 [NC]

would be a neat trick, enabling anyone to access my blog archives by doing..

 https://corz.org/blog/2003-nov

in their browser, and have it automagically transformed server-side into..

 https://corz.org/blog/index.php?archive=2003-nov

which corzblog would understand (note: corzblog can also produce flat links - the crucial "other side" of the flat-links equation that people often forget in their quest for cute URLs). It's easy to see that with a little imagination, and a basic understanding of posix regular expression, you can perform some highly cool URL manipulations.

Here's the basics of regexp (expanded from the Apache mod_rewrite documentation)..

Escaping:

	\char escape that particular char

		For instance to specify special characters.. [].()\ etc.

	Text:

	.             Any single character  (on its own = the entire URI)
	[chars]       Character class: One of following chars
	[^chars]      Character class: None of following chars
	text1|text2   Alternative: text1 or text2 (i.e. "or")

		e.g. [^/] matches any character except /
			 (foo|bar)\.html matches foo.html and bar.html

	Quantifiers:

	? 0 or 1 of the preceding text
	* 0 or N of the preceding text  (hungry)
	+ 1 or N of the preceding text

		e.g. (.+)\.html? matches foo.htm and foo.html
			 (foo)?bar\.html matches bar.html and foobar.html

	Grouping:

	(text)  Grouping of text

		Either to set the borders of an alternative or
		for making backreferences where the nthe group can
		be used on the target of a RewriteRule with $n

		e.g.  ^(.*)\.html foo.php?bar=$1

	Anchors:

	^    Start of line anchor
	$    End   of line anchor

		An anchor explicitly states that the character right next to it MUST
		be either the very first character ("^"), or the very last character ("$")
		of the URI string to match against the pattern, e.g..

		^foo(.*) matches foo and foobar but not eggfoo
		(.*)l$ matches fool and cool, but not foo
	

If you are new to messing around with code or text, you should memorize the above section. The power you gain and the time you will save is well worth the minutes it will take to get your head around it!

I use regex dozens, sometimes hundreds of times a day, mainly to find (and often replace) specific strings in large documents. I have probably saved about a year of LIFE all-in. That's time you can spend doing things you enjoy. Nuff said.

shortening URLs

One common use of mod_rewrite is to shorten URL's. Shorter URL's are easier to remember and, of course, easier to type. An example..

beware the regular expression:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^grab /public/files/download/download.php

this rule would transform this user's URL..

  https://mysite/grab?file=my.zip

server-side, into..

  https://mysite/public/files/download/download.php?file=my.zip

which is a wee trick I use for my distro machine, among other things. everyone likes short URL's, and so will you; using this technique, you can move /public/files/download/ to anywhere else in your site, and all the old links still work fine; simply alter your .htaccess file to reflect the new location. edit one line, done - nice - means even when stuff is way deep in your site you can have cool links like this..

and this; links which are not only short, but flat..

capturing variables

Slapping (.*) onto the end of the request part of a ReWriteRule is just fine when using a simple $_GET variable, but sometimes you want to do trickier things, like capturing particular variables and converting them into other variables in the target URL. Or something else..

When capturing variables, the first thing you need to know about, is the [QSA] flag, which simply tags all the original variables back onto the end of the target url. This may be all you need, and will happen automatically for simple rewrites. The second thing, is %{QUERY_STRING}, an Apache server string we can capture variables from, using simple RewriteCond (aka. conditional) statements.

RewriteCond is similar to doing if...then...do in many programming languages. If a certain condition is true, then do the rewrite that follows..

In the following example, the RewriteCond statement checks that the query string has the foo variable set, and captures its value while it's there. In other words, only requests for /grab that have the variable foo set, will be rewritten, and while we're at it, we'll also switch foo, for bar, just because we can..

capturing a $_GET variable:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{QUERY_STRING} foo=(.*)
RewriteRule ^grab(.*) /page.php?bar=%1

would translate a link/user's request for..

https://domain.com/grab?foo=bar

server-side, into..

https://domain.com/page.php?bar=bar

Which is to say, the user's browser would be fed page.php (without an [R] flag in the RewriteRule, their address bar would still read /grab?foo=bar). The variable bar would be available to your script, with its value set to bar.

This variable has been magically created, by simply using a regular ? in the target of the RewriteRule, and tagging on the first captured backreference, %1.. ?bar=%1

Note how we use the % character, to specify variables captured in RewriteCond statements, aka "Backreferences". This is exactly like using $1 to specify numbered backreferences captured in RewriteRule patterns, except for strings captured inside a RewriteCond statement, we use % instead of $. Simple.

You can use the [QSA] flag in addition to these query string manipulations, merge them. In the next example, the value of foo becomes the directory in the target URL, and the variable file is magically created. The original query string is then tagged back onto the end of the whole thing..

QSA Overkill!
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{QUERY_STRING} foo=(.+)
RewriteRule ^grab/(.*) /%1/index.php?file=$1 [QSA]

So a request for..

https://domain.com/grab/foobar.zip?level=5&foo=bar

is translated, server-side, into..

https://domain.com/bar/index.php?file=foobar.zip&level=5&foo=bar

Depending on your needs, you could even use flat links and dynamic variables together, something like this could be useful..

By the way, you can easily do the opposite, strip a query string from a URL, by simply putting a ? right at the end of the target part. This example does exactly that, whilst leaving the actual URI intact..

just a demo!
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule foo.php(.*) /foo.php? [L]

The RewriteCond statement only allows requests that have something in their query string, to be processed by the RewriteRule, or else we'd end up in that hellish place, dread to all mod_rewriters.. the endless loop. RewriteCond is often used like this; as a safety-net.

If all you are after is a /simple/flat/link/ to server-side.php?query=variable translation, use something like this..

cooler access denied

In part one I demonstrated a drop-dead simple mechanism for denying access to particular files and folders. The trouble with this is the way our user gets a 403 "Access Denied" error, which is a bit like having a door slammed in your face. Fortunately, mod_rewrite comes to the rescue again and enables us to do less painful things. One method I often employ is to redirect the user to the parent folder..

they go "huh?.. ahhh!"
# send them up!
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^(.*)$ ../ [NC]

It works great, though it can be a wee bit tricky with the URLs, and you may prefer to use a harder location, which avoids potential issues in indexed directories, where folks can get in a loop..

they go damn! Oh!
# send them exactly there!
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^(.*)$ /network/routers/Voyager/ [NC]

Sometimes you'll only want to deny access to most of the files in the directory, but allow access to maybe one or two files, or file types, easy..

deny with style!
# users can load only "special.zip", and the css and js files.
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^(.+)\.css$
RewriteCond %{REQUEST_FILENAME} !^(.+)\.js$
RewriteCond %{REQUEST_FILENAME} !special.zip$
RewriteRule ^(.+)$ /chat/ [NC]

Here we take the whole thing a stage further. Users can access .css (stylesheet) and Javascript files without problem, and also the file called "special.zip", but requests for any other file types are immediately redirected back up to the main "/chat/" directory. You can add as many types as you need. You could also bundle the filetypes into one line using | (or) syntax, though individual lines are perhaps clearer.

For those that don't code, the "!" sign simply means not something. In most programming languages one writes "not equal to" as "!=" or "!==" or similar.

Here's the sort of thing you might find inside my /inc/ directory..

all-in-one control..
RewriteEngine on
Options +SymLinksIfOwnerMatch
# allow access with no restrictions to local machine at 192.168.1.3
RewriteCond %{REMOTE_ADDR} !192.168.1.3
# allow access to all .css and .js in sub-directories..
RewriteCond %{REQUEST_URI} !\.css$
RewriteCond %{REQUEST_URI} !\.js$
# allow access to the files inside img/, but not a directory listing..
RewriteCond %{REQUEST_URI} !img/(.*)\.
# allow access to these particular files...
RewriteCond %{REQUEST_URI} !comments.php$
RewriteCond %{REQUEST_URI} !gd-verify.php$
RewriteCond %{REQUEST_URI} !post-dumper.php$
RewriteCond %{REQUEST_URI} !source-dump.php$
RewriteCond %{REQUEST_URI} !textview.php$
RewriteRule . http://%{HTTP_HOST}/ [R,NC,L]

Ban User Agents, referrers, script-kiddies and more..

There are many valid reasons to ban a particular request from sucking up your site's resources; resources that could be better served to valid, interested users. It might be some cross-site attack script, or inward link from a place you don't want to be associated with, or perhaps a web sucker or download manager, whatever; .htaccess + mod_rewrite provides ways to protect your content from unwanted "guests".

The basic formula is standard if-then logic: if the request meets a particular CONDITION, then REWRITE the request. The "conditions" can be many things; perhaps the referrer header sent by their browser (the site they came from), or the page they asked for, or a particular query parameter, or the type of client (browser, etc.) they are using, or any other piece of information Apache has attached to the request. Here's an example which will deny access to "Teleport Pro", a download manager which is known to suck, hard..

Who need's a local copy, when I'm right here?..
RewriteCond %{HTTP_USER_AGENT} ^Teleport\ Pro [NC]
RewriteRule . abuse.txt [L]

It's your site, and just like your home, you have every right to exert some control over who gets in. You may have a huge list of user agents you'd rather not have eating your bandwidth; so use the [OR] flag, and line 'em up..

A little garlic for the net vampires..
RewriteCond %{HTTP_USER_AGENT} ^BackWeb [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Bandit [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^BatchFTP [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^BecomeBot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [NC,OR]
# etc..
RewriteCond %{HTTP_USER_AGENT} ^Net\ Vampire [NC]
RewriteRule . abuse.txt [L]

This forms the basis of what often becomes a HUGE list of ban-lines. Remember, we aren't limited to user agent strings..

Suckers, h4x0rz, kiddies, cross-site scripters and more.. Bye now!
# why not come visit me directly?
RewriteCond %{HTTP_REFERER} \.opendirviewer\. [NC,OR]
# this prevents stoopid cross-site discovery attacks..
RewriteCond %{THE_REQUEST} \?\ HTTP/ [NC,OR]
# please stop pretending to be the Googlebot..
RewriteCond %{HTTP_REFERER} users\.skynet\.be.* [NC,OR]
# really, we need a special page for these twats..
RewriteCond %{QUERY_STRING} \=\|w\| [NC,OR]
RewriteCond %{THE_REQUEST} etc/passwd [NC,OR]
RewriteCond %{REQUEST_URI} owssvr\.dll [NC,OR]
# you can probably work these out..
RewriteCond %{QUERY_STRING} \=\|w\| [NC,OR]
RewriteCond %{THE_REQUEST} \/\*\ HTTP/ [NC,OR]
# etc..
RewriteCond %{HTTP_USER_AGENT} Sucker [NC]
RewriteRule . abuse.txt [L]

Fortunately, mod_rewrite can parse enormous lists of ban-lines in milliseconds, so feel free to be as specific and comprehensive as required.

As ever, thorough testing is strongly recommended. Simply send requests matching your conditions and see what happens. And importantly; normal requests, too. Firefox, Opera, Konqueror, and most other decent browsers, allow you to alter the user agent string; though you would quickly find the process tedious in a testing situation. Far better to use some tool better designed to send fake HTTP requests..

It's not too difficult to mock up a web request on the command-line with any-old user agent using a scripting language like php or Perl, if you have these things available (read: most Linux/UNIX/BSD/etc. as well as many other OS). Many examples exist online. In fact, you could quickly create a suite of tests, designed to interrogate all your rewrite rules, with results logging and much more, if required. cURL is always useful for jobs like this, so long as you don't add a cURL ban-line!

On a Windows desktop, Sam Spade can send a single spoofed request with a couple of clicks, along with a stack of similarly handy tricks, and regularly proves itself invaluable.

Don't let just anyone hammer your site!

While I'm on the subject of abusive web clients, you will probably have noticed that many clients (bots, spiders, automated suckers and such) like to disguise their user agent information, in fact any information, in an attempt to bring your site to its knees, hammering your pages umpteen times per second in the process. Not good.

If you are interested in a way to defeat hammering web clients regardless of who they pretend to be or whether or not they accept cookies or any such malarkey, protecting your valuable server resources for genuine clients, check out: Anti-Hammer.

prevent hot-linking

Believe it or not, there are some webmasters who, rather than coming up with their own content will steal yours. Really! Even worse, they won't even bother to copy to their own server to serve it up, they'll just link to your content!  no, it's true, in fact, it used to be incredibly common. These days most people like to prevent this sort of thing, and .htaccess is one of the best ways to do it.

This is one of those directives where the mileage variables are at their limits, but something like this works fine for me..

how DARE they!
Options +SymLinksIfOwnerMatch
# no hot-linking
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?corz\.org/ [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(gif|png) [NC]
RewriteRule .*\.(gif|jpg|png)$

You may see the last line broken into two, but it's all one line (all the directives on this page are). Let's have a wee look at what it does..

We begin by enabling the rewrite engine, as always.

The first RewriteCond line allows direct requests (not from other pages - an "empty referrer") to pass unmolested. The next line means; if the browser did send a referrer header, and the word "corz.org" is not in the domain part of it, then DO rewrite this request.

The all-important final RewriteRule line instructs mod_rewrite to rewrite all matched requests (anything without "corz.org" in its referrer) asking for gifs, jpegs, or pngs, to an alternative image.

There are loads of ways you can write this rule; Google for "hot-link protection" and get a whole heap. Simple is best. You could send a wee message instead, or direct them to some evil script, or something. Mine is a simple corz.org logo, which I think is rather clever. Actually, these days, I do something even cleverer-er..

lose the "www"

I'm often asked how I prevent the "www" part showing up at my site, so I guess I should add something about that. Briefly, if someone types https://www.corz.org/ into their browser (or uses the www part for any link at corz.org) it is redirected to the plain, rather neat, https://corz.org/ version. This is easy to achieve, like so..

beware the regular expression:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteCond %{http_host} ^www\.corz\.org [NC]
RewriteRule ^(.*)$ https://corz.org/$1 [R=301,NC]

You don't need to be touched by genius to see what's going on here. There are other ways you could write this rule, but again, simple is best. Like most of the examples here, the above is pasted directly from my own main .htaccess file, so you can be sure it works perfectly. In fact, I recently updated it so that I could share rules between my dev mirror and live site without any .htaccess editing..

here's what I'm currently using:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
For fun, here's how to do the same thing in php (in your site's early init routine)..
And with an extra php request, of course..
if (substr($_SERVER['HTTP_HOST'], 0, 4) == "www.") {
    header("Location: https://mydomain.org".$_SERVER['REQUEST_URI']);
    exit();
}

multiple domains in one root

If you are in the unfortunate position of having your sites living on a host that doesn't support multiple domains, you may be forced to roll your own with .htaccess and mod_rewrite. So long as your physical directory structure is well thought-out, this is fairly simple to achieve.

For example, let's say we have two domains, pointing at a single hosted root; domain-one.com and domain-two.com. In our web server root, we simply create a folder for each domain, perhaps one/, and two/ then in our main (root) .htaccess, rewrite all incoming requests, like this..

All requests NOT already rewritten into these folders, transparently rewrite..
#two domains served from one root..
RewriteCond %{HTTP_HOST} domain-one.com
RewriteCond %{REQUEST_URI} !^/one
RewriteRule ^(.*)$ one/$1 [L]

RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URI} !^two
RewriteRule ^(.*)$ two/$1 [L]

All requests for the host domain-one.com are rewritten (not R=redirected) to the one/ directory, so long as they haven't already been rewritten there (the second RewriteCond). Same story for domain-two.com. Note the inconsistency in the RewriteCond statement; !^/dir-name and !^dir-name should both work fine. But needless to say, if you get a 500 error on your server, that would be a good place to start looking!

Also note, with such a simple domain & folder naming scheme, you could easily merge these two rule sets together. This would be unlikely in the real world though, which is why I left them separate; but still, worth noting.

Other general settings and php directives can also go in this root .htaccess file, though if you have any further rewrite you'd like to perform; short URL's, htm to php conversion and what-not; it's probably easier and clearer to do those inside the sub-directory's .htaccess files.

automatic translation

If you don't read English, or some of your guests don't, here's a neat way to have the wonderful Google translator provide automatic on-the-fly translation for your site's pages. Something like this..

they simply add their country code to the end of the link, or you do..
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^(.*)-fr$ https://www.google.com/translate_c?hl=fr&sl=en&u=https://corz.org/$1 [R,NC]
RewriteRule ^(.*)-de$ https://www.google.com/translate_c?hl=de&sl=en&u=https://corz.org/$1 [R,NC]
RewriteRule ^(.*)-es$ https://www.google.com/translate_c?hl=es&sl=en&u=https://corz.org/$1 [R,NC]
RewriteRule ^(.*)-it$ https://www.google.com/translate_c?hl=it&sl=en&u=https://corz.org/$1 [R,NC]
RewriteRule ^(.*)-pt$ https://www.google.com/translate_c?hl=pt&sl=en&u=https://corz.org/$1 [R,NC]

You can create your menu with its flags or whatever you like, and add the country code to end of the links.. <a href="page.html-fr" id="... Want to see this page in French?

Although it is handy, and I've been using it here for a couple of years here at the org for my international blog readers, all two of them, heh. Almost no one knows about it, mainly because I don't have any links. One day I'll probably do a wee toolbar with flags and what-not. Perhaps not. Trouble is, the Google translator stops translating after a certain amount of characters (which seems to be increasing, good), though these same rules could easily be applied to other translators, and if you find a good one, one that will translate a really huge  document on-the-fly, do let me know!

If you wanted to be really clever, you could even perform some some kind of IP block check and present the correct version automatically, but that is outside the scope of this document. note: this may be undesirable for pages where technical commands are given (like this page) because the commands will also be translated. "RewriteEngine dessus" will almost certainly get you a 500 error page!

Another thing you might like to try; rather than individual country flags; fr, de, etc., use the "u" flag, for "Universal". In theory, Google will check the client's location, and automatically translate to that language. One line in your .htaccess would cover all languages, and automatically cover new ones as Google adds them.

While I'm here, slightly related; you can do a similar thing browser-side, create a "bookmarklet" (a regular bookmark, except that it "does something"), using this code for the location..

the same sort of thing, except browser-side..
javascript:void(location.href='https://translate.google.com/translate?u='+location.href)

..which you will instinctively learn to click at the merest whiff of unrecognizable text, I reckon. Put it in your toolbar somewhere visible, is my sincere recommendation.

httpd.conf

Remember, if you put these rules in the main server conf file (usually httpd.conf) rather than an .htaccess file, you'll need to use ^/... ... instead of ^... ... at the beginning of the RewriteRule line, in other words, add a slash.

inheritance..

If you are creating rules in sub-folders of your site, you need to read this.

You'll remember how rules in top folders apply to all the folders inside those folders too. we call this "inheritance". normally this just works. but if you start creating other rules inside subfolders you will, in effect, obliterate the rules already applying to that folder due to inheritance, or "decendancy", if you prefer. not all the rules, just the ones applying to that subfolder. a wee demonstration..

Let's say I have a rule in my main /.htaccess which redirected requests for files ending .htm to their .php equivalent, just like the example at the top of this very page. now, if for any reason I need to add some rewrite rules to my /osx/.htaccess file, the .htm >> .php redirection will no longer work for the /osx/ subfolder, I'll need to reinsert it, but with a crucial difference..

this works fine, site-wide, in my main .htaccess file
# main (top-level) .htaccess file..
# requests to file.htm goto file.php
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^(.*)\.htm$ https://corz.org/$1.php [R=301,NC]

Here's my updated /osx/.htaccess file, with the .htm >> .php redirection rule reinserted..

but I'll need to reinsert the rules for it to work in this sub-folder
# /osx/.htaccess file..
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule some rule that I need here
RewriteRule some other rule I need here
RewriteRule ^(.*)\.htm$ https://corz.org/osx/$1.php [R=301,NC]

Spot the difference highlighted in the subfolder rule?. You must add the current path to the new rule. now it works again, and all the osx/ subfolders will be covered by the new rule. if you remember this, you can go replicating rewrite rules all over the place.

If it's possible to put your entire site's rewrite rules into the main .htaccess file, and it probably is; do that, instead, like this..

it's a good idea to put all your rules in your main .htaccess file..
# root /.htaccess file..
Options +SymLinksIfOwnerMatch
RewriteEngine on
# .htm >> .php is now be covered by our main rule, there's no need to repeat it.
# But if we do need some /osx/-specific rule, we can do something like this..
RewriteRule ^osx/(.*)\.foo$ /osx/$1.bar [R=301,NC]

Note, no full URL (with domain) in the second example. Don't let this throw you; with or without is functionally identical, on most servers. Essentially, try it without the full URL first, and if that doesn't work, sigh, and add it - maybe on your next host!

The latter, simpler form is preferable, if only for its tremendous portability it offers - my live site, and my development mirror share the exact same .htaccess files - a highly desirable thing.

By the way, it perhaps doesn't go without saying that if you want to disable rewriting inside a particular subfolder, where it is enabled further up the tree, simply do:

handy for avatar folders, to allow hot-linking, etc..
RewriteEngine off

cookies

Lastly, a quick word about cookies. While it's easy enough to set cookies in .htaccess without any mod_rewrite..

create a cookie called "example-cookie", and set its value to "true"..
Header set Set-Cookie "example-cookie=true"

..you will need it to read the cookie information back again, and "do stuff" with it. It's easy. For example, to check if the above cookie exists and has the correct value set, we could simply do..

check for that same cookie + value..
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !example-cookie=true
RewriteRule .* /err/401.php

..which could easily form the basis of a simple authentication system. As with any RewriteCond, you can get pretty complex, checking multiple cookies, utilizing regexp and more, but that's enough to get you started. You will probably want to add another RewriteCond to prevent looping on the 401. I'll leave that as an exercise.

While I'm at it, note, you can also set cookies with a RewriteRule. Check this..

Set a cookie with this visitor's "Original Referer", using RewriteRule..
SetEnvIf Referer "^https?://(.*)/" myref=$1
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !corz.org [NC]
RewriteRule . - [cookie=original-referer:%{ENV:myref}:.%{HTTP_HOST}:1440:/]

The first line checks the value of the "Referer" header (usually sent by the browser - by the way, the misspelling of "referrer" annoys a lot of people, but its hard-coded into the web now)

Let's say the referring URI is "https://corz.org/blog/", which matches (of course it matches, all Referers will match!) so the part between "https?://" and the next occurring slash ("/") of the referring URI is captured inside the braces () and then backreferenced (by the use of "$1", 1 = the first captured string, in this case "corz.org"), that value being stored in an Environment Variable (Apache has loads of them created on every request, your IP address, browser type, time of the request, remote port, loads...) named "myref".

In other words: myref=corz.org

Then we do a couple of RewriteCond conditions, the first to prevent "empty" referers (where no referer information is sent) and the second to prevent resetting the cookie with the name of our own domain*. Then it's down to business..

The RewriteRule, its simple dot "." expression, again matching every single request, passing the request straight through (to any remaining .htaccess code, by use of the "-") without altering the URI in any way, and while it's at it sets the browser's cookie variable "original-referer" to the value of the Apache variable "myref", which is currently "corz.org", accessing this variable by use of the "%{ENV:<Name-of-Environment-Variable>}" construct.

The domain of the cookie is set to "corz.org". Note, it's best practice to use a preceding dot in "www"-less, or other sub-less domains).

The cookie will expire in approximately 1440 minutes (24 hours). The cookie "path" is set to "/", covering the entire site. Job done.

Finally, having said ALL that, I'd still much rather use PHP sessions wherever possible, and if taking a chance on something more permanent, with a persistent cookie, php (or similar) is still a better place to be coding this sort of stuff.

conclusion

In short, mod_rewrite enables you to send browsers from anywhere to anywhere. You can create rules based not simply on the requested URL, but also on such things as IP address, browser agent (send old browsers to different pages, for instance), and even the time of day; the possibilities are practically limitless.

The ins-and outs of mod_rewrite syntax are topic for a much longer document than this, and if you fancy experimenting with more advanced rewriting rules, I urge you to check out the Apache documentation.

If you have Apache installed on your system.. there will likely be a copy of the Apache manual, right here, and the excellent mod_rewriting guide, lives right here. do check out the URL Rewriting Engine notes for the juicy syntax bits. That's where I got the cute quote for the top of the page, too.

Have fun!

;o) Cor

troubleshooting tips..

Fatal Redirection

If you start messing around with 301 redirects [R=301], aka. "Permanently Redirected", and your rule isn't working, you could give yourself some serious headaches..

Once the browser has been redirected permanently to the wrong address, if you then go on to alter the wonky rule, your browser will still be redirected to the old address (because it's a browser thing), and you may even go on to fix, and then break the rule all over again without ever knowing it. Changes to 301 redirects can take a long time to show up in your browser.

Solution: restart your browser, or use a different one.

Better Solution: Use [R] instead of [R=301] while you are testing. When you are 100% certain the rule does exactly as it's expected to, then switch it to [R=301] for your live site.

rewrite logging..

When things aren't working, you may want to enable rewrite logging. I'll assume you are testing these mod_rewrite directives on your development mirror, or similar setup, and can access the main httpd.conf file. If not, why not? Testing mod_rewrite rules on your live domain isn't exactly ideal, is it? Anyway, put this somewhere at the foot of your http.conf..

Expect large log files..
#
# ONLY FOR TESTING REWRITE RULES!!!!!
#
RewriteLog "/tmp/rewrite.log"
#RewriteLogLevel 9
RewriteLogLevel 5

Set the file location and logging level to suit your own requirements. Unless you are using Apache >= v2.4. The RewriteLog and RewriteLogLevel directives have now been deprecated. Now your old RewriteLog directives will bring the server down..

Recent versions of Apache need this, instead..
#
# ONLY FOR TESTING REWRITE RULES!!!!!
#
LogLevel alert rewrite:trace3

This sets the main LogLevel to alert, and turns it up the volume of rewrite logging to trace3. A full explanation of the various LogLevel and trace options can be found here.

Of course, there is no separate rewrite log now, so to get a readout of the rewrite logging, do something like (at a terminal prompt on the server)..

(or use BareTail or similar on your Windows desktop)..
tail -f /var/log/httpd/error.log|fgrep '[rewrite:'

If the offending rewrite rule is causing your Apache to loop; load the page, immediately hit your browser's "STOP" button, and then restart Apache. All within a couple of seconds. Your rewrite log will be full of all your diagnostic information, and your server will carry on as before.

Setting a value of 1 gets you almost no information, setting the log level to 9 (or trace8, from Apache 2.4 onwards) gets you GIGABYTES! (less true under the new scheme) So you must remember to comment out these rules and restart Apache when you are finished because, not only will rewrite logging create space-eating files, it will seriously impact your web server's performance.

Traditionally, RewriteLogLevel 5  was very useful, but 2 was probably enough information for most issues. In the new scheme, trace:3 is probably about as high as you want to go, unless you have lots of space and time, or some rule that simply refuses to work!

Suddenly "Forbidden"..

On the subject of new and improved Apache versions, if you are rolling your own server you may have noticed that your sites dramatically fail to load after an Apache upgrade. Everything is "Forbidden", and you don't have permission to access, well anything.

It's possible that your site's DocumentRoot points to a symlink. If that's the case, add this to your site's host (probably vhost) section:

Oh yes, it's a "permissions problem"..
Require all granted

And you're in!

debug-report.php
A php script to make your mod_rewrite life easier.

When things aren't working as you would expect, rewrite logging is a good option, but on a hosted server, you probably won't have that option, without access to httpd.conf. Fortunately, what's usually required is no more than a quick readout of all the current server variables, $_GET array, and so on; so you can see exactly what happened to the request.

For another purpose, I long ago created debug.php, and later, finding all this information useful in chasing down wonky rewrites, created a "report" version, which rather than output to a file, spits the information straight back into your browser, as well as $_POST, $_SESSION, and $_SERVER arrays, special variables, like __FILE__, and much more.

Usage is simple; you make it your target page, so in a rule like this..

Send EVERYTHING to the debug unit..
RewriteRule ^(.*)\.html$ /catch-all.php?var=$1

You would have a copy of debug-report.php temporarily renamed to catch-all.php in the root of your server, and type https://testdomain.org/foobar.html into your address bar and, with yer mojo working, debug-report.php leaps into your browser with a huge wodge of exactly the sort of information you need to figure out all this stuff. When I'm messing with mod_rewrite, debug-report.php saves me time, a lot. It's way faster than enabling rewrite logging, too. Also, it's free..

download

debug report

click to see zip archive contents

Before you ask a question..

Firstly, read this at least once in your life. I insist!

NOTE: THIS IS NOT A COMMUNITY. And I am not your free tech dude. Sure, folk sometimes drop back in, but realistically, the chances of someone else coming along and answering your tech question are about as close to zero as it gets; almost no one sticks around but me, the guy who wrote all that text (above).

If you can't be bothered to read the article, I can't be bothered responding. Capiche? I do read all comments, though, and answer questions about the article. I'm also keen to discuss anything you think I've missed, or interesting related concepts in general.

If you are still sure that you want to post your own, personal, tech question, then please ensure that you first, either..

a) Have read the article (above) and have tried "everything" yourself; in which case; post the exact code that isn't working (preferably inside [pre][/pre] tags), replacing any personal domain names with "example.com" (advertising gets deleted) or else..

b) Pay me. The PayPal button is at the top right of the page. I offer many related services, if you need priority assistance, get in touch.

Other posts will be ignored and/or deleted.


return to paged comments
cor - 06.11.04 10:26 pm

I decided to add comments here too,
we're most definitely in different territory with this page.

;o)



Al Raq - 09.11.04 4:17 am


Thank you.
You helped me solve a problem with hotlinkg.
Kind regards.
Al


cor - 09.11.04 9:15 pm

Thanks Al, that's what makes it all worthwhile.

;o)


ZiZe - 28.11.04 1:06 pm

Nice article.
One of the best about .htaccess i've read so far, and it helped me solve a problem i had with hot-linking.

tnx :)


Tony Elliott - 06.12.04 5:52 am

You have a good teaching gift :) I understood what you wrote.
Great work, I have bookmarked this site for future reference.
Thanks


maxcreation - 10.03.05 3:34 pm

RewriteRule ^files/(.*)/(.*).zip /download.php?section=$1&file=$2

I was looking for a solution to redirect partially to what rule matched and $1 etc was solution. It was hard to find.

I kinda googled Google trough and then later I accidentally find this article. Its great! Thank you. :)


cor - 10.03.05 9:51 pm

Yup, it's a cute and powerful command, like much of the rewrite stuff, Ralf did a great job.

More than once I've started to code some cute redirection in php with the idea that I'd "like to move away from htaccess", but usually realise fairly quickly that it's just simpler and more elegant this way. My ubercool 404 page being the exception.

You know I've even had emails from folk saying they don't believe half this stuff will work because it's too simple. smiley for :lol:

Well, I reply, I try!

;o)


guido - 22.03.05 12:02 am

here's my current redirect:

RewriteRule ^block/(.*)/(.*) /mydir/block.php?divid=$1&xtra=$2

So the url http://../block/fire/wood would go correctly to:
/mydir/block.php?divid=fire&xtra=wood

but what about if there is no second part.
This works:
http://../block/fire/ (goes to /mydir/block.php?divid=fire&xtra=)
But This doesn't
http://../block/fire (goes 404)

is there a way to send this even if there is no trailing / ?

And something else i just noticed, if I go:
http://../block/fire/ it will resolve to:
/mydir/block.php?divid=fire/wood&xtra=



cor - 23.03.05 12:14 pm

well, firstly, it's you that's supplying the links, so we wouldn't expect to get "unfinished" URL's!

In answer to your question, yes! You simply make the slash an "optional" character, something like this..

RewriteRule ^block/(.*)/?(.*) /mydir/block.php?divid=$1&xtra=$2

So it shouldn't matter whether you have a slash or not, both will work. This trick is commonly used to combat "the trailing-slash problem".

As to your second issue, it's a matter of approach. Using mod_rewrite to split up multiple variables, particularly when the are free (not supplied specifically by your pages) is not ideal.

You (and the server) could handle the whole thing more efficiently by passing everything after "block" and using php to split up the string into its respective variables. I'm not saying it's impossible, just that it would be a whole lot of messing around when a simpler approach exists. Also you benefit from better error handling, etc.

As with all rewrite fun, some experimentation is involved!
have fun!

;o)


Secret Chamber - 08.04.05 6:57 pm

I was wondering if you use htaccess to password a directory, is every file in that directory protected from search engine and other bots?


mikolaj - 09.04.05 7:07 pm

Excellent write up -- this was exactly what I was looking for.
Great work.


cor - 09.04.05 9:24 pm

Secret Chamber, if you password protect a directory, nothing and no one can access any file inside it, unless authorised. If you need to limit access to only certain files, check out the hide files stuff on the first htaccess article. You could even password protect cetain files and allow regular access to others.

thanks mikolaj, I kinda hoped it would be.

;o)


cor - 10.04.05 10:59 pm

rewriting multiple GET variables

This is from an email (by the way, you get a MUCH faster response if you post your questions on this page!), but I thought I'd drop it here, because I imagine others will find this useful, and I didn't cover this trick in the main article. The email..

Here is the rule I am trying to get work

RewriteRule ^(.*).html$ /?view=$1 [nc]
RewriteRule ^(.*).html\?(.*)$ /?view=$1&$2 [nc]

Basically what I want to make work is this. If I go to

corz.html => /?view=corz

but what I also want to work is this

corz.html?status=1&job=jedi => /?view=corz&status=1&job=jedi

only the first works... and the name/value pairs in the second example can not be retreived via $_REQUEST, it only returns /?view=corz.


It's a good question, and can't be solved with the "normal" rewrite methods.

The problem is that mod_rewrite doesn't consider the GET variables as part of the regular request string, but that doesn't mean it doesn't see them. The solution goes like this..

RewriteRule ^(.*).html(.*) /?view=$1&%{QUERY_STRING} [nc]


That's it, a nasty hack, but simple and effective!

for now..

;o)


cor - 10.04.05 11:02 pm

forgot to mention, you can usually get off with simply using the "qsa" flag (query string append)..

RewriteRule ^(.*).html(.*) /?view=$1 [nc,qsa]


;o)


Jan! - 12.04.05 9:27 pm

I am rather puzzled by your anti-hotlinking rules:

RewriteCond %{HTTP_REFERER} !^http://?corz?.*$


The question marks don't make sense to me. First of all, the second slash in http:// is not optional, as far as I know. What browser would send a referer like http:/example.com/ instead of http://example.com/?

The second question mark makes the "z" entirely optional. That means that anything starting with http://cor is valid.

The .*$ is also unnecessary. It does not give any extra information to the regular expression engine. "Hey, I want something that starts with this and ends in anything whatsoever" is the same as just saying "Hey, I want something that starts with this".

My suggestion is to change the rule to these rules:

RewriteCond %{HTTP_REFERER} !^http://(www\.)?corz\.org/
RewriteCond %{HTTP_REFERER} !^http://(www\.)?corzoogle\.com/


I know the "www" part is not used on your site, but seeing how this is an example to many, it could prove useful.

PS: I didn't include the "[nc]" flags because your parser kept barfing errors about the tags being unbalanced. Bloody helluva trial-and-error finding that out! You might want to give some better feedback, such as text in the neighbourhood of the error. (I finally found the answer after meticulously examining your sample bbcode.)


cor - 13.04.05 5:16 am

Hah! well, the simple answer is that I did the hot-link protection before I fully understood the optional characters. It was one of those messy times when it fixed something that wouldn't work, and got left in, maybe; slipped through. This often happens to .htaccess code that works, almost superstitiously, in fact.

I discovered the "Hey, I want something that starts with this and ends in anything whatsoever" is the same as just saying "Hey, I want something that starts with this" a while back, quite by accident, but again, the old hot-link code got overlooked, probably other .htaccess code, too, it just works!

I don't get off with this kind of slackness with the php stuff, it has users. You're right, though, this page has users too, a lot of folk travelling through, so I really should pay more attention! Thankfully I've got visitors willing to take the time to keep me right. Thanks for that. Or rather, this..

RewriteCond %{HTTP_REFERER} !^http://(www\.)?corz\.org/ [nc]


Much more elegant than my messy example! I'll put it up where it belongs. Thanks again Jan!

;o)

ps.. for a bbcode square bracket, you do it doubled.. [[!]] <- I had to do four there! smiley for :D

pps.. the bbcode tag balance checking is new for the current cbparser beta, and although fairly rudimentary, is deadly effective! More user feedback on which tag is barfing would be smart. noted. ta.

Jan! you can come back any time!


Steve - 19.04.05 10:06 pm

Jan, great work on the page. However, I am despeately trying to find a solution to do the following:

(1) Redirect

(2) Maintain http://www.abc.com/page.htm in address bar of browser.

RewriteRule seems to only work with local pages within the same domain. If I want to rewrite and hide the real physical link that I am redirecting to, being an external domain, it does not seem to work.

Any thoughts?


cor - 20.04.05 2:18 pm

for deception of this kind, and breaking the internet in general, use frames.

;o)


speeke - 23.04.05 2:22 am

Hi all

I would like to have my urls

(eg http://www.mysite.com/products/index.php?main_page=product_info&products_id=3 )

show up as:

http://www.mysite.com/products/ OR simply http://www.mysite.com/products/index.php.

Sometimes these links are generated using the GET command, other times they are direct links.

Any suggestions gratefully received.

PS I am not a php whiz by any means, so please be kind. :)


speeke - 23.04.05 2:23 am

Hmmm - the 'preview my comment' and 'add my comment' buttons appear to do the same thing, so sorry about the double post!


cor - 23.04.05 12:53 pm

The buttons do the same thing dude, it's the refresh button that reposts your comment (doesn't your browser warn you that you are about to do this?) no worries, I just delete 'em!

To the question, I'm not sure if I understand it correctly.

The links are generated by your php, the form of the links is the crucial point. Either they are "flat", like http://www.mysite.com/products/socks/wool/ or "dynamic", like http://www.mysite.com?section=products⊂=socks&type=wool

First decide which you'd like to use. You would be best to stick to one or the other, at least for the particular section/site/page we are theoretically discussing.

The .htaccess side of things (la raison d'etre pour cette page!) will catch ALL links and transform them to your requirements. I will presume you want nice user-friendly "flat" links to appear on your page, so really it's simply a question of working out what they will be, and putting together some regex to catch them.

I suspect that you are really looking for php advice, but I'm still not ready to go public with that part of the site! smiley for :geek:

;o)


mikolaj - 23.04.05 10:12 pm

since I'm using MySQL on my site, i have one main template -- the index.php -- and all pages are created with ?page=aboutus for example. since i would like to convert to a friendlier /aboutus/, which is easy, i've run into a problem.

I set the .htacccess as
RewriteRule ^(.*) /index.php?page=$1


which works for what i'm going after; but now all images and scripts stored under the root directory is considered a ?page=$1 (!)
(like, /images/ and /css/)

I want the site to be "site.com/aboutus/" and not "site.com/go/aboutus" (which can be done and fix the root-directory-problem.)

what do you suggest?


armadillo - 27.04.05 4:38 pm

I have scoured the internet for days, and nothing I've found or tried has worked. What I want to do is restrict access to the music files in a directory to only those who link the files directly from my site. In other words, I want to make it impossible to get these files if you simply enter the url in the location bar, so the only way you can get them is to use the link on a page I've set up. Is there a way?


cor - 27.04.05 7:01 pm

armadillo, see HotLink Protection (above). If you also want to prevent direct (typed-in) access, remove this line..
RewriteCond %{HTTP_REFERER} !^$

although be warned, some browsers don't send correct referer information, and they won't be able to download your files, even if they do use your special links.

There will be more complex ways to do this. For instance, you might want to set a cookie with your page, and read it again with the .htaccess file in the same folder as the music files, at least then they would have to have visited your page. An example (nicked from webpimps.com, when it was still around)..
Cookie Password Protection

Make a folder to be protected.
Then upload a .htaccess file with the following lines.

RewriteEngine on
RewriteCond %{HTTP_COOKIE} !^.*access=granted.*$
RewriteRule .*$ http://www.yourexample.com/loginerr.htm

Its a good idea to change access=granted to your own "password" to stop
people from being able to guess it. Create a file and call it whatever
you like (some hard to guess), put it in a public folder.
<HTML>
<HEAD>
<SCRIPT>
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}
var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
setCookie("access", "granted", now);
</SCRIPT>
</HEAD>
<BODY>

</BODY>
</HTML>

The code above will create the cookie to be read by the htaccess file.
If the visitor hasn't been to this page, they won't get access. 

If you felt kinky, you could even rotate the values with php.
You might also want to take a look at this.

oops! mikolaj missed another comment! smiley for :roll: your command looks like it would send the server into a spin-cycle! But assuming that it does work for you (tell me how!) you could try a couple of rewritecond lines to match the affected directories..

rewritecond %{REQUEST_FILENAME} !^\/images\/(.*)$

or else, perhaps only do the rewrite when the requested file doesn't actually exist..

RewriteCond %{REQUEST_FILENAME} !-f

There are a few ways to do this, but that should work okay.

for now..

;o)


cor - 27.04.05 7:32 pm

I was thinking about this dynamic site thing and I thought I might put it all together for other interested (yet clueless) souls out there! if you want to try this sort of thing out for yourselves, here is a miminimum ruleset, as I see it..
# for "dynamic" data-driven sites..
options +followsymlinks
rewriteengine on
rewritecond %{REQUEST_FILENAME} !-f
rewriterule ^(.*)$ http://mytest/index.php?page=$1 [nc]
"mytest" is a fictional testing domain, running on your own machine!
Your index.php file might start by containing the following..
<?php
echo 'Yo! Page = '.@$_GET['page'];
?>
to check on the variable being passed. If you requested "http://mytest/img/toolbar/foo.jpg" and that file doesn't actually exist, you will get..

Yo! Page = img/toolbar/foo.jpg

in the resultant page. Then it's just a question of splitting the path variable ($_GET['page']) into its elements.

;o)


mikolaj - 28.04.05 3:50 am

Actually, I got it:
RewriteRule ^({^/}+)/$ /index.php?page=$1 {L}

(for whatever reason I can't add a bracket in the code -- so i used a "{" in it's place.)

See it in action:
http://www.mauiweddingmusic.com/


cor - 28.04.05 3:16 pm

Bandwidth Limit Exceeded!

That's recursive rewrite rules for ya! smiley for :lol:

The [L] simply prevents mod_rewrite from processing further rules, but it won't always prevent a recursive loop. (to do a square bracket with bbcode, double it; [] <- I did four there!)

Experimentation, trial-and-error is usually the way with mod_rewrite. The main thing is, it works for you, and while it may not work in every situation, most of us are only trying to deal with one situation, so hat's off, sir!

for now..

;o)

ps.. While experimenting with rewrite rules (on your test server), in particular, with rules that aren't working, it's often useful to add the following lines to your server config (httpd.conf)..
#
# ONLY FOR TESTING REWRITE RULES!!!
# NOT FOR 'LIVE' SERVERS!!!
RewriteLog "/home/test/rewrite.log"
RewriteLogLevel 9

The log location is up to you, the log levels probably go from 1 (almost no information) to 9 (a lot of information). Remember to disable (comment out) once you get the rule working!


mikolaj - 29.04.05 4:54 am

ah, the log-- that's a good idea. thanks.
as for the bandwith, we have mp3 samples that's probably eating it up.


cor - 29.04.05 10:56 am

And impressive MP3's at that! If I ever have a Maui Wedding, I'll be in touch! I was just kidding about the bandwidth, I don't think recursive rewrite loops count towards bandwidth usage, mind you, some web hosts smiley for :eek: ... not mine! they rock! smiley for :D

And yes, the rewrite log is very often overlooked, even by folk who know it exists. It is extremely useful, but watch out! it fills up very quickly, especially during recursive loops!

;o)


euio - 30.04.05 7:42 am

hello, just a question. In shortening urls, u said

----------

this rule would transform this user's URL..

http://mysite/grab?file=my.zip

server-side, into..

http://mysite/public/files/download/download.php?file=my.zip

------------

should public, files, download folders be existent to use the shortening of files?


cor - 30.04.05 2:50 pm

yes and no.

For this rule, yes, it points to an actual file, "download.php", which must exist, and so all the folders it lives inside. There are no rules following this one, so it has to point to somewhere real.

But rewrite rules themselves are not limited to "real" files, you can redirect to anything at all. Scroll up the comments a bit to see how we were creating nice "real" looking paths that are in fact nothing more that variables. You can redirect to "in-between" strings that will later be redirected again to "real" files, etc, etc.

I used "/public/files/download/" simply to demostrate how a very deep place (with its long URL) could be presented with a very short URL, but the possibilities are practically limitless.

It all depends on what are you trying to do...

;o)


Made up name ! :D - 01.05.05 5:40 pm

Quick question ... I've been trying to figure out a way to do this:

when people type http://www.mysite.com/?q=r&x=m it points to http://www.mysite.com/script?q=r&x=m

How can I do that ?

Thanks very much !


cor - 02.05.05 12:17 am

dude! that's the default action!

It should just happen. So long as ensure "script" is the file Apache would use as its index, ie "index.php".

l*rz..

;o)


Made - 02.05.05 2:17 am

Hey cor ! Thanks for the answer ! smiley for :D

Quick question though ... if script.php is not the index for apache .. is there a way I can make it point to that script ?

Thanks again !



cor - 04.05.05 4:45 pm

why would you want to do that?

I don't even think it's possible, by the time apache gets to .htaccess level (the last place it looks), it has already trandlated the path to /index.php (or whatever your index file is) so the only way around that would be to rewrite the index requests..
rewriterule ^index.php$ http://test.com/another.php [qsa,nc]

which just seems silly!

;o)


euio - 06.05.05 6:49 am

well, I actually think the whole article write up is rather a very useful reference. ^_^ I also think that you can better expand your article in the future by referring to more specific examples like explaining the .htaccess the wordpress publishes or any other blog softwares. ^_^

I've been visiting this page lately as I'm figuring out/experimenting on some of the topics you have tackled and honestly you've done a great job!

btw, I have a question regarding wordpress' .htaccess.
can you explain this?


cheers
euio


euio - 06.05.05 6:55 am

how come I can't post the code??? it says 'preview, tags don't balance'?

here it is again..
QSA,L


cor - 06.05.05 3:27 pm

looks like you were trying to use square brackets, this is a bbcode comment facility, and square brackets is tags! if you need to do square brackets, simply double them -> [[ ! ]] It's all in the instructions page, but I know, life is hectic! Alternatively, put code examples inside pre tags.

I guess you're referring to switches, "qsa" we just did, and means "Query string append", in other words, mod_rewrite will append your GET variables back onto whatever it outputs, and "L", means "last rule", in other words, no further rules will be processed.

I'm not familiar with wordpress, I have my own blogging software! But yeah, this is the page where I should mention that if you install corzblog, and you get a 500 error from your server, try removing the .htaccess files!

l*rz..

;o)


Made up name ! :D - 07.05.05 1:43 pm

Hey cor, I figured out a way to do and I wanted to do .. I just wanted to say thanx for you help and patience ! smiley for :D

See ya !

P.S. Great job on this article !


cor - 07.05.05 7:31 pm

let's see it then!

;o)


euio - 09.05.05 12:07 am

hey cor!
just a quick question...
what if I put all the images under www.site.com/i/ directory, and when I call any image via 'img src' or 'url()', no matter where the html/php file is (say under www.site.com/files/index.php), I want the the browser to automatically read 'img src=/i/image.jpg' or 'url(/i/image.jpg)' NOT as 'www.site.com/files/i/image.jpg' BUT 'www.site.com/i/image.jpg' when 'ww.site.com/files/index.php' is loaded? ^_^

euio


Made - 09.05.05 2:51 am

Simple:

RewriteRule /?$ script.php?%QUERY_STRING

:)


cor - 09.05.05 5:54 am

yeah, Made, I like it, elegant! Believe it or not, it's what I tried first! But like I said earlier, that command just throws the server into a loop! at least all the servers I have stuff on, but as ever, ymmv, so nice one!

euio, surely I don't understand the question!

Web browsers translate partial paths automatically! If you look at the source of my front page, you'll see the blog image link, for instance, looks like src="/img/bloggerlogo_tn.png" but the browser knows that really means https://corz.org/img/bloggerlogo_tn.png, and grabs the correct image, no problem! HTML 101, dude!

Any link presented to the browser without a leading slash is interpreted as a relative, and any link with a leading slash is interpreted as being "from root", so the browser will add the "http://example.com" part itself. I'm guessing you've been oscillating between relative "./img/img.png & ../img/img.png" and absolute "http://mydomain/img/img.png" linking. But there is a middle path!

If you are looking to simplify things at the coding stage, why not put the paths into variables and just use them wherever you need to place content. I always start any page by declaring lots of variables like $root, and $domain, and stuff, so I don't have to keep typing them.

$img_path = 'path/to/images/';

Though if you're simply using /i/, that does seem kinda retrograde!

;o)


euio - 09.05.05 7:40 am

^_^you pretty much answered my question actually when you said

"Any link presented to the browser without a leading slash is interpreted as a relative, and any link with a leading slash is interpreted as being 'from root'"

The instant I picked up what you mean, I realized my question really was confusing...

misconception:

example scenario... given 'http://site/' as the root...

I load index.php

http://site/folder1/index.php (note folder1)

and in it I call for the image.jpg with leading slash

img src="/imagefolder/image.jpg"

With that I mistakenly assumed that "/imagefolder/image.jpg" = "http://site/folder1/imagefolder/index.php"

which is completely in contrast to your explanation... which suggests that...

since the root is http://site/, hence, "/imagefolder/image.jpg" is equivalent to "http://site/imagefolder/image.jpg"

well i think it's clearer to me now ^_^

euio


DJDeals.com - 12.05.05 4:41 pm

Does anyone know a way to have .htaccess alter a URL so that if someone clicks a URL with an extra part appended to it, our .htaccess will remove the appended portion and send them to the correct URL?

This post I sent to google may explain what I mean in better terms:





cor - 12.05.05 10:28 pm

I'll tell you how if you remove that obscene scrolling javascript from your website! smiley for :lol:

;o)

ps.. look at the status bar when you're browsing around corz.org! That's how you do the status thing! static!


sol - 13.05.05 7:31 pm

Cor, You helped me with the [QSA] option, which works beautifully. Do you know of a way to pass along POSTED values to the redirect? Thanks.


cor - 14.05.05 8:47 am

hmm, I can't think of a way off-hand. But it probably makes a lot more sense to grab them from inside your script, like this..
<?php
$array = $_POST;
echo'<b>POST variables</b><br>
<pre>',print_r($array),'</pre>';
?>
and use them as normal. So long as it's not a permanent redirect (301, etc), they should still be available (along with $_GET variables, ENV variables, etc) in your target script.

;o)

ps.. of course I could have just done "print_r($_POST)", but I wanted to take the extra step to quickly show how to get these babies into a regular array, easy-peezee.


cor - 14.05.05 1:02 pm

It just crossed my mind that not everyone uses php! (why? I don't know!) but these are server variables, so you can get them out with any old api, just look for HTTP_POST_VARS or similar in whatever language you're using!

;o)


arc - 14.05.05 8:00 pm

cor, nice article. My problem is an URL with a variable number of &keys=variables attached. How do I handle this with RegExps? Say:

script.php?key1=value1&key2=value2&(... up to 10)

gives me:

foo/key1/value1/key2/value2/script.html .

How to convert this (back)?

Do you have an idea?

-- arc


cor - 15.05.05 8:44 am

well, arc, it all depends what language you are using server-side. If it's php, there are many ways, check out the pathinfo function. Something I often use myself is this..
$path_pieces = explode("/", $_SERVER['PHP_SELF']);
"explode()" being in my top three most useful functions!

other than php? erm ...

;o)


arc - 16.05.05 12:41 pm

Thanks for your speedy answer.

Sorry, I did not stress that my problem is to re-convert the data. I know how to generate
it; I will use some kind of post-processor in my application for that.

But I don't understand how to decode/transform with regexps lists with an arbitrary number o encoded key-value pairs. You know, in web-applications there is sometimes only one key-value pair transmitted, sometimes more. It would be pretty inelegant to write, say, 10 times a RewriteRule, if the number of key-value pairs is between zero to 8.

In rules like below, the number of backreferences is restricted, because regexps are not recursive (I think):
RewriteRule ^files/(.*)/(.*).zip /download.php?section=$1&file=$2 [nc]

Or is there some magic way around -- some kind of "dynamic backreferencing"?

Thanks.

arc


balkrishna - 16.05.05 3:46 pm

Hi need help

I had written this code on my .htaccess file and uploaded on root, but this code is not working. Why?

Guide me.



cor - 16.05.05 10:57 pm

arc, I don't understand. I suspect what you are attempting to do is something very difficult, when a much more simple solution exists; ie, pass the whole lot to the script and let the script work it out.

balkrishna, what code?

;o)


Sboa23 - 17.05.05 12:14 am

I want to do a mod-rewrite to convert:

http://www.oursite.com/cgi-bin/some.cgi?category=22

to show as

http://www.oursite.com/22.html


and how would I get:

http://www.oursite.com/subfolder/page.shtml

to show as:

http://www.oursite.com/page.html

With a mod-rewrite? Can it be done with one line of code? Or a line for each event?

Thanks,

Rob James


cor - 17.05.05 8:31 am

Rob, you're not trying! The first example looks complicated until you turn it around and say, how do I get mod_rewrite to transform this..

http://www.oursite.com/22.html

into this..

http://www.oursite.com/cgi-bin/some.cgi?category=22

which is a real simple find-and-replace type rule..
rewriterule ^(.*)\.html$ http://www.oursite.com/cgi-bin/some.cgi?category=$1 [nc]
Try looking at the second one the same way; it's just as easy!

;o)


Sboa23 - 19.05.05 3:20 am

I see what you are saying; however, I am trying to make dynamic urls static using a mod rewrite.

I am trying to make the http://www.oursite.com/cgi-bin/some.cgi?category=22 drop the subfolder and rewrite everything after the "=" into a static html page.

Then one to make http://www.oursite.com/subfolder/page.shtml
drop out the subfolder and just show the page after the domain.

and possibly convert the shtml pages to html pages using mod rewrite.

I understand the above postings have been about making static pages look like dynamic; however, I am trying to do just the opposite.

I am actually trying to drop information and only rewrite some of it. I am trying to get a understanding of the drop part.

Is there documentation some where on the web?

BTW: I installed Tiger on my G3 800 laptop and a old G3 300. It is amazing that the Mac OS keeps making the boxes faster. Then again more ram helps too.

Thanks,
Rob James


cor - 19.05.05 9:11 am

Firstly, no, most everything above is about making dynamic pages look static, that's pretty much the main reason many folk start using mod_rewrite.

Sboa23
I am trying to make the http://www.oursite.com/cgi-bin/some.cgi?category=22 drop the subfolder and rewrite everything after the "=" into a static html page."

The rule I put in my last post will do exactly this!

Sboa23
Then one to make http://www.oursite.com/subfolder/page.shtml
drop out the subfolder and just show the page after the domain

Again, real simple regex..

rewriteRule ^(.*)/page\.html$ /page.html [nc]

Easy! Quite possibly I simply misunderstand you, I've read your post maybe six times and I'm still no closer to figuring out exactly what you're trying to do.

Let's break it down. These are questions I'd like you to answer..

1. user clicks a link on your page. Is that link written as a static link or a dynamic link? (static: http://my.com/22.html or, dynamic: http://my.com/cgi-bin/some.cgi?category=22 ) I mean the actual text appearing on the actual page the user gets in their browser. (static/dynamic)

2. What is the URL of the page, the actual real physical page that you want the user to be sent to? (Full URL)

What it looks like you are trying to do is real easy regexp, but like I say, I may simply not understand your exact needs.

As to OS X, that's simply because it started out as a hugely inneficient mess of code, and slowly (actually, not so slowly) the apple engineers are making it cleaner, faster, much more efficient. It'll level out pretty soon methinks.

;o)


Eric - 21.05.05 2:08 am

Made wrote:
Quick question though ... if script.php is not the index for apache .. is there a way I can make it point to that script ?

Yes, that can be done in .htaccess like so:
DirectoryIndex script.php index.html index.php index.phtml index.htm default.htm default.html

That's all one line ... order from first to last determines default precedence.

You can even take it a step further tell PHP to process a file with no extension like so:

<Files script>
ForceType application/x-httpd-php
</Files>



cor - 21.05.05 6:40 am

I'd taken it as read that anyone messing with rewrite rules would be aware that you could change the default index filename! As you quoted, Made said "...if script.php is not the index for apache .. is there a way I can make it point to that script ?", so surely HE already knows! He wasn't asking to change the default index file, was he? Maybe I misunderstood.

And aye, it's true that you can get php to parse any file, with or without an extension, but forcing php to parse files with no extension is a risky business.

The biggest trouble is that webmasters often put in wee hacks like this, and then forget they are there. A month or two later, some other function is added, lets say, an upload facility, and before you know it you have one enormous security event just waiting to happen. I've seen it happen!

Also it's sloppy, file extensions are there for many good reasons. If someone wants to present dynamic links as http://mysite/foo/bar, then they can achieve this with mod_rewrite, that's why we're here, there's no need to have an actual script called "foo"! In a UNIX environment, extensionless files are generally executable binaries.

It's not evil, as such, to do this in a webserving environment, but there are a few caveats..
  • DO specify an exact filename, in this case "script"
  • ONLY apply this in directories where it is strictly necessary
  • DON'T use extensionless scripts when a rewrite rule will do the job

Remember, If you put something like that in your main .htaccess file, it will apply to your whole site.

At the end of the day, there are better ways to do these things, and I've still not heard one valid reason to use "extensionless" files in a server environment (though they are super-useful on your windows desktop, mapped to regular text files, for "notes".)

As a sidenote, if MultiViews is enabled for a directory, and you have a file in it called file.php, simply requesting "file" will return "file.php", thanks to Apache's "content negotiation". check it out

I'm not having a go, I just feel strongly about this!

;o)


wowbagger - 26.05.05 7:47 pm

this page, alone, has helped me understand this so much! thanks a lot!


Odilon - 01.06.05 5:47 pm

Thanks for the run down on rewriting.

Whilst I can get some of rules working for my site I can't get all.

I'm building a new site. The old site has A LOT of pages with no file extension. For example, I'm trying to get /page_name redirected to /new_page/file.php

Would I need to create a rule for each page? At the moment it seems to choke on the fact that no file extension is specified.

Any clues on how to overcome this?
Odilon


Slava - 08.06.05 2:42 am

Hi, I have this: search.vulk.com/index.php?search=one+two+three
But some time it can be just search.vulk.com/index.php?search=one+two
Or search.vulk.com/index.php?search=one

It varies. so how can I make it look loke like: search.vulk.com/1-2-3
Or search.vulk.com/1-2
Or search.vulk.com/1
??
Thank you for your help.



cor - 08.06.05 2:54 pm

Odilon, my first thought, perhaps a lateral one, is "why not ADD extensions?". You say you are building a new site, so don't continue this bad habit, eradicate it!

If this is not possible for some reason (why, oh why?), what you ask is simple, one blanket rule redirecting to /new_page/file.php, though you don't say how you would like the old location to be incorporated into the new location, for instance, /new_page/file.php?page=page_name, and the blanket exaples (above) would do that.

If you give us more idea about exactly how you need the final request to look, I could cook a better rule maybe, but I still recommend you ADD EXTENSIONS! smiley for :ken:

Slava, pfff, no idea! If I get a minute I might look into it. You might want to ask one of the guys at forumn.modrewrite.com (see useful links section above these comments) To be honest, I'm not sure I understand exactly what you're trying to do.

;o)


sol - 09.06.05 5:47 pm

Cor,

I am rearchtitecting my company's site, images were all over the place, I have consolidated them to one image folder, but want to make sure any all references to the files are redirected to this location.

is it possible to say write a condition or rewrite that when any call to any .gif, .jpg, .swf, .png file it redirects to this folder and same file name?

~SOL


cor - 10.06.05 5:47 am

Well sol, something like this..
rewritecond %{REQUEST_URI} ^.*\.(gif|jpg|png|swf)$ [NC]
RewriteCond %{REQUEST_URI} !^/?img/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*)\.(.*)$ http://example.com/img/$2.$3 [L]
would do the job. Any requests for images that don't exist will be redirected to an image of the same name inside a folder named "img" in the root of the site. TADA!

There's some superfluous digits their, I'm sure, and it won't catch old links to images in the root of your site (who keeps images in the root of their site?) but will otherwise work fine, and won't send your server into a spin-cycle, either. Test locally first, of course.

Note, the file existence check is the last of the the rewritecond lines, so only files which have been matched by the first two rewritecond rules will have this check performed. This will aid server performance if you're dealing with lots of queries.

If you were feeling kinky, you could write it like this..
rewritecond %{REQUEST_URI} !^/?img/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*\.(jpg|gif|png|swf))$ /img/$2 [L]
which is niftier (those nested brackets are soo sexy!), but still doesn't deal with old bad links to images in your root folder. Surely no one has those! smiley for :blank:

Oh OKAY!
If you must  have links to images in your root, you could add this..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\.(jpg|gif|png|swf))$ /img/$1 [L]
to catch any requests that the previous section didn't match, and the only requests it didn't match were requests for images in the root directory, but really, who on earth keeps images in their root? smiley for :lol:

;o)

ps.. if you are adding this to the main server conf file rather than an .htaccess (you mention "my company's site") remember to use ^/(.*)/ ... instead of ^(.*)/ ... in the RewriteRule line.


George - 12.06.05 1:15 pm

I have a very simple problem which I can't seem to find an answer to. I am trying to redirect from a static directory www.example.com/info/ to non-static content www.domain2.com/info/?user=user. The problem is with Redirect command is that the redirected address always tails with a / making it www.domain2.com/user/?user=user/. Is there anyway I can merely remove the trailing / from the redirected address? Thanks.


cor - 12.06.05 2:01 pm

Show us your rules George, use pre tags for best formatting.

;o)


kirk - 19.06.05 3:04 am

About all I can manage to do is hang my site and give myself a big headache. What I'm trying to do is allow links and bookmarks to my site go to a category renamed 'Japanese-Tea-Cups' from 'JTC'.

The old link was: http://www.shopjapan.ca/Merchant2/merchant.mvc?Screen=CTGY&Category_Code=JTC

The new link will be: http://www.shopjapan.ca/Merchant2/merchant.mvc?Screen=CTGY&Category_Code=Japanese-Tea-Cups

So what I need to do is only change from 'JTC' to 'Japanese-Tea-Cups'. It needs to allow links such as http://www.shopjapan.ca/Merchant2/merchant.mvc?Screen=PROD&Product_Code=TM-171&Category_Code=JTC where "PROD&Product_Code=TM-171" varies but never has to be rewritten.

Eventually I'll do this for other categories than 'JTC' but I can just redo the conditions and rules for each new set of substitutions, right?

Any help would be appreciated.

Thanks,
Kirk


natasha - 20.06.05 3:28 am

hi fellows
i m newbie to this,so pls dont mind my following stupid request for help :)
--
i installed php and apache using PHPtriad, i have a folder for dynamic website that picks all the data from DB, and its location is c:\apache\htdocs\mysite,,,,,,,now i want to use this mod rewrite, for only this site not for others. so how can i implement this only for this site and also where to place my .htaccess file, by the way i searched my all pc i dont have any .htaccess file but i do have httpd.conf..........please help me in this ........
target: to chage (localhost\mysite\index.php?maincat=games&table=gm) like urls to localhost\mysite\whatever.html
please help me guys i will love to hear from you soon
bye

p.s. also tell me when i will upload it what i have to do further smiley for :D thanks



cor - 20.06.05 4:49 am

kirk, I really think you aught to be dealing with this inside merchant.mvc

You don't need a redirection, it's still the same script getting the variables! save yourself a headache and just catch the "Category_Code" variable inside merchant.mvc and convert "JTC" to "Japanese-Tea-Cups".

It's a one line string-replace.

natasha, you can put rewrite rules inside an .htaccess file, inside httpd.conf, or inside <vhosts> directives, and other places.

I don't know enough about how your URL's are generated, though, to advise on particular rules (dynamic domains? how?). Have a read through the comments here, it looks like you just need a simple redirect, but I can't really say.

PHPTriad has been dead for years, it's now Sokkit, no? a "simple" installer for apache/php/mysql. I never liked it, you're usually better off installing the three individually. The apache foundation, in particular, have done an excellent job with their installers.

;o)


kirk - 20.06.05 5:57 am

So, you're saying you can't just replace one part of a URL with a different term using mod_rewrite? Miva is now compiled and I can't modify any of the modules so that's not an option.

kirk


Peter - 20.06.05 10:18 pm

Hi,

Your trips and tricks helped me a lot. I've got one strange thing.
I'm hosting also some multimedia clips and hotlinking prevention works well when someone left click on the hot link. If someone right click and do the save target as option it will start the download without a problem. If they use a downloadmanager they will receive the redirected html. Is there a way to rule that right click and save target thing out?

TIA,

Peter

PS. I could not paste the .htaccess code due to some bbcode mismatch


cor - 22.06.05 5:00 am

kirk, I don't know what Miva is, but any server-side program that doesn't allow you to get inside and change it is Bad News. web programming is best done with an open-source language (like php), so before you get wired into finding a solution to this problem, you might want to consider finding a solution to the real problem! or recompile!

In the meantime, I guess something like this..
RewriteCond %{QUERY_STRING} JTC [nc] 
RewriteCond %{QUERY_STRING} !Japanese-Tea-Cups [nc]
RewriteRule ^(.*)$ $1?%{QUERY_STRING}&Category_Code=Japanese-Tea-Cups [l,nc] 
would do the job, our added variable will over-write the earlier value, but you definitely want to think hard about this closed-source server-side stuff!

Peter, the right-click doesn't pass referer information by the sounds of things. you can either a) disallow requests without referrers (remove that line, though be warned, older browsers might not be able to download your file), or b) tackle this inside the browser with one of the many right-click protection scripts (I DO NOT condone their use, by the way, but you asked)

But if the file exists, and is publicly accessible, there will always be a way round these tactics. If you want real protection, you might want to consider a secure download facility.

;o)

ps.. put code inside pre tags!


Peter - 22.06.05 3:01 pm

Thanks.

The right click protection scripts are not applicable (I hate them also) because the URL(s) hotlinked to my file(s) is on their own server.

I tried to remove the:

RewriteCond %{HTTP_REFERER} !^$

But that redirected all downloads (even my own)

Tia


exupery - 22.06.05 4:29 pm

Hi,

maybe you can help me, I'm pretty newbie...

I've juste set up a htpasswd protected directory on a distant server. But I'd like to have it working on a local server as well (different address for #AuthUserFile in the .htaccess).
What would be the conditional statement to put in the .htaccess ?

regards,
christopher


alexlop - 23.06.05 9:14 am

i've got a problem

i follow everything you said
everything is fine

but my apache (macos X.3.9) doesn't find the file

httpd.conf
<Directory "/Users/alexlop/Sites/">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>

then my htaccess

RewriteRule ^folder/(.*) sp_index.php?dir=./$1
RewriteRule ^file/(.*) sp_index.php?file=./$1
RewriteRule ^thumb/(.*) sp_getthumb.php?source=$1

and the error message in the browser

Not Found
The requested URL /Users/alexlop/Sites/gallery/sp_index.php was not found on this server.

(when i put http://127.0.0.1/~alexlop/gallery/sp_index.php?dir=./$1 in the whatever.php it works but not transparent whatever.php is in the browser url bar: i want it transparent)

so i hope iit's clear and that someone can help me
thx



alexlop - 23.06.05 9:36 am

to late
i found alone

the answer

RewriteBase /~alexlop/gallery/


cor - 24.06.05 1:51 am

glad we could help alexlop!

exupery, for what? you don't actually say what you want the rule to do! If you want more help, please explain some more.

;o)


Listo - 29.06.05 11:23 pm

Hello,

I asked this on the previous page and thought I would ask on this section as well. I would like to take the search term from the "referrer" at the time of request or from the web logs then redirect to a particular page relevant to that search term. Is this something you can do??

As before...Thanks in advance!!



cor - 30.06.05 4:52 am

hi Listo! Yeah, this is the page. scroll up to the Hot-Linking section. Those rewritecond lines should set you off in the right direction.

It all depends how complex a redirection you want to perform, you don't say, so it may be possible, maybe not! If you want to just, say, send a word to a search engine, a-la corzooogle.php?q=foobar, then it's easy enough. more details = better replies!

Don't forget to check out the useful links, the apache rewriting guide is an essential read for anyone messing with this stuff.

;o)


sol - 01.07.05 12:16 pm

Cor,

Your trick up above worked beautifully for me. Thanks! One problem... every now and again we get a rogue process where the redirect redirect indefinitely... (I cant seem to find the file that is triggering it). Is there a way to write one more condition on the rewrite to check if the redirected file exists before redirecting?
rewritecond %{REQUEST_URI} !^/?img/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*\.(jpg|gif|png|swf))$ /img/$2 [L]




Steve - 02.07.05 7:23 pm

I'm switching from Isapi_ReWrite to Mod_ReWrite.
Do you know the Mod_ReWrite equivalent of:
RewriteRule /article-(\d+)\.htm /displayarticle.php\?id=$1 [I,L]
(Turns http://www.mysite.co.uk/displayarticle.php?id=2884
into http://www.mysite.co.uk/article-2884.htm)

RewriteRule /list-([^.]+)\.htm /listarticles.php\?category=$1 [I,L]
(Turns http://www.mysite.co.uk/listarticles.php?category=Example
into http://www.mysite.co.uk/list-Example.htm)

I like your explanations...How about adding a section on rewriting dynamic url's to static ones?


cor - 04.07.05 2:28 am

Sol, I recommend you enable rewrite logging for a day and find out what's really happening before we fix it!* that rule won't loop, even if you have a missing file somewhere.

Steve, those rules look fine, only the explanations are back-to-front, they turn /list-Example.htm into /listarticles.php?category=Example, not the other way around!

As for the sections, I have thought about it, I have loads of wee snippets lying around that some would doubtless find useful, but they'd need double-checked and explained and well, I might do that, but it won't be this month!

;o)

references:

1:
a good trick is to enable the rewrite logging, and then set a search spider on your site. switch the rewrite logging off immediately after the test, or your server will suffer and die



flashGuy - 04.07.05 10:35 am

Hi guys, I would appreciate any help you might have regarding the following question. I have a flash website with two files “index.html” and “index.swf”. The search engine is indexing my index.swf file on its own with out html formatting and messes the look of my site. Is there a way to make .htaccess file that, when someone finds my index.swf file in search engines it will be redirecting people to html page that holds the swf file inside ? Thank you all ahead of time.


cor - 04.07.05 2:00 pm

flashGuy, the htm > php rule above is what you need, with a small modification. simple is best..
RewriteRule ^foo\.swf$ /index.htm [r=301,nc]
The "r=301" part will ensure the search engines permanently update all their links to point to the new location. You'll need to add the usual rewrite-enabling commands before it, of course.

I don't use flash, so I'm presuming the user doesn't need to access the flash file directly (like a php include). But if they do (and the more I think about it, the more this seems likely), you'll probably need to create a RewriteCond line like the hot-linking rules (also above), so it might become..
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com/ [nc]
RewriteRule ^foo\.swf$ /index.htm [r=301,nc] [nc]
Then it will only be loaded if called from your containing page, but that still wouldn't prevent direct access.

There's a more elaborate solution here.

Personally I'd completely deny access (see deny rules in the main article) to the flash file, keep it outside the webroot (completely), and stream it through php..

<?php
$swf
= fopen('my.swf', 'rb') {
    
passthu('$swf);
}
?>


If a php solution interests you, download a copy of my distro machine for example code. You'll likely need to add some HTTP headers before that to let the browser know what it's getting.

have fun!

;o)


flashGuy - 05.07.05 6:42 pm

Thank you for taking time to help me. I'll try it and let you know how it worked out. However regarding denying an access to .swf file is not an option for me since I do want search engine bots to spider my .swf files.


cor - 06.07.05 12:22 am

The idea is, to the browser, it's just the same as before, php's "streaming" is transparent. (check out the pajamas demo page, all these images are "passed through" php, but arrive in the browser exactly as they exist on the server, as regular images. They could just as easily be movies, or whatever).

Because the only way to see the swf is via the php file (which it could easily remain as "index.htm", or whatever"), you can check stuff before you stream the swf. You could check *anything*, in fact, and you could easily have one group of clients (search engines, whatever) allowed direct access to the swf, and another group that MUST load the containg page first (which could, for instance, initiate a php session, which is like a server-side cookie, a way to "recognise" users, temporarily, I use these a lot).

The swf itelf would have a url something like index.htm?file=my.swf which poses no problem to any search engines, but big problems to anyone who attempts to access the file without proper authority!

You could even go an extra step and have users enter some keyword to "unlock" the swf, though that's probably going to far!

If you decide to stick with .htaccess, remember, you can allow referrers, too (you allow your own domain, don't you!) so a line for google underneath the one for your own domain might be an good idea.

;o)


Xtreme984 - 08.07.05 11:07 pm

well, I got a phpBB board, and got a sweet dynamic signature mod.
however phpBB doesn't allow php extensions to be used as images or whatever.

I need the url to look like: http://domain/phpBB2/signature-$id.png
(where $id is the user identification number specified in the database)
while it's really http://domain/phpBB2/signature.php?id=$id

and I don't understand any of this. (main language being Dutch)


cor - 09.07.05 9:38 am

Your English seems fine to me.

Show me what you've got so far, and I'll see if I can spot where it's going wrong.

;o)




Xtreme984 - 09.07.05 10:25 am

that's the problem, I don't have anything since I don't know where to start. I've been able to make a normal redirect using the .htaccess
but maybe it's because of the fact that I use phpTriad.

I'd think along the lines of

RewriteEngine On
RewriteRule ^signature-(.*).png http://domain/signature.php?id=$1


cor - 09.07.05 12:02 pm

Well, Xtreme984, that's pretty close, try this..
RewriteEngine On
rewriterule ^phpBB2\/signature\-(.+).png http://example.com/phpBB2/signature.php?id=$1 [nc]
or something very like it.

Has phpTriad been revamped or something? That's the second time I've heard someone mention it recently and I thought it was long since dead! I usually recommend folk install apache, php, MySQL, etc from their source distributions, or binary distributions if you have no compiler available.

If you're a windows user, you have it real easy these days; all the big web server packages are available in easy-to-use binary installers. There's really no need for phpTraid, easyphp, Sokkit and the like any more. In my (and others) experience these "simple installers" usually end up being more complex and harder to maintain than the "raw" software packages.

;o)

ps.. this is also a valid image link.. http://example.com/phpBB2/signature.php?id=f0056&sig.png


Xtreme984 - 09.07.05 12:12 pm

well, I have had the phpTriad package for years now...

and on phpBB2 the php extension is excluded to be used as images for security reasons. if you can set me up with some instructions to set the environment up from the seperate binary distro's I'll do that.
I'll try your rewriterule, thanks.


cor - 09.07.05 12:34 pm

that's what I meant by my "ps". It's an old trick for using generated images on bulletin board (which, of course, do not allow .php extensions) usually we just do something like this..

http://domain/my/ampsig/amp.php/foo.jpg

the "/foo.jpg" is generally disregarded, the image generates just fine, and because it has a ".jpg" extension, phpbb doesn't mind at all. For more details, you might want to check this out.

If phpTriad works well for you, and is up-to-date, then it would probably be counter-productive to go uproot all that for the sake of a "pure" install. But if you're thinking about upgrading anyway, it's definitely worth considering doing exactly that.

Perhaps it's a cop-out, but the documentation that comes with these packages is quite excellent, not that you'll need it much; the apache team in particular have worked hard on their windows distribution, and you probably wouldn't have any troubles installing it.

There's some interesting webserver install fun at the secret comments page (not a secret any more now, is it?) that you might enjoy. In fact, feel free to drop webserver-related questions right there!

Whatever you do, make a complete backup of all your sites and apache installation and especially the configuration files and databases before you begin!

for now..

;o)


Xtreme984 - 09.07.05 12:39 pm

I don't need any backups or whatever, I'm just still using the webserver set-up as a thing to do my web development testing, however I'm considering to host my own website on my machine at some time (for my game development etc.)


cor - 10.07.05 10:03 am

it doesn't matter how trivial your current usage is, your setup works, and any working setup should be backed up! You may need to revert to it, especially if you go experimenting. Everyone needs a back-up, preferably scripted and nightly!

Sadly, most folk only learn of the importance of back-up after they've lost their valuable data. typing even one config line twice is Bad. Losing creative work is like DEATH. nuff said.

As for installing the "raw" server packages, really the best way to do it is just do it! Head along to the respective sites, apache.org, php.net, mysql.com, etc, and grab their latest Windows binary installers (I'm presuming you are a windows user). The installers are very good, and they will guide you through the process.

After that it's just tweak, test, tweak, test, and so on, like the rest of us!

Good luck!

;o)


Xtreme984 - 10.07.05 3:07 pm

ok, thanks for all the comments back, I'm now in the progress of setting up the new apache server. but one question, should I set up php as an apache module or as CGI-module?


Shaun - 10.07.05 5:37 pm

Does anybody know how to turn this ...

http://user.website.com/index.php?username=shaunG


... into this ...

http://user.website.com/shaunG


I can manage to get it down to something like:

http://user.website.com/index.php/shaunG


Or:

http://user.website.com/index/shaunG


But I haven't yet figured out a way to pass a variable without specifying a document, as you can with normal URL "GET request" format:

http://user.website.com?username=shaunG



cor - 11.07.05 7:02 am

Well Xtreme984, I prefer Apache module.

Shaun, this is another one of those backward things isn't it? surely you mean convert user.website.com/shaunG  into  user.website.com/index.php?username=shaunG?

I'll assume you want the ultimate target to be http://user.website.com?username=shaunG (the biggest challenge!) as in your last example. You could do it like this..
rewriteengine on
rewritecond %{QUERY_STRING} !^.*username.*$ [nc]
RewriteRule ^(.*)$ http://user.website.com?username=$1 [nc,l]
it's tricky, sure, but it does the job, converting a user-friendly..

http://user.website.com/shaunG

server-side, into..

http://user.website.com?username=shaunG

There are probably safer ways, but not if you want the variables bang against the domain like that (which I like, by the way)

have fun!

;o)


Xtreme984 - 14.07.05 6:28 pm

I kinda got problems with setting up the apache...
I can't connect to localhost anymore, if I can connect to localhost neither PHP or MySQL seems to be working.


michael - 18.07.05 6:49 am

Hello,

Thanks for the excellent page.

I have gone over many, and as good as your is I'm *still* confounded by what seems like it should be an easy task. I'm trying to rewrite the following, which will allow me to do a redirect to an admin tool without having the nasty cgi- url...

/cinesphere/admin /cgi-bin/WebObjects/ActiveMedia

Up to this point I've just put a folder named "admin" in the "cinesphere" folder, and had a simple index.html page which redirects to the ugly url. (I'm not normally called upon to be a web admin, as you can tell...) But since developers consistently fail to update their source with this shortcut folder/page, and I'm sick of recreating it, I'm trying to achieve a simple, elegant (permanent!) solution :)

Interestingly using only /admin (which does not exist) or /cinesphere (which does) as the start point for the rewrite works fine, but /cinsphere/admin...forget it...I have tried every permutation I can think of.

It's probably something stupid that I've missed, but it's driving me bonkers regardless.

Thanks!

michael


cor - 18.07.05 12:12 pm

hmm, on some servers, the path "/admin" is a reserved path, internally linking elsewhere, often an "alias" in the main httpd.conf.

none of the .htaccess rules begin with "/", though they would if they were out in the main httpd.conf file. You probably just omitted to "escape" the slash in the path you are trying to match. Something simple should do the trick, try..
RewriteEngine on
RewriteRule ^cinesphere\/admin\/?$ /cgi-bin/WebObjects/ActiveMedia [r,nc]
remember to put the slash back at the start of the line if you are adding this to your main httpd.conf file.

If that doesn't work, you must have some other thing interfering, in that case come back and we'll try something else.

;o)


michael sanchez - 19.07.05 9:43 am

Hi Cor,

I had actually tried what you suggested, as I think I've tried (as I said) every permutation I can think of, and it didn't work now or then.

Interestingly though, in poking around further, I discovered the following thing: if the folder /cinesphere *does not* exist, then the same redirect syntax works (even without the escapes, actually). As soon as I rename the folder to /cinesphere, it fails.

I am at a total loss...

Thanks,

michael


cor - 19.07.05 12:25 pm

Well, michael, that rule should work whether the folder exists or not, perhaps it has an .htaccess inside it, and this is interfering? Well, something is. If you have access to the httpd.conf (you must, if you are putting forward slashes at the start of the Rewrite rules) then check for aliases/script aliases that could be getting in the way.

Or perhaps you have other rewrite rules being processed before this one?

Or why not just use some made-up link name, like "http://myexample.com/cineadmin", and be done with it? I use these sorts of links to point to places all over the site, stuff like /bbtags, /engine, /source, and many others. If something works, use it! using "/admin" sounds like a great idea to me!

;o)


michael sanchez - 20.07.05 7:34 am

cor -

Alas, no. Position makes no difference. No .htaccess anywhere. 2 default Aliases (for /apache/icons/ and apache/htdocs/manual/) and one (default?) ScriptAlias for the cgi-bin.

Using the rewrite that works - /admin - has the drawback of breaking the shortcut for all who have previously bookmarked it as /cinesphere/admin. But it's what I will do for now since I have a whole life to tend to outside of the 6 hours I've poured onto this! :)

I'm pretty sure this is due to some interaction between tomcat and apache. Following this hunch, I created a temporary link to my home folder, and put another folder in there and attempted the same rewrite - and not to my surprise it worked just fine:

^/msanchez/admin/?$ /cgi-bin/WebObjects/ActiveMedia4

Although what that interaction IS?? - I haven't the slightest clue at the moment.

Thanks again for your help.

Michael


cor - 20.07.05 5:59 pm

Ahhh well, of Tomcat I have exactly zero experience, so maybe there is some interaction here, yes, seems possible, certainly that rule would work on any regular Apache setup.

I know what you mean about the whole life apart from these things, sometimes the working solution, though inelegant and clunky, is perfectly acceptible because the devoting the time involved in developing a stylish, cute, perfectly-formed alternative is not acceptable.

Some of my old rules are are crazy stuff, but they work! One day, perhaps I come back to them, wiser, and go FIXED! But it's way down the list!

All the best!

;o)


sim - 22.07.05 8:03 pm


Would you please suggest me
How to redirect/rewrite Apache to Tomcat server so that I can capture user name, who has logged in using Apache's .htaccess way of validating users.




tom - 28.07.05 1:32 am

I'm testing a site, where the temp URL loads the site from a sub-folder instead of root-level (eg., "somehostexample.com/~user/" instead of "www.thefinalexample.com/")

Problem is, all the absolute links in this (someone else's code) are breaking (where a link to the stylesheet or some forum topic are looking for the files from the root, instead of "root/~user/"

I can't seem to get the syntax right in the .htaccess file, so I can test this sub-foldered site before switching over to the final domain.

Please help!


cor - 28.07.05 3:28 am

Well tom, there's two ways to approach it. The first would be with a rewrite rule, something like the image rules in previous comments, essentially checking if the request contains "~user/", and if not, add it!

Personally, I'd rather create a temporary "virtual domain", and test the site exactly as it will be when it goes live, ie. "in the root". nose about this area of the site for more information on virtual domains.

sim, if this is a Tomcat question, see previous comment! If not, what?

;o)


michael sanchez - 28.07.05 4:35 am

cor -

I seem to have found the problematic "interaction."

First off I googled this excellent page which had the tip-off in it. It has a lot of great information on xml/tomcat/apache/linux:

http://www.csparks.com/XMLWithoutTears/ServerSideXML.xml

(comment was under the linked section "achieving complete transparency")

So the problem according to a comment on the above web page is that loading the tomcat module (there are several flavors, in this case mod_jk.c) after loading mod_rewrite will prevent mod_rewrite from rewriting tomcat directories. (exactly my symptom). It seems letting mod_jk load last means it executes first, and since the path I want to rewrite lives under the (apache configured) /tomcat/webapps directory, apache hands the URL off to tomcat before mod_rewrite gets the chance to parse/rewrite the URL.

The tomcat configuration file (which loads mod_jk) in my environments is brought in as an Include in apache/conf/httpd.conf. To fix I took the LoadModule and AddModule directives for mod_rewrite out of the usual LoadModule and AddModule list (in /apache/conf/httpd.conf), and moved them below the tomcat Include:
# Tomcat support
Include /opt/WebWare/Products/tomcat/conf/httpd-jk.conf

# ms - 050722 - mod_rewrite must be loaded after mod_jk (which is loaded in the above tomcat conf file)

# for url rewrites to work on tomcat/webapps/app_name paths
LoadModule rewrite_module     libexec/mod_rewrite.so
AddModule mod_rewrite.c
And la voila my friend...works like a charm.

Ahhh...the bliss of winning the battle :)

Thanks again.

michael


cor - 29.07.05 5:56 pm

Great news michael! Reminds me of early messings with WebDAV, how the module load order could completely mess things up, a real "gotcha", and well caught!

Now there's two places on the net folk can find a solution to their tomcat mod_rewrite troubles!

;o)


Tom - 30.07.05 2:59 pm

Hi,

I'm currently making a redirection script. I have tried lots of different RewriteRule's and read at least 10 tutorials on it, but still can't understand how to do it.

The RewriteRule which has got me the closest to success (500 Internal Server Error) is:

options +followsymlinks
RewriteEngine On
RewriteRule ^(.+)$ /redirect.php?id=$1


What I basically want to happen is when a user types "http://mysite/2gh56" it takes them to "http://mysite/redirect.php?id=2gh56".

Thanks,
Tom


cor - 31.07.05 2:01 am

Well Tom, that's a fairly basic rule. Try this variation..
options +followsymlinks
RewriteEngine On
RewriteRule ^(.+)$ redirect.php?id=$1 [l,nc]
If that doesn't work, something else is amiss.

Do you have mod_rewrite enabled on your webserver? And if so, are you allowed to set rules? That rule should work without problem, so if it doesn't work, you'll need to fix elsewhere before you start messing with the rules themselves.

have fun!

;o)


Freaky - 14.08.05 1:03 pm

hi , i need some help with a .htaccess , .htgroup & .htpasswd files i removed , eversince , now when i type in my adress bar xyz.co.za it comes up scrambled , but when i type www.xyz.co.za it appears correct , i am fairly new to this , trying to find out what i did and to learn my self.....any help will be appreciated.....

I think it is probably with redirecting......


cor - 14.08.05 4:02 pm

Freaky, trying both of those addresses gets me your hosting admin login. not good. If it's scrambled for you, maybe your browser is old and can't handle the https (secure pages).

Basically you've totally screwed up your hosting settings and you'll need to ask the admin to replace the files you removed, or better yet, replace them yourself from the backup you made before you deleted them.

You backed them up before you deleted them, right?

By the way, I need to know, exactly why did you remove these files?

;o)


Chris - 15.08.05 11:27 am

Hi,

my website at www.pcandice.co.uk uses cgi script. I have read through this excellent article and while I understand the ethics of how it all works I can't work out how to solve my problem.

I want to be able to goto page:

http://www.pcandice.co.uk/cgi-bin/commerce.cgi?cart_id=1121432917.14831&product=Head_Units&pid=28

but I want the URL to be displayed as something like:

http://www.pcandice.co.uk/cgi-bin/product/Head_Units/28.htm

(i am not too bothered about loosing the cart ID as I no longer use it for anything)

I also want to be able to goto web page:

http://www.pcandice.co.uk/cgi-bin/commerce.cgi?product=Head_Units

and for the URL to read:-

http://www.pcandice.co.uk/cgi-bin/product/Head_Units.htm


Hope I have given you enough info to understand what I want, basically it's to make the pages on my site appear to be static rather than dynamic and allow them to be indexed correctly.

Thanks for any help

ChrisJ



cor - 17.08.05 4:32 pm

Hi Chris,

Firstly, it's a myth that your pages won't be indexed properly unless they have "real" links. It was maybe true five years ago, but not now, not with any spider that matters.

Another thing, there's no need to put "cgi-bin/" in the user's URL, you can add that in your rewrite rule. Show us your code so far, and I can see where you're going wrong.

;o)

ps.. I don't understand what ethics has to do with mod_rewrite! well, apart from hot-linking rules! smiley for :D


Ant - 18.08.05 10:44 am

Managed to create a .htaccess file as follows:

RewriteEngine On
RewriteRule ^royalinn(.*) http://localhost/Places/subscribers/royalinn/

so that a shortened URL (http://localhost/Places/royalinn) displayed instead (losing the subscribers bit), but now the link to CSS and images is broken and its not displaying properly.

Any advice as to how I can do this without breaking any links.

Thanks


cor - 19.08.05 11:31 am

Well, Ant, something simple like..
rewriteengine on
RewriteRule ^subscribers/royalinn(/)? /Places/subscribers/royalinn/ [nc]
would do the trick, but I suspect it's probably not the best way forward.

The above rule improves on your example by firstly being portable - it won't just work on localhost, but any host - and secondly, the [nc] part catches "places" as well as "Places" for human-typed links, but it still doesn't fix your images, or does it? I'm guessing you have your images in a local (to the page) subfolder and are using "relative" linking. bummer.

It all depends how your images are linked. I use absolute linking (beginning with a "/", donating the root of the site) pretty much 100% of the time these days because it eliminates a whole bag of potential problems like this. relative linking relies on the browser being given the correct address to play with, and if you are using mod_rewrite to present "pretty" links, the user gets whatever they entered into the browser instead of the "real" address. Looks great, but kills those pesky relatively linked images. broken.

a good image link..
<img src="/Places/subscribers/royalinn/img/image.jpg" alt="seeme" />
which will work regardless of what rewrite rules are in place.

Some folk argue against absolute linking because they say "it makes your pages less portable", but in my reality I have found exactly the opposite to be the case. on any page of mine, the "/Places/subscribers/royalinn/" part of the absolute link would be generated by php, and derived from the precise relative location of the actual page delivering the link.

It's your call. If you stick with relative image linking, you'll have to use a proper redirect, that is, put make the [nc] into [r,nc] and have the "real" location appear in the user's browser, or else get funky with the mod_rewrite rules..
RewriteEngine on 

# requests to /Places/royalinn >> /Places/subscribers/royalinn/ 
RewriteCond %{REQUEST_URI} !(jpe?g|png|css)$ 
RewriteRule ^Places/royalinn(/)?$ /Places/subscribers/royalinn/ [nc]

# broken (relative) image links fixed..
RewriteCond %{REQUEST_URI} (png|jpe?g)$ [nc]
RewriteRule ^Places/royalinn/img/(.*)$  /Places/subscribers/royalinn/img/$1 [nc]
The first part catches requests that aren't images or css files and rewrites them invisibly to the real address. The second part catches only image requests and sorts them out, you do do the same for relatively linked css files too, if you like.

Lastly, if you have more "places", you might want to consider replacing "royalinn" with a regex condition, and using it in the target URL, would save you writing multiple rules when one would do.

for now..

;o)

ps.. the brackets () on the conditional rules are optional, as far as I know, you could probably use square brackets too [], experimentation is usually necessary on any particular setup, just because something works at home doesn't mean it won't bring down the entire server the instant you upload it to your website!

And if that's a shared server, other webmasters might not be too chuffed! smiley for :lol:


West - 22.08.05 7:38 am

Hi,

First of all great article!! But sadly getting to grips with regular expressions is a major task. At least for me.

My prob is that i want to call my index.php page something else, like other.php. But my host won't change this for me.

So, i need something in my htaccess file so that when a user visits my site with the url www.mysite.com/other.php it will actually grab my index.php file.

This is so that search engines will have my index page down as other.php but it is actually index.php. Can this be done??

Thanks for any help!!!


cor - 22.08.05 8:57 pm

I 'm not quite sure I understand the question (I often misunderstand htaccess questions, but often that's because, so does the one asking the question, and in my usualy merry way I will try and answer it anyway..)

It's very easy to use a different filename as your index, a simple .htaccess tweak (which really belongs in part one of these articles)..
DirectoryIndex other.php
That's it!

From now on, if someone accesses your site by "http://mysite.com/". Apache will automatically serve up "http://mysite.com/other.php" as your index file. No rewrite required!

However, the next two paragraphs of your question seem to contradict this desire, so I'm not sure quite what you want. Feel free to jump back here with more information if I didn't cover your answer already.

Quite why you want to do this, I haven't the foggiest idea. Is this some SEO secret that I don't know about?

;o)


West - 24.08.05 10:27 am

Hi Cor,

Excellent that works great. I have now read all the articles so that should prevent any more obvious questions.

Yes, the reason i wanted to change my index page is to do with SEO but it's no great secret. Certain search engines award a page better page ranking if a search word appears in the actual url of the page as well as in it's content. So if i was selling oranges a url like www.buyoranges.com/oranges.php would work well.

So now i'm giving away trade secrets hopefully you can help me with the final piece in my htaccess puzzle?

Members at my site have their own folder on my server. At present these are created in the root folder. Not good security wise. So i want to ceate my member folders in a directory called "users" off the root. For example "/users/exampleuser".In this directory they would have their own index page.

However i want them to enter a url like "www.mysite.com/exampleuser" and be directed to "www.mysite.com/users/exampleuser".

This all seems easy enough looking at your examples, so i used this
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)/(.*) users/$2 [nc]

But this raised an error.

Your example of
RewriteRule ^files/(.+)/(.+).zip download.php?section=$1&file=$2 [nc]

worked fine on my server. However i do not know the name of the start directory (in your example files) and my server raises an error 500 if i use
^(.+)/(.*)
or
^/(.*)

Any ideas would be much appreciated. Many thanks,

West.


cor - 25.08.05 3:41 pm

heh, yeah I know that one, and use it a lot, too. I was thinking too literally, trying to figure out how "other.php" would be of any use to you, SEO-wise. I guess "other" will in fact be some other word. smiley for :ken: with ya. By the way, page title is way more important.

It would be good to know what the .htaccess errors were, more useful, no matter.

In my example ( ...zip) there *is* no starting directory. "files" is completely fictional, and only appears in the user's URL, nowhere on site. The tricky part to your request is that you don't want users to type anything to signify that they require the /users/ subdirectory, so you'll need to do addditional checks if you want it to work.

I'd probably start with something like this..
rewriteengine on
rewritecond %{REQUEST_FILENAME} !-f [nc]
rewritecond %{REQUEST_FILENAME} !-d [nc]
RewriteRule ^([^/]+)(.*) users/$1$2 [nc]
at least, without knowing what your errors were, I'd start like that. try it.

The first bracketed condition ([^/]+) grabs everything up to the first "/", and uses that as your user folder. Additionally, any requests for files inside the user folder (the super-loose (.*)) will also be redirected, so long as the previous conditions are met, i.e. it's not a real file or directory existing at the location specified in the request. In other words, it won't rewrite requests for your existsing stuff.

It may need some tweaking for your setup, extra conditions, whatever, but that should get you started.

have fun!

;o)

ps. note. you may want to test the performance penalty of using "-d" and -f" in your setup.


West - 26.08.05 1:44 pm

Right well that works great. At least it certainly does the redirection. The only problem is that i want the redirection to take place without the users browser title bar showing the new path. That is, i want

http://www.mysite.com/someuser

to look in

http://www.mysite.com/users/someuser

But for the address bar to still display

http://www.mysite.com/someuser

I was going to use Alias, but a far as i can tell that is for folders outside of the document root, which does not apply in this case.

What do yo think?


cor - 26.08.05 7:18 pm

I agree, that's why I omitted the "r" from the switches. What I mean is, as it stands, the browser address bar will not change to the real path. Well, it shouldn't.

Did you change the rules, or add other rules after those, or something?

;o)


Leo - 28.08.05 6:48 pm

Hi,

I was wondering if someone could help or point me in the right direction.

I have a website which shows www.website.com/profile.php?id=5218 for profiles. The id=5218 being the specific user profile.

All the id's i.e. 5218 correspond to a username(i.e happyboy) in the database.

What i want to be able to do is that when users go to www.website.com/happyboy , the url will automatically forward to www.website.com/profile.php?id=5218 without this change showing in the user's address bar.

Any ideas how i go about doing this? Some help will be greatly appreciated.

Thanks,

Leo


cor - 29.08.05 4:13 am

Leo, I wouldn't even begin to consider using .htaccess for this, though it would be a fairly trivial matter with php.

Accessing a mySQL database with .htaccess? pfff.. pass!

Use .htaccess only to redirect to a php handler, then the handler can access the db, provide the correct page, etc.

have fun!

;o)


Leo - 30.08.05 6:07 pm

Thanks, i will look into changing stuff with the PHP instead then and see if i have much luck that way. And then use .htaccess to redirect.


Mufasa - 30.08.05 8:50 pm

Thanks for putting up this web page! I had never used mod_rewrite before and your examples made it very easy to understand.


cor - 31.08.05 7:30 am

Leo, there is no such thing as luck. Don't forget the [qsa] !

You're welcome Mufasa, I tend to keep lots of notes, and I have to try and make things simple so I can understand them myself a month later, so to go from that, to sharing some of what I've learned in the form of a web page often isn't such a big step.

The real work comes when you add a comment facility!

;o)


Buddy - 02.09.05 1:03 pm

Howdy!
I am slow catching on and will probably figure this one out, but the symbols have me confused. I am trying to write webpages and everytime I put in a url like http://www.mydoman.com/Billing/signup.php?clienttype=6&package=14 and I try to test it in http://validator.w3.org/check?uri=referer (I want all pages passed) it comes back with an error code of the & symbol.
How do I put the url in the page to pass the validator and what would the .htaccess rule be?




cor - 02.09.05 2:00 pm

Hey Buddy, I don't think this is an .htaccess issue, you probably just forgot to html-encode the "&" symbol.

If you want to validate, use "&amp;" (no quotes!) insead.

;o)


arjun - 06.09.05 11:46 am

The best info i have read about .htaccess. Kudos!


Ben - 12.09.05 2:56 am

Hi,

here is my problem : I have pictures and scripts in subfolders of my website. I don't want people to go and see directly them, so I tried to use htaccess with deny all... but then my website was not able to show my pictures and to use the scripts any longer...
is there any way to protect files in a subfolder without preventing my own website to use them?

Thanks!


cor - 12.09.05 7:58 am

Yes Ben, there are at least two ways..

The first is called "Hotlink Protection". See above.

The second, you carry on with the "deny all", and feed the images through a php pass_thru() function, or similar.

The first is easier, the second is more foolproof (but more work for the server) and really, for images, totally over the top, unless your images are very special.

;o)


cor_fan - 13.09.05 4:08 pm

Hi Cor,

I am new to this rewrite hope you can help.

I have an alias url eg. www.myaliasurl.com/site. The actual url is eg. www.actualurl.com/site.

When I type www.myaliasurl.com/site it goes to www.actualurl.com/site successfully. But it does not display www.myaliasurl.com/site on the address bar, it changes to the actual url. I do not want the user to see the www.actualurl.com/site address. Is this possible and how?

Many thanks


cor - 14.09.05 7:27 am

Hi cor_fan (interesting nick!), it sounds like you are performing a proper "redirection", with something like [r=301] after the rule (or even just the r), which tells the browser to reload the new location.

Drop the "r" (or "r=number") part and you will get what you want. Supposing your pages are well written (ie. php pages refer to "self" and not spcific URL's, etc), the myaliasurl.com should stay in the address bar, too.

If you pages aren't written so well, you may have to make other changes. Check your HTML for hard links pointing to www.actualurl.com, remove them, and it should all work as expected. I use this techniques all over the place, even rely on it..

https://corz.org/engine

No "r", see!

;o)

ps.. The old way of doing this sort of thing was to put your whole page inside one big frame, but frames are evil and best avoided for web pages.


cor_fan - 14.09.05 12:15 pm

Thanks for the help above.

I thought I could set it up on the actual site. I'll contact the guy who set it up for me and ask him to do that.

All my php pages include a constant file. This file has a BASE_URI. This const can be set to either '' (relative) or 'http://myaliassite.com/mysite/' (absolute).

Cheers
cor_fan

Another happy customer.


12_Centuries - 17.09.05 12:02 am

I love this! I'm using it to great effect. EXCEPT... something that seems very basic isn't working well... check this out:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^article/(.+) article.php?id=$1 [nc]
RewriteRule ^bio/(.+) bio.php?id=$1 [nc]
RewriteRule ^query/(.+) query.php?q=$1 [nc]
RewriteRule ^key/(.+) key.php?c=$1 [nc]


It all works great, except for the last line. Apache just stops responding to all requests for a minute or two whenever I try it.

A sample url:
http://localhost/key/2

should be turned into
http://localhost/key.php?c=2


Any ideas why that one won't work, yet the others do?

Thanks!


cor - 17.09.05 7:24 am

there's nothing in the rules that would cause the problem, chances are it's conflicting with some other rewrite rule (perhaps in the main server's httpd.conf or virtual hosts setup) or something else..

The word "key" could be conflicting with any one of a whole host of things, and is generally not a good choice for redirections and similar unless you have complete control of the server, do you?

If so, enable error logging, and rewite logging and find out exactly what the problem is. If there are other rules interfering, making the [nc] into [nc,l] will likely fix it, but only the logs can tell you for sure, I'm just shooting in the dark.

;o)


12_Centuries - 18.09.05 5:33 pm

Cor, thanks so much for the advice. I find this very fascinating. I don't have control over the server, only .htaccess, so I tried changing "key" to "area" and the same thing happened. So I then changed it to "pjb" (my initials) and it still happens. I also tried using [nc,l], but that didn't work either.

Can I enable rewrite logging from .htaccess?

This is a tremendous mystery!


cor - 19.09.05 4:17 pm

There's a keyboard shortcut or something in Firefox I really must track down. Twice now I've hit it (after hitting ctrl-b instead of ctrl-v - paste) and it jumps to a link, and when I go back, POOF! no form data.

That's three really solid posts I've lost this week.
LOSING DATA IS WORSE THAN DEATH!!!!!!!

DAMN!




12_Centuries - 20.09.05 4:11 pm

Shoot, I hope that data you lost wasn't a great response to my problem! :-)


cor - 21.09.05 11:38 am

hehhe no worries! I had to shoot out in a hurry, and my post here was the last thing I did (made me late, in fact! hahah), then lost it. lemme think now..

no, you cannot enable rerwrite logging from .htaccess, probably a good idea considering most folk are hosted on shared servers and rewrite logging is a serious drain on resources, but super-useful in situations like this.

It seems to me the sort of thing that someone with a particular experience would look at your post and go "Oh! yeah! that's such-and-such", but I don't have that experience.

I even tried recreating the conditions here to see if I could duplicate the error. no. Very strange indeed.

Have you tried creating a bogus rule after the last rule here? Nothing is too weird when it comes to mod_rewrite, it's possibly a "last rule thing".

I'd definitely try this on a home mirror (where you CAN enable rewite logging) to see if there's some other factor in your site that influences this. If it works fine at home, chances are it's your web hosts' issue, and you can mail them about it!

for now..

;o)


randomarrival - 21.09.05 9:56 pm

Can anyone tell me why this line is not working? Or have any ideas why it wouldnt work?

RewriteRule ^Merchant2/merchant\.mvc\?Screen\=CTGY\&Category_Code\=(.*)$ http://www.mysite.com/$1.html [nc]


Im trying to get this -
http://www.mysite.com/Merchant2/merchant.mvc?Screen=CTGY&Category_Code=test

To Go to This -
http://www.mysite.com/test.html


cor - 22.09.05 1:07 am

Oooh randomarrival! I had to stop and think about this one! smiley for :D

Personally I'd (again) handle this sort of thing with php. But I like a challenge, and this one looks like it requires some real mod_rewrite voodoo, but turns out to be simple enough when you get down to it..
rewritecond %{REQUEST_URI} !-f
rewritecond %{QUERY_STRING} Screen\=CTGY&Category_Code\=(.*)$
rewriterule ^Merchant2/merchant\.mvc$ /%1.html [nc]
The first line prevents looping, always handy, the rewritecond checks the query string (the ?something=something&somethingelse=somethingelse parts of a request) and matches the Category_Code variable in parenthasis so it becomes available to the rewriterule line via "%1". (a second (.*) match would be %2, etc)

It would be easy enough to add your "Screen" variable onto the new url, simple converting the [nc] flag into [nc,qsa] would append the entire original query string onto the new URL and it would therefore be available in the target page via the usual methods.

"/%1.html" could be "http://www.mysite.com/%1.html", if you prefer, though that's not usually necessary.

By the way, you can't check for query string variables with a rewriterule line - like you can with the regular "path" parts of the query - you need to access those bits via ${QUERY_STRING}.

have fun!

;o)


randomarrival - 22.09.05 3:32 pm

That all seems to make sense - but I appear to be having problems with it still.

Now I get a 404 Error and the address in the browser address does not change.

My main goal is to get the address just to display as the shortened one. Am I completely missing something?


n0ah - 27.09.05 8:51 pm

12_centuries (and of course you cor)

IIRC .htaccess ignores the last line in the file

so do this:

RewriteRule ^key/(.+) key.php?c=$1 [nc,l] <enter> (will create the line that will be ignored)



cor - 27.09.05 9:00 pm

randomarrival, the link that the user clicks, its HTML code, what would you like THAT to be?

;o)


randomarrival - 27.09.05 10:57 pm

The link address would be

http://www.mysite.com/Merchant2/merchant.mvc?Screen=CTGY&Category_Code=TEST

I would like the browser to display it as

http://www.mysite.com/TEST.html

I do also want to be able to enter the http://www.mysite.com/TEST.html address to display the http://www.mysite.com/Merchant2/merchant.mvc?Screen=CTGY&Category_Code=TEST when it is called. There would actually be no TEST.html file.


cor - 28.09.05 10:40 am

Thanks n0ah, I missed that when I passed through yesterday. Interesting, though I haven't a clue what IIRC is, my "last line thing" was right enough, then? Weird!

I always add an extra linebreak to scripts, etc, (except php scripts) so perhaps I've unwittingly circumvented this myself! Cheers for the heads-up! Now I gotta go find out what IIRC is, (apart from AOLbonics!)

Ahhh! randomarrival, I suspected as much! What you are asking is exactly the opposite of what you were actually attempting to implement in the first place. I did wonder why you wanted the long URL's! But mine is not to reason why..

okay, what you want is dead simple, something like..
rewriterule ^(.+)\.html /Merchant2/merchant.mvc?Screen=CTGY&Category_Code=$1 [nc]
would do the job. The short address will appear in the address bar. make [nc] into [r,nc] if you want the long address displayed instead.

have fun!

;o)


randomarrival - 28.09.05 3:10 pm

That works! Thanks - I guess I was just trying it backwards to start off with and that had me all screwed up.


cor - 29.09.05 12:06 pm

yup, it's quite common for folk to drop comments here along the lines of "I need to convert so-and-so into so-and-so", when in fact, they mean exactly the opposite, whereas you took it to the next level by actually providing an example! Good work! smiley for :lol:

It works now, that's always the main thing.

;o)


Ketlan - 29.09.05 10:08 pm

Excellent (and interesting) stuff. I've never even looked at htaccess possibilities before.

Here's a possibly tricky problem for you. I run several pages, blogs and so on from my own machine, access to them being http://IP/whatever...
I used to run a 3000 member forum but gave that up last year - but I notice that my access log file gets enormous over just a week or so, full of attempts to access the non-existent forum. I've tried using a simple redirect page and I've tried to simply block access so everyone who attempts it just gets a 404. Both of these work but my access log still gets massive because it's recording every attempt to get the damned forum. Apart from anything else, it's making it very difficult to keep tabs on when my other pages are being accessed.
Have you any idea how (or if) I could use htaccess to either stop recording these attempts to access a dead page or to deny the access without any record at all going into the log?

Any help would be appreciated. I've lost a few billion brain cells since I started trying to work this one out. smiley for :eek:


cor - 29.09.05 11:07 pm

Try this in your httpd.conf (or vhost block, perhaps you keep those in a separate file - wise) ..
SetEnvIf Request_URI "^/oldfolder(.*)$" dontlog
CustomLog logs/access_log common env=!dontlog

RewriteEngine on
RewriteRule /oldfolder(.*)    -    [F]
It's fairly self-explanatory, all requests to the old forum set an environment variable which we check against when deciding whether or not to log the request. If it's *not* set, we log, as usual.

Then we kick in with the mod_rewrite to stop all those requests dead in their tracks. (the "F" flag means "F* OFF!") The user gets the plain server generated 403 message. Forbidden. Without this, they'd get redirected to your regular error page which would - you guessed it - leave a log entry.

Simply replace "oldfolder" with the name of the old folder where the forum was. Restart apache and Bob's yer Uncle!

Hopefully that should save a few braincells.

;o)


Ketlan - 30.09.05 9:58 am

Hi again, and thak you for the speedy and helpful reply.

I placed your section in that part of the httpd.conf file immediately after the access.log set up. I left it all night so I could get a sensible idea of whether it worked or not. Sadly, it didn't seem to - though that may just be a problem with where it appeared in the conf file. I'll read the notes in the file (that'll make a change) and see if I can find a more logical location.

One question about the code snippet you provided. In the first line, there is
"^/oldfolder(.*)$"
Given that the forum was accessed by http://IP/forum/index.php, then all other pages followed the form http://IP/forum/section/threadID... and so on, should that line be something like
"^/forum/(*)$"?
I find the logic of all this stuff fascinating but I'm starting to think I'm too old for all this thinking.

Oh, one tiny little trick that might save some stress for any stray readers who have been irritated by having to close Apache down just to delete enormous access and error logs then restart it (when the log files are recreated fresh), is just to Open the files in situ, Select All/Delete, then Save, overwriting the file in the process. Instant empty log file without a restart.
I did say it was a tiny trick. smiley for :D


Ketlan - 30.09.05 12:18 pm

Whoops. That SO didn't work. smiley for :lol: That'll teach me to try to be clever. smiley for :roll:


cor - 30.09.05 4:18 pm

Heh, yeah, deleting the contents of a live log file can have *mixed* results, on some setups, with certain processes it does work. But often it will prevent any logging until you restart the process.

If you run windows (and don't have access to tail, etc) I recommend BearTail.

As for your rules, that certainly *should* work. Yes, replace both occurences of "oldfolder" with "forum". And then restart Apache. You must restart apache to get httpd.conf changes to take effect. (a "graceful" restart is fine)

It's preferable to put this in a vhost block, in fact using virtual hosts if almost always preferable in the first place. If you are placing directly into the httpd.conf (*ouch*) it's a little trickier. First ensure you are placing the rewrite rules inside a valid <Location> or <Directory> block, perhaps something like this in the main block..
<Directory />
    Options FollowSymLinks
    # etc, etc, the usual stuff
    rewriteEngine on
    rewriterule ^(.*)forum(.*)$    -    [F]
</Directory>
and ensure a) any existsing customlog directive is commented out, the one that probably reads something like..

CustomLog /usr/local/apache/logs/access_log common

(put your two new custom log lines after that section) and, b) that you don't have a real 403 page for that part of your site (perhaps specified in .htaccess) or else they will get redirected to it, and of course that gets logged!

Virtual hosts makes this (along with lots of other stuff) a great deal easier. Generally, I don't touch my httpd.conf after the initial setup-and-tweak, keeping my virtual hosts in a separate file. Load all the comments for more information about this, and browse around the /serv/ part of the site for details.

There's enough information there to get things working and if you persevere you'll get things exactly the way you want. But don't let that stop you getting back here if you get stuck!

have fun!

;o)

ps.. if you find the logic fascinating, you should check out the mod_rewrite guide (first link in useful links section) - mind-bending stuff!


Ketlan - 30.09.05 5:09 pm

That's great, thanks. I shall have a play around with that later this evening. I can see there's a LOT to learn about this stuff (and I thought I knew everything). smiley for :roll:
Thanks for your help thus far. It's very much appreciated. I'll be back soon - either screaming for help or (preferably) cheering. smiley for :D


DAV - 05.10.05 7:16 am

Thank you corz for helping me out
I have 1more query:
suppose my link shows www.example.com/product.php?catid=2.
I want to convert it to www.example.com/product/2 dynamically.
I've tried alot but no success. some of your code works but i want to convert it dynamically.

Plz help


cor - 07.10.05 4:11 pm

DAV, check my reply to randomarrival, a few comments back, same story.

;o)

ps.. all my code works. often it's clunky, inelegant, dumb, fat and plain old stupid, but it always works! smiley for :ken:


Manuel - 07.10.05 10:12 pm

thanx for all expleining
manuel


desperewrite - 08.10.05 6:06 pm

hi, good page and lots of hints! However, got a tricky htaccess issue and can't get my head round it - I read all the above but still no luck. Is it ok to post it here?


cor - 08.10.05 7:04 pm

sure, so long as we didn't just do exactly the same thing!

;o)


desperewrite - 08.10.05 7:25 pm

Ok, thanks for that. What I am trying to do is point my domain to a subfolder off my root in my webspace. I have set up subfolders with the name of the domain (but I don't think thank matters), e.g. www.example.com has a subfolder in my web space called www.example.com. I then set up a subfolder www.example.com/test. In there, I placed a default document so that it can be accessed without a long url. Now, when I try to access www.example.com through a browser, it all works great. However, when trying www.example.com/test through a browser, I get www.example.com/www.example.com/test in the URL. While the script works, it will cause issues later on with my project. I tried www.example.com/test/test.html which works fine, so I think it's something with the default document, but I am at a loss what to do.

My htaccess file is as follows

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} www.example.com
RewriteCond %{REQUEST_URI} !www.example.com/
RewriteRule ^(.*)$ www.example.com/$1 [L]

Any ideas what's wrong? Very much appreciate your help as it drives me round the bend...


Makc - 09.10.05 12:42 pm

Hello Cor,

Thank you for the article.

I've found an explanation and a solution for my trouble from your hint:
rewritecond %{REQUEST_URI} !-f
rewritecond %{QUERY_STRING} Screen\=CTGY&Category_Code\=(.*)$
rewriterule ^Merchant2/merchant\.mvc$ /%1.html [nc]


and i has adopted this code to my needs:
RewriteEngine on
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{QUERY_STRING} id\=reboo(.*)
RewriteRule ^index\.html err.html [nc]


Now all requests which contains string 'id=reboo' redirects to err.html
Spamadvertising is really s%cks :(

Thanks again and keep up the good work! :)


desperewrite - 09.10.05 10:58 pm

Cor,

Futher to my earlier posting, I also tried www.example.com/test/default.html which is the default document which also works without funny URL. So, it's something that's happening when accessing a folder that has got the default document but the default document is not in the URL, if this makes sense?
Thanks
Christoph


corz - 10.10.05 9:14 am

desperewrite, I suspect you are going about this the long way. The double www.example.com/www.example.com was just to throw me, right? Really it could be mytopexample.com/someother.com for simplicity, yeah?

My first thought was "yup, that would work", and it should, does. I even tested it to be sure (though without the rewritebase rule). But still, isn't this complete insanity???

If I understand correctly, you are trying to host multiple domains on one server. There are better ways to achieve this, virtual hosts for starters. Do you have access to the main httpd.conf file?

If you don't, doesn't your web hosting appliance (CPanel or whatever) allow you to setup domains on the system without resorting to .htaccess tweaks? If not, are you seriously considering setting up multiple domains on a host with such lousy software?

Feel free to send more enlightenment this way if I'm missing something, but I just get the feeling that something else is wrong, something apart from htaccess.

;o)

ps.. Makc, check out my bbcode parser - the new beta will be packaged and available real soon - dedicated to giving the buggers a hard time! smiley for :D


desperewrite - 10.10.05 9:33 am

Cor, sorry if I threw you - the double in the domain is a bit confusing, the rewriting that could happen could be in the format of www.example.com/mysubfolder/test/default.html where domain is the domain I want to access and subfolder the folder in which the code for this domain resides, so another domain would be in mysubfolder2 etc, for example. I don't have access to the main config file, yes, that would be easier. Nope, control panel does not give me anything either. I do want to host multiple domains on one shared space, yes. Yes, I realise there are other hosts that offer different options but this one is free, and there are other reasons why I would like to use them. So, if at all possible, I'd like to get the rule to work. Any suggestions?
Thanks

Christoph


boris - 15.10.05 3:47 pm

can you help with?

http://domain/index.php?cmd=upload

to rewrite to

http://domain/upload/


corz - 17.10.05 7:46 pm

Nah boris, you don't need help, it's too easy! And there are at least three examples of exactly what you want on this very page.

Ahh, desperewrite, another comment from the upgrade day, I had a feeling there's was another I missed. The first question is, if you don't have access to the main config, and you don't have a domain facility in your hosting panel, how will you get the requests to resove to the box in the first place?

Technically, your rules work. You could even go further and plant .htaccess file into the folders themselves, redirecting everything to the alternative domain; but unless you have a valid DNS setup, no amount of rewriting will get those domains served up.

;o)

ps.. I know a host that can do it all, sub-domains, parked-domains, offers shell access, stats, FTP, pop, smtp, cPanel, the works, $25/year. Interested?


Eelke - 20.10.05 2:32 pm

Cor, still wondering about IIRC? It means, "if I recall correctly" and is pretty commonly used. Thanks for an excellent write-up, I plan to put it to some use one of these days.


corz - 20.10.05 5:19 pm

hahah! yes, of course! I guess my aolbionics is slipping these days, it's generally frowned upon in the chatrooms I visit (apart from my own), which is a bit sad (and snooty) really, because it's a gr8 time saver!

Thanks Eelke, worry not, although the average lifespan of an internet page may be 100 days, this will still be here when you get around to the tweaking!

for now..

;o)


desperewrite - 28.10.05 6:47 pm

cor,

I have got a DNS panel all right, so domains work for the host. Yes, would be interested in hearing about the host you are mentioning...

thanks


shanX - 28.10.05 7:27 pm

Hi Corr!
Please help me with this problem.
I want to change the path of a file,
-from: http://domain/dir1/dir2/dirX/#1.file
-to: http://domain/dir1_dir2_#1.file.

I tried directive below, But it didn't work.
RewriteRule ^(.+)/(.+)/dirX/(.+).file $1_$2_$3.file

I think there's a little bug in it. Can u help me.
Thanks.


corz - 29.10.05 6:52 am

shanX, although theoretically, your not escaping the slashes and dots would appear to break this, in reality mod_rewrite wouldn't care much (a dot is  a character) and it would still work. Your problem is more likely the "#" character; don't use that in filenames, it's a page id reference, and belongs after the URL.

for now..

;o)

ps.. Didn't Firefox used to remember your form text areas? I just went to a page, hit "back", and lost this post (I had to do it TWICE!). Weird.


shanX - 29.10.05 2:24 pm

Thanks for earlier reply!
Actually I've used the '#' character as a reference to a part of the filename, for an example I'm gonna use this mod_rewrite on a directory containing a file list such as given below.
        -pics/Honda/Civic/thumbs/1.jpg
        -pics/Honda/Civic/thumbs/2.jpg...

So that it'd result to be
        -pics/Honda_Civic_1.jpg
        -pics/Honda_Civic_2.jpg

I tried using rewrites below as you suggested, but they didn't work.
RewriteRule ^(.+)/(.+)/thumbs/(.+)\.file $1_$2_$3\.file[nc]
RewriteRule ^(.+)\/(.+)\/thumbs\/(.+)\.jpg $1_$2_$3\.jpg [nc]
RewriteRule ^\/(.+)\/(.+)\/thumbs\/(.+)\.jpg $1_$2_$3\.jpg [nc]

Similar directives such as Hotlink preventions used in other directories are working.
Thanks Again!


corz - 29.10.05 5:29 pm

They are all pretty close, probably workable, too. This will definitley work..
RewriteRule ^(.+)\/(.+)\/thumbs\/(.+)\.jpg $1_$2_$3.jpg [nc]
and if it doesn't, something else must be wrong with your setup. (by the way, you don't need to escape the targets, only the regexp conditions)

The "-pics", I guess, is another one of your "references to something", but if by some chance you are actually referring to a real folder called "-pics", and these commands are going in an .htaccess file inside that folder, clearly the rule would instead need to be..
RewriteRule ^(.+)\/(.+)\/thumbs\/(.+)\.jpg /-pics/$1_$2_$3.jpg [nc]

If you still have troubles, please post the exact full paths of the URL's and targets you are using, saves me guessing.

;o)


shanX - 29.10.05 7:05 pm

Thanks for response, It's true without exact paths It leads us to confusions.

* Actual path : http://domain/pics/Jaguar/XK/thumbs/1.jpg
* Path expected: http://domain/pics/Jaguar_XK_1.jpg
* Variables to be tracked here are 'Jaguar','XK','1'
* .htaccess file resides in the 'pics' folder

I tried
RewriteRule ^(.+)\/(.+)\/thumbs\/(.+)\.jpg $1_$2_$3.jpg [nc]
but It throws up 404's, for either requests, actual and expected.
Thanks.


corz - 30.10.05 12:13 pm

I need to think about this. Not your rewrite rule, that's dead easy. But about why everyone, I mean, almost to a man, asks rewrite rule questions back-to-front! Like others, you even provide examples of your back-to-front rules, which I look at, and go, "yup, nothing wrong with that". smiley for :lol:

I've gotten so much feedback about how good this article is that I started to believe it all! smiley for :roll: I'll need to work on making this all real clear on the page. I'll sketch some updated copy for the main article methinks, right now, while I'm here. If you don't skip this, you'll understand things and be better able to cook up whatever rules you need in the future..

A Rewrite Rule is composed of two distinct parts. The "match" (the first part) and the "target" (second part). Optionally you can add a third section, rewrite flags, inside [square brackets].

rewriterule     Public URL     Private URL      [options]
                           >>>
rewriterule     something      somethingelse    [[whatever]]

The transformation happens left-to-right, which is exactly how the rewrite rule is itself written.

The first part is what mod_rewrite will try to match against. This is the real URL the browser is sending to the server. That's the URL that travels over the wires of the internet. That's the URL that appears in pages, links, perhaps generated by php, whatever, that's the URL the world sees. That's what mod_rewrite will be looking at when it tries to make a match.

IF and only IF it matches, mod_rewrite proceeds with the rewriting, transforming the original matched URL to the new "target" path, usually an internal URL, the real location from which the web server (Apache) will grab the file to serve to the client.

Now, shanX, armed with this information, and looking front-to-back this time around, can you see how easy your rewrite rule is? The browser will be asking for http://domain/pics/Jaguar_XK_1.jpg, so that is the first part..
rewriterule http://domain/pics/Jaguar_XK_1.jpg *something* [yup]
The real path, the target, where the file actually lives (the second part) is http://domain/pics/Jaguar/XK/thumbs/1.jpg
so our rule becomes..
rewriterule http://domain/pics/Jaguar_XK_1.jpg    http://domain/pics/Jaguar/XK/thumbs/1.jpg [better]
Now we simply replace the variable parts with some regexp conditions, mindful that we are already inside the "pics" folder..
rewriterule ^(.+)_(.+)_(.+)\.jpg    /pics/$1/$2/thumbs/$3.jpg [nc]
and we're done! No really, it's as simple as that.

Like they say, it's easy when you know how.

;o)


p - 08.11.05 3:30 am
Hey cor!
I'm having 2 problems...
1) I have a mod rewrite rule as follows...
 RewriteRule ^Download-Polyphonic-(.+)-(.+)\.html$ downloads.php?id=$1&name=$2 
this works perfectly fine...except when $2 (which is song name) involves a colon eg.. downloads.php?id=2&name=12:09 doesnt work instead I get download-polyphonic-2-12:09.html the root folder is not included >.<

2) How can I get rid of those annoying %20 that replace spaces in the song name, can i create a rule to write them into underscores?

Thanks!!


Fluxid - 08.11.05 3:30 am

Hi!
I'm new to .htaccess files, so i can't find any solution to my problem.

I want to rewrite this:
http://anydomain.pl/u/AbcUser
to this:
http://anydomain.pl/_index.php?s=user&user=AbcUser

I wrote this in .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^u/([A-Za-z0-9_\-]+)$ _index.php?s=user&user=$1 [NC]

and i've put it to root

but it's not working. when i try to get to /u/AbcUser server returns 404
here's line from log:
[Mon Nov 07 19:33:04 2005] [error] [client 127.0.0.1] File does not exist: D:/Server/Apache Group/Apache2/htdocs/u/AbcUser

as you can see i'm using Apache on my local computer to tests. Everything is working fine, but not rewrite.

httpd.conf is okay...

can you help me?

PS: also i like to add SessionID links when it's needed, so i also like to rewrite
http://anydomain.pl/u/AbcUser/?sid=#
to
http://anydomain.pl/_index.php?s=user&user=AbcUser&sid=#
when sid exists...

PS2: Sorry for my language mistakes

PS3: I've edited this post 4 times, it's too late for me... in my country it's 03:40 am...


shanX - 08.11.05 5:37 pm

Thanks for the explanation!


Judith Archer - 13.11.05 4:22 am

Good work, nice webpaqe.


corz - 24.11.05 1:52 pm

This page is Jinxed! Fortunately, I've taken to copying my posts into my scraps file as I work. I had to scroll for this..

Firstly, you can't have colons in filenames. period. But that won't stop the rule from working. The colon is part of the "name" variable, and will pass straight through to downloads.php, no problem at all. That rule is perfectly fine.

If you haven't sussed it yet, please show me exactly what URL (that is visible, external, yadda yadda) URL, the one that is seen and clicked. And exactly what you expect at the other end, along with actual error text you receive. Are you sure it's not your downloads.php that's giving the error, perhaps you are trying to pass this colon to the filesystem, which of course, won't work; you can't have colons in filenames.

And the spaces, well, %20 doesn't replace the space, that *IS* the space! You can't send a raw space in a URL, it needs to be encoded, which is what the %20 is. If the %20 is causing troubles for your downloads.php script, then that's a php issue, and a trivial one.

At the end of the day, it sounds like the back-end php needs some work, perhaps redesign. All this text processing should be done with php, not mod_rewrite, also a place to check for nefarious requests. And what's "polyphonic"? it seems erroneous, makes the URL so big the unmod_redirected version is neater!

By the way, it was your < and > that prevented you from posting. They need to be like [<][>].

have fun!

;o)

That was for "Person X", who's name got sucked into the htaccess vortex that swirls under this page.

Fluxid, your rules are fine, technically. It looks like the trouble is deeper. Do simple rewrite rules work? By the way, the sid= part can be easily handled with a RewriteCond line.

for now..

;o)


Wytze - 27.11.05 1:46 pm

Thanx for the article Cor,

I've been trying to get a .htaccess file to do a very simple redirect. But when the redirect occurs the url in the addressbar changes to the new (redirected) url.

I've been using this code

RewriteEngine On
RewriteRule ^$ http://tour.blabla.mijndomein.com/pro4.php


Adding a [P] makes the url fail, adding [R] doesn't make any difference. I can't figure out what I am doing wrong. Can anybody help me ?




corz - 29.11.05 2:36 am

Wytze, I suspect the rule itself is the problem. It's wacked!

You'll need to a) specify *something* to rewite, "^$" isn't valid, afaik, and b) do some rewrite condition to prevent the thing going into a spin-cycle (loop). Then your [r] will work, though better with [r=301], if it's a permanent redirection..
RewriteCond %{REQUEST_URI} !report.php
RewriteRule ^.*$ http://itest/report.php [r=301]
or something like that.

;o)


HJ - 29.11.05 3:11 pm

Hi everybody,

I've got a little problem with the encoding of the QUERY_STRING. This line causes the error:

RewriteRule ^prod\.html(.*)$ "http://www.example.com?%{QUERY_STRING}"

There are a lot of variables I've already encoded, such like this:

N%3DFz%26U3%3D3128%26U4%3D335%26U7...

Now, when I enter prod.html?N%3DFz%26U3%3D3128%26U4%3D335%26U7 get back a site like this:

http://www.example.com/?N%253DFz%2526U3%253D3004%2526U4%253D707

So ModRewrite has encoded it a second time.

How can I prevent this?



corz - 30.11.05 2:41 am

Have you tried [qsa] ?

;o)

ps.. see also [ne]




Christopher - 30.11.05 3:45 am

My website is down, i can not login to my control panel. The problem is i can not buy it again because it stil appear like a domain name regitred. Can i make a redirection of first site to another site? For example i want to redirect www.myexample.com to www.mydomany2.com ? So if i will try to open the first link, the internet browser to redirect me to my new link?

PLEASE HELP

ASAP


corz - 30.11.05 3:53 am

Christopher, to setup a redirection from the old domain, you need to access your registrar's admin page and redirect it from there (for instance, your GoDaddy admin page, if you use GoDaddy).

Normally you can setup simple web redirects, DNS redirects, whatever you need. But you need to have access to the domain setup. You don't need access to your old server, unless the domain name was somehow tied into your old hosting package with that server and they own the domain that you used to use, in which case you are probably screwed.

If the latter is the case, a polite email to their sysadmins might be a nice idea.

for now..

;o)


Henry ! - 02.12.05 6:43 pm

Christopher !! S.O.S. !! Help !

I must do a url rewrite for this:

http://www.site.com/aff/t.php?a_aid=michael&a_bid=affid

It is for an affiliate script... the url MUST have those 2 vars. or it won't work.

I've done this so far:

Rewriterule ^(.*)\.htm$ /aff/t.php?a_aid=$1&a_bid=f78ebc0e

But this will work for user michael only !!

And the url should be www.site.com/affusername

(not with .htm, but i took from your examples...)

Any workaround ?

Thanks !!


corz - 03.12.05 2:49 pm

no comment.

;o)


corz - 04.12.05 5:01 am

I had to come back and have another look, really, this isn't a joke, is it? smiley for :eek: I thought you guys were just teaming up for a wee spoof, but the more I read your posts, the more I think you could be two genuine, unconnected peeps with real   issues. It was the "Christopher !! S.O.S. !! Help !" that threw me! smiley for :lol:

HJ, if you're serious, fuxake! why did you html encoded the & symbol? That's nuts! Just asking for trouble. But anyway, you should still get N=Fz&U3=3128&U4=335&U7 out the other end, but instead of being separate variables, it will all be one big empty variable, and you'd have to sort that out in your target script. Something like..
$_GET: Array
(
  [N=Fz&U3=3128&U4=335&U7] =>
)
instead of the usual..
$_GET:  Array
(
  [N]  => Fz
  [U3] => 3128
  [U4] => 335
  etc..
Yeah?

And what's the quotes around the address? Does that work? Hmm. [qsa] would have a similar effect, because it is also working with these pre-encoded variables (why oh why?!?). If you must encode these variables, you might want to consider converting them to base64 or something, you could send it as ?SomeVar=TiUzREZ6JTI2VTMlM0QzMTI4JTI2VTQlM0QzMzU=, and then decode it at the other end. But, again, what's wrong with using regular variables?


Henry ! Oh Henry ! ...

After reading your post about ten times I think I might have an inkling of what you are trying to do - ten minutes I'll never get back! as they say on the imdb smiley for :lol: - simply, you need to have the affiliate id in the link for this to work. So the user clicks

www.site.com/affusername/f78ebc0e

or something like that. If you can't or won't put the affiliate id in the URL (and really, it's crazy that there are two id's for each account, name and id, why not just one of the two, like just the name? - by the way, I really don't know what an affiliate program is - just that it sounds like something I want to avoid at all costs - so perhaps this is a normal practice for those, whatever they are, but it's still crazy)

The alternative is to store a database of mappings, which would be accessible to a php handler script, it would have something like..

michael = f78ebc0e
spunky = f78eb13b
whipit = etc..


in it. The /micheal request would be intercepted - with mod_rewrite..

Rewriterule ^(.*)$ /handler.php?user=$1

and redirected to the php handler, which redirects the browser on to the full, crazy URL.

Download the source for my 404 page (hit any bogus link at corz.org to get to it, or go directly to the /engine) which does some cute redirecting, and will provide you with a fairly clear example of how to redirect your own mapped links.

404 keeps its own mappings (simply old link > new link type affair) inside itself, because there aren't that many, but if you are dealing with dozens or even hundreds of links, you'll definitely want a separate database (just a plain text file) for these. That, or put the ugly ID in the URL!

/me googles affiliate program.

Yup, just as I thought, totally the Wrong Thing for corz.org, but, well, each to their own, eh!

;o)


sam - 05.12.05 8:32 pm

HI,

Is there a way in Apache to parse the text in the files, and find all urls, or perhaps once a url is clicked, be able to access it.

We need to find and replace all the external urls on our server.

Thank you.




daveberk - 08.12.05 9:29 am

I have several subdomains but sometimes people add a "www." to the start like:

http://www.subdomain.example.com

or

www.subdomain.example.com

I am trying to write a htaccess redirect to eliminate the "www"

I am trying this:
RewriteCond %{REQUEST_FILENAME} ^www\.(.+)\.whyagel\.com$
RewriteRule ^(.*) http://$1.whyagel.com [NC,L]
I also tried:
RewriteRule ^www\.(.+)\.whyagel\.com$ http://$1.whyagel.com [NC,L]
but it doesn't seem to be working.

Any ideas?

Thanks!


corz - 08.12.05 9:31 am

daveberk, I moved you comment from the other page.

In answer, I use this..
rewriteengine on
rewritecond %{http_host} !^$
rewritecond %{http_host} !^corz.org
rewritecond %{http_host} ^www\.corz\.org [nc]
rewriterule ^(.*)$ https://corz.org/$1 [r=301]
and it works great.

sam, you'd be better off with a grep in the shell. If you want help with that, mail me.

;o)


Steve - 08.12.05 5:38 pm

how do i support multiple domains and multiple sites on 1 shared hosting account using htaccess? can this be done?


corz - 09.12.05 9:07 am

It can be done, Steve, but not easily. You'd definitley be better off with a host that supports this sort of functionality out of the box.

If you still want to press ahead, do a wee search at webmasterworld forums, I remember reading a thread there where they explored different methods of achieving this, with varying success.

But really, you need to get a better host!

;o)


gratzo - 09.12.05 7:52 pm

I'm running an Apache server (WAMP) on a windows box, and have some virtual hosts set up on it - trying to figure out why you allways have to type in the "www" before the URL. If you don't, you hit the "root" folder of the server.

The DNS is handled via dns.afraid.org, and both the "www" and "non-www" domains are pointed to the server.

I'm thinking that there is a command that can either rewrite or redirect (or both) and have it hit the same "virtual" site with or without the "www". I've searched the Apache docs, and all over your site, and google, and can't seem to find this specific topic. smiley for :blank:

Here's what I've tried and it's not working:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.myexample.com/$1 [R=301,L]


Wondering if I have it backwards - this is all new to me!


roger - 12.12.05 3:41 am

In reply to Steve's question, I found this code snippet somewhere:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(ww+\.)?parked-domain\.com
RewriteCond %{REQUEST_URI} !/parked-domain/
RewriteRule ^(.*)$ /redirect-folder/$1 [L]
RewriteCond %{HTTP_HOST} ^(ww+\.)?second-parked-domain\.com
RewriteCond %{REQUEST_URI} !/second-parked-domain/
RewriteRule ^(.*)$ /redirect-folder-2/$1 [L] 


But I am trying to do the same thing, but I cannot figure what must be inserted where in the code above. Can anyone out there help me?

I have a primary domain with hosting. But now I have another domain name which I want to host in a sub-folder within the primary domain. But in the code above I am not sure where I have to insert the parked domain name, the sub-folder name and the primary domain name.


corz - 12.12.05 11:48 am

It's almost spelled out..
RewriteCond %{HTTP_HOST} ^(www\.)?parked-domain-NAME\.com
RewriteCond %{REQUEST_URI} !/parked-domain-FOLDER/
RewriteRule ^(.*)$ /redirect-folder-FOLDER/$1 [L]
The first line catches the requests (with or without the "www" part), the second line checks that the request (which may itself be a rewrite) isn't for something in the redirected folder, and lastly, all requests to the parked domain are redirected to the folder.

The confusion lies in the code presuming that the folder name and parked domain name will be identical. It's functionally the same as
RewriteCond %{HTTP_HOST} ^(www\.)?thefoodomain\.org
RewriteCond %{REQUEST_URI} !/foofolder/
RewriteRule ^(.*)$ /foofolder/$1 [L]
But that's not all you need to do. Unless the domain records actually point to that box (at your registrar), and the web host is setup to host this parked domain (at your hosting admin page, or else manually, by the system admins), none of this redirection will work as you expect. Your webserver is the last link in the chain.

I definitely advise seeking a host which can handle this stuff, not just because it's a lot easier to setup, but also because if you rely on this parked host, and anything goes wrong, they will be much more likely to be willing and able to help out.

I'm always amazed by the number of folk who are out there, messing with mod_rewrite and the dns records without having first setup a webserver at home, a place to test-test-test all this stuff out.

If you take webmastering seriously, get a development mirror, and a few dyndns.org domains to play with.

;o)


roger - 13.12.05 1:24 am

Hey, Thanks a lot for your quick reply and advice.

Yes, it seems like now I have to turn to my web host for help. Because although the redirection itself now works fine(i.e. typing my parked-domain gets my hosted-domain page), somehow, the htaccess rules don't seem to kick in and so the redirect to the folder does not happen. wierd... smiley for :eek:


corz - 13.12.05 2:32 am

Possibly your host doesn't even support .htaccess, some don't. A good way to tell is to drop a gibberish .htaccess file in your root folder, if your site still works, you probably don't have .htaccess capabilities.

If .htaccess works normally, putting gibberish in a live .htaccess file will bring up a nice 500 error page. But in your case, it simply sounds like the dns records need to be altered at your host, if they will do that for you.

Remember, grovelling is always a bad idea, except when dealing with sysadmins.

;o)


unDead - 09.01.06 12:42 pm
Help!
Need to rewrite incoming requests for .html to generic URI. In the progress of upgrading my website using php templates, but plan to keep index.html files. Thought I hit the jackpot when I came across your mod_rewrite rule for all requests to whatever .htm will be sent to whatever.php. It worked great until I realized that my home page with URL .html had disappeared!smiley for :eek: So that particular code doesn't work for me.

I am now employing generic URIs as far as possible in my website, and right now the server returns the new php file in response to a request for the generic URI, but there is error (page not found) when a request comes for the specific .html URL. This wouldn't be any big deal to fix if there were only a few files affected, but there are hundreds of files on this website with who-knows-how-many bookmarks set to the previous .html URL.

I want a fix that will return the file of that name no matter the content type specified, at least with respect to requests for html, htm, php content types; let the image files be specific URLs for the moment. What I want is this: suppose someone requests name.html, the server will look for name and if there is only name.php, then it will return that file; if there is no name.php but name.html, then it will return name.html. This is because I plan to use index.html in my directories.

I did come across another mod_rewrite rule that looked like it might work, but when I tried to implement it (typed it into my .htaccess file in the root), nothing happened.smiley for :blank: Here's the rule I tried (I substituted .php for the .phtml):

#   backward compatibility ruleset for
#   rewriting document.html to document.phtml
#   when and only when document.phtml exists
#   but no longer document.html
RewriteEngine on
RewriteBase   /~quux/
#   parse out basename, but remember the fact
RewriteRule   ^(.*)\.html$              $1      [C,E=WasHTML:yes]
#   rewrite to document.phtml if exists
RewriteCond   %{REQUEST_FILENAME}.phtml -f
RewriteRule   ^(.*)$ $1.phtml                   [S=1]
#   else reverse the previous basename cutout
RewriteCond   %{ENV:WasHTML}            ^yes$
RewriteRule   ^(.*)$ $1.html


Can you tell me whether this rule will work? Perhaps I did not type it correctly. I removed the comments and typed only:

RewriteEngine on RewriteBase /~quux/ 
RewriteRule ^(.*)\.html if exists 
RewriteCond %{REQUEST_FILENAME}.php -f 
Rewrite Rule ^(.*)$ $1.php 
RewriteCond  %{ENV:WasHTML} ^yes$ 
RewriteRule ^(.*)$ $1.html
(reformatted - ed)

Of course I did also use enable: Options +Multiviews +FollowSymlinks.

Maybe I typed it wrong? (I'm new at this stuff.)

Anyway, I hope you can help me solve this problem. Seems to me it must be a common problem with a commonly known solution, but I just haven't been able to find it out. Thanks in advance.


corz - 10.01.06 12:20 pm

I think you need to get back to basics here, unDead, and delete that mess of code! You struck gold with my original rule, you just need to *tweak* it slightly. The original rule..
Options +FollowSymlinks
 RewriteEngine on
 RewriteRule ^(.*)\.htm $1.php [nc]
is illustration. If you scroll down, you'll see a real example..
Options +FollowSymlinks
 rewriteengine on
 rewriterule ^(.*)\.htm$ http://corz.org/$1.php [r=301,nc]
can you spot the subtle difference?

In the second example, there's an "$" after the \.htm, which means, rewrite this line ONLY if the filename ends in .htm, whereas in the first example, so long as the extension begins .htm, the rule will work. myfile.htmyipee would trigger the rule, for instance, as of course, would .html files. Add the $ and it will work exactly as it does at corz.org, allowing html files to display just fine.

As for the order in which they are served, there's no need to get complicated, Apache has a built-in mechanism for this, which I touched on in part one. I'm assuming by "Generic URI" you mean links without filenames, like http://mysite.com/blog/, as opposed to http://mysite.com/blog/index.php. What you need is the DirectoryIndex directive, and it looks like this..
DirectoryIndex index.php index.html index.htm
which tells apache to serve up first an index.php if it exists, and if it doesn't, serve up index.html, and if that doesn't exists, index.htm, and so on. you can add more types and re-arrange the order to suit your needs. With these two simple directives you should be able to achieve what you desire.

Easy when you know how. smiley for :geek:

;o)


unDead - 10.01.06 4:24 pm

Thanks, Cor.

I really don't know what I'm doing.smiley for :erm:

Actually I got the "mess of code" to work so that requests for .html files return .php files except when the .html file exists. But this only solves half the problem, because I need to generate a 301 page to alert the search engines and also to allow people to update their bookmarks. As it is with the mess of code, even if the server returns the .php file, the .html URL shows up in the browser address, and also when the page is bookmarked.

My goal: when visitor requests http://www.mywebsite.com/directory/filename.html, the server returns the filename.php but the user-agent displays the generic URI http://www.mywebsite.com/directory/filename, and also alerts my visitor to update his bookmarks. Exception when there is an existing filename.html, then server returns that, but still want to display the generic URI and give alert to update bookmark.


Anyway, I wanted to give your code a shot. But I modified it somewhat, thinking to try to rewrite the .html to non-specified type. So I deleted the mess of code and typed in this:

rewriteengine on
 rewriterule ^(.*)\.html$ $1 [r=301,nc]


I know, I know, it was a dumb thing to do.smiley for :roll: But I had supposed that if I left in the http://mywebsite.com/ then I would run into problems with my files in so many different directories. Anyway, this is what happened. I keyed this address into my browser:
www.mywebsite.com/directory/filename.html, and I ended up here:
www.mywebsite.com/home/hansad/public_html/testweb/filename

At least there was no .html or .php appended to the file name. But I found myself in the core of the earth when I wanted to be somewhere above sea level.

So then I did something stupid again. I added RewriteBase / to the mix. That was even worse. Couldn't access my web pages at all, got a message something like "redirect unable to execute" and the mention of "cookies" in it (sorry, I didn't copy the error message; just got the hell back into the .htaccess file).

So then I tried this:

RewriteRule ^(.*)\.html$ http://www.mywebsite.com/$1 [r=301,nc]


That works just fine for my test filename.html in test directory. I direct the browser to http://www.mywebsite.com/directory/filename.html, and the browser displays http://www.mywebsite.com/directory/filename.

BUT

when I tried to go to http://www.mywebsite.com/, the browser address showed http://www.mywebsite.com/index but there was nothing. Nothing at all. Where did my index.html page go?smiley for :eek:

I do after all want to still keep some .html pages, in particular my index.html files at home and directories and subdirectories.

So why didn't I keep to your rule and just use .htm$ instead of .html$? Because I don't have (and never have had) any links using .htm. What would be the point? I don't follow why this should apply to what I want to accomplish. And why did I drop the .php from your rule? Because I want to upgrade to "cool URIs" and avoid specific content type URLs.

I am bewildered why your modified rule worked nicely for my test directory, but then my home page disappeared. smiley for :blank: I sent the browser to http://www.mywebsite.com, and when that didn't work, to http://www.mywebsite.com/, but I ended up in a blank page, with the URL displayed in the browser address bar: www.mywebsite.com/index
Why the page doesn't display even so, I don't know. Then too, I didn't see any 301 notice, no alert of any kind.

So for the time being, I have pasted back the mess of code, hoping to find a better solution.

I'm sure I've missed out something obvious... like Mr. McGoo. Can you point me in the right direction?


corz - 11.01.06 11:33 am

Yikes!

Well, firstly, I think this whole "generic link" thing is a Very Bad Idea. (note Winnie-The-Pooh capitalisation). File extensions are there for many good reasons, and if you rely on content encoding and mime-types to get the page correctly displayed in the user's browser, there are times when this will simply fail. And portable, it is not.

Also, you must have one or the other. Either you send a proper redirect, and have the proper URL appear in the browser, or you don't.

You can't send a 301 and have your "cool" links in the browser address bar. It's either, or. And I don't think http://mysite.com/index is cool at all, it just looks like something's broken, which it clearly is!

By the way, the "point" of redirecting .htm files is so you can include them inside php files. php is code. html is content. A modern web page is a mix of both of these things (and style - css - as well), so while the content (the text of the page) exists in the .htm file, you wouldn't want the user to load that directly, or else they'd miss the headers and whatever else your containing php code usually sends along with it.

There's one thing I always strive for in any work, and that is simplicity. I'd definitely think again about all this. However, if you do decide to continue on down this path, why not try a nasty hack like this..
RewriteEngine On
# only rewrite is file doesn't exist..
RewriteCond %{REQUEST_FILENAME} !-f [nc]
# if it hasn't got a .php extension, add it..
RewriteCond	%{REQUEST_FILENAME} !(.+)\.php$
RewriteRule ^(.+)\.?(.+)?$		$1.php [nc]
# finally, if there is no actual php file there, present its .html equivilent..
RewriteCond %{REQUEST_FILENAME} !-f [nc]
RewriteRule ^(.+)\.(.+)$	$1.html [l]
That's roughly what you're after isn't it?

If someone requests http://mysite/test.html , or http://mysite/test.php and it exists, that's what the user will get in their browser. Now to your "cool" links..

If someone requests http://mysite/test, it is firstly rewritten to "test.php", and if that exists, test.php is presented to the browser. Their address bar still just shows http://mysite/test

If it doesn't exist, the request is rewritten to "test.html", and that is presented to the browser. Again, their address bar remains http://mysite/test. Cool?

This is just off the top of my head, and would need tested and tweaked. I guess you could go further and take extra steps to remove the extensions altogether, but that seems OTT. Your cool links will work, basically checking for file.php, and then file.html in that order and presenting whichever it finds, without altering the address bar in any way. (no [r])

As soon as you send a 301, of course, the address bar will change to the redirected location, I don't think it's possible to send a 301 to your cool link and then work internally from there, as it's something that's physically sent to the browser, ie. last.

At least this will get you started, though really, if I were you (which, as ever means, "if you were me") I wouldn't even go there.

I used Macs for years, and the lack of decent file extension support was a cross-platform nightmare. My opinion: file extensions are cool.

;o)

ps.. note, every "-f" file existence check causes you a performance hit.

pps.. also note, if you start messing with 301 redirects, broken ones can take a long time to change in your browser (so you might go on to fix it, but not realize, because the browser is still pointing itself at the first (broken) 301. Solution: restart your browser, or use another one.

ppps.. (the legal limit for pee-esses) if you have access to the httpd.conf (main apache config) add this to the bottom of it..
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 5
which will spurt out loads of information for you. 9 is the most info 1, the least. 5 is useful, I find. IMPORTANT: remember to comment out these lines when you finish testing or you will seriously impact your server's performance.


unDead - 13.01.06 12:31 pm

Thanks a lot, Cor. Wish me luck.smiley for :roll:


Mike - 14.01.06 11:40 pm

Quick question... I would like to redirect any requests to mysite.com/test/* to test.mysite.com/* .
I tried using this in the /test/.htaccess file:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mysite\.com [nc]
rewriterule ^(.*)$ http://test.mysite.com/$1 [r=301,nc]


This works fine for most urls, e.g. http://mysite.com/test/, http://mysite.com/test/index.php, http://mysite.com/test/dir/whatever.php, which all redirect to the correct http://test.mysite.com/ etc url.
However if I request http://mysite.com/test without a trailing slash, i am instead redirected to http://test.mysite.com/C:/wamp/www/test

Would appreciate any help.
As you can see I am running WAMP.




corz - 15.01.06 12:01 pm

Ahh.. trailing slash problem. Please be aware, I'm still under the influence of last night's poisonous substances, but something like this in your root .htaccess would probably do the trick.
RewriteEngine On
RewriteCond %{http_host} test.mysite.com [nc]
RewriteRule ^test$ /test/ [r=301,nc]
RewriteRule ^test/(.+)$ http://test.mysite.com/$1 [r]
worth a try, anyway! smiley for :ken:

;o)


corz - 15.01.06 12:05 pm

I can already see an easier way to write it. smiley for :roll:

;o)


Mike - 16.01.06 3:00 pm

Thanks for the help, I managed to get it to work using this in the root .htaccess:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^mysite.com$ [nc]
RewriteRule ^test(.*)$ http://test.mysite.com$1 [r=301,nc]



corz - 16.01.06 3:44 pm

Yup, Mike, once you get up to the root, there are a few ways to achieve the effect.

Good work!

;o)


Joe - 17.01.06 2:47 am

ok, so what do the [L] [R] [NC] and all those statements mean at the end of the line? is there somewhere with a list of them all? i cant find one :(


corz - 17.01.06 10:14 am

See the Useful Links section (above), first link.

;o)


paul - 19.01.06 12:47 am

I have the following situation that I couldn't find an example for in this great page.

We have many subdomains like areuba.travel.com, jamaica.travel.com, mexico.travel.com, etc... If a subdomain is hit, I'd like to acheve to pull out a file with the subdomain name as a parameter for a PHP file sitting in the root of the web.

Something like this:

user types in: http://aruba.travel.com/ or http://aruba.travel.com/index.html
(other filenames except index.html in the subdomains should show 404)
page served: http://www.travel.com/sub.php?name=aruba
the browser URL still shows: http://aruba.travel.com/

Is something like that possible to do?

Thanks a lot for your help,
Paul


corz - 19.01.06 9:06 am

Well, Paul, almost anything is possible, but before I dive in with possible real solutions, I'll need to know how things are setup at your server. Good question, by the way, well framed.

Okay, these "subdomains", are they real? I mean, do they exist properly on the DNS, and there's a folder in the root of your main site that is accessed when someone uses one of them?

If they are real subdomains (like those created with CPanel or whatever) then firstly, you'll need to put the rewriting rules IN that subdomain. The main .htaccess file isn't read unless someone accesses the main site.

And if you redirect from a subdomain to the main domain, it's going to be very difficult to keep the address bar from changing, becasue you will be performing an "external" redirect, out of one domain and into another.

If these are subdomains that are created on-the-fly with mod_rewrite, it might make things easier to implement, but also easier to mess up those redirections; if this is the case, show us the code that creates the subdomains. If it's not, consider it; you could take care of the whole thing with mod_rewrite.

If you have access to the DNS and web server setup, perhaps set up a wildcard subdomain system; basically a wildcard A class record for your domain, and tell Apache to send all requests to the main root. Then you could add any old names you need later on by simply using them.

Regardless, the principle goes something like this..
rewritecond %{HTTP_HOST} !^www\. [NC]
rewritecond %{HTTP_HOST} ^([^.]+)\.domain\.org [NC]
rewriterule ^(.*)$ http://www.domain.org/page.php?name=%1 [R,NC,L]
An example only, from a subdomain's .htaccess file, and an "external" rewite, so the user gets the real location in their browser. Just food for thought.

And if you are stuck with fixed subdomains, you might want to consider some php solution. If your index.php inside the subdirectory were some intelligent handler (maybe start by copying sub.php to /sub/index.php) - because it can access all your files the same way the php scripts in your main site can - it could possibly handle the whole thing from the subdomain, which would ensure the user stays inside your chosen subdomain, too.

It all depends on how your site is designed, and how your web server is setup.
More details please!

;o)


Joe - 20.01.06 11:50 am

thanks, i found that link the second i posted this question :(

i have a new question... i used the funky rewrite to change www.yoursite.com to yoursite.com, however i am having some problems in PHP.

one of the PHP appli's just stops working when i use the www eliminator...
for some reason it doesnt like the redirecting process, and just stops working. To get it to work again, i have to remove the rewritecond from my .htaccess.

Are there many situations where this may occur?
im wondering if its fixable, and if its worth the risk of causing problems for my users just for the sake of a 'prettier url'...

help smiley for :D

UPDATE: i fixed the problem in question, seems i had to remove the www from the admin area's too, so it stopped re-directing... but anyway, i am still wondering about any other Risks involved?


corz - 20.01.06 5:26 pm

Yup, Joe, that was the obvious answer; remove 'www.' from php scripts.

It is usually best to use $_SERVER['HTTP_HOST'] in your php scripts, saves errors, increases portability, etc.

risks: none.

I've *never* used the www part at corz.org, but then, I write all my own back-end code, so if you are using some off-the-shelf package, it's certainly worth testing it thoroughly (preferably at your development mirror) before deploying.

But that's true in any case.

;o)


Merlin - 23.01.06 5:33 pm

Hi,

I've fairly new to the joys of mod_rewrite and have a rewriterule in a htaccess
file that fails for some reason thats not clear to me.

This rewrites a PHP file into a different PHP file.

Options +FollowSymlinks
RewriteEngine on

rewriterule ^(.+)\.php http://localhost/httest/page.php?p=$1 [r=301,nc,qsa,ne,l]


I would expect the translation to be done once and finished with, but it seems the rule is repeatedly reapplied, so the server gets into a loop. I bypassed the problem by doing this.

Options +FollowSymlinks
RewriteEngine on

RewriteCond $1 !^page$ [nc]
rewriterule ^(.+)\.php http://localhost/httest/page.php?p=$1 [r=301,nc,qsa,ne,l]


Taking out the r=301 generates a different symptom "The server encountered an internal error or misconfiguration and was unable to complete your request."

What nugget of info am I missing about this case? Why is the rewriterule being repeatedly reapplied ?

Thanks in advance

BTW: Great site.... :)


Paul - 23.01.06 10:46 pm

I have a parked URL, say
parkedUrl.com
that I want to get redirected to
http://www.mainDomain.com/index.html?page=shop/subpage&source=letter&link=test 
so that when a user types
parkedUrl.com
they get taken in the php code to the landing page at www.mainexample.com where I collect the tracking source and link information. Can you help? I image it is something like the following that I use to redirect my other parked domains, but not exactly.

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^parkedUrl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.parkedUrl.com$
RewriteRule ^(.*)$ http://www.mainDomain.com/index.html?page=shop/subpage&source=letter&link=test [R=301,L]


As of last week I had never even heard of htaccess - this is all very new to me.


corz - 24.01.06 1:31 pm

Merlin, your first rule redirects *every* php file. for instance..

http://localhost/some/deep/page.php would redirect to http://localhost/httest/page.php?p=some/deep/page

Understand, at the end of the first run, when the browser is phyisically redirected to "page.php", the rule has to run again; it's a whole new request. A browser like Firefox will tell you "Redirection limit for this URL exceeded. Unable to load the requested page".

So, adding RewriteCond $1 !^page$ before the rule prevents Apache getting in a loop. Removing the "r=301" shouldn't cause an error. The rule looks fine, and it should work exactly as expected, the only difference being that the user sees the old URL in their browser address bar instead of the new redirected URL. You removed the comma, too, right?

If it's still not working, enable rewrite logging (see the new troubleshooting section up in the main page for more details) and try again! Feel free to drop more information and questions here.


Paul, again, see the troubleshooting tips (above), the section about "Fatal Redirection". There's nothing inherently wrong with your rule, though you might prefer the shorter..
RewriteCond %{HTTP_HOST} ^(www\.)?parkedUrl.com$ [nc]
RewriteRule ^(.*)$ http://www.mainDomain.com/index.html?page=shop/subpage&source=letter&link=test [R=301,L,NC]
But it's all the same, really.

What puzzles me is where the extra data comes from; the "tracking source and link information", there's nothing in the original request, so I assume this is some arbitrary data you are adding yourself.

Thing is, unless the data is based on something like their IP, or other unique characteristic, won't all requests have the exact same data? And if so, what's the point in tracking it?

Just thinking out loud! You'll have something up your sleeve, no doubt. smiley for :ken:

for now..

;o)


Paul - 24.01.06 4:33 pm

Thanks. The problem was indeed a fatal redirection. The reason I want the tracking information in the redirect is that the domain name will only be used for a single snail mail sales letter and I want to track who uses the domain and thus who responded because of the letter.

Paul


Dan - 27.01.06 1:43 pm

I have a problem with a "ErrorDocument 404". Can I get the requested not-found url to the 404-page, so I can save the name of the not-found file?

http://www.myexample.com/thisfiledontexists.php

Will redirect to:
http://www.myexample.com/404.php?dont_exists=thisfiledontexists.php

Dan


corz - 29.01.06 6:05 am

Sure, Dan, hit any 404 here at the org for a demo of some possibilities.

Essentially you just need to set your error handler to your php page..
ErrorDocument 404 /err/404.php
And that's it. You don't need any special redirection because the ?dont_exists=thisfiledontexists.php is superfluous, that information will be inside $_SERVER['REQUEST_URI'], so you can use it inside you 404 page without any extra effort. When you hit a 404, the request hasn't changed, it's still /the/missing-page.html, or whatever. This is by design.

Download the source for 404 (available from any 404 page), it has instructions, examples, and what-not.

;o)

ps.. I see, Paul. So basically it's just a simple count.

If you used email instead of snail mail, you could tag on some Unique ID to the url, http://myurl.com?clientID=e4896btc9r8769asfiejh, or whatever, and have some more useful tracking going on.

And if you did that, (I'm just thinking out loud for someone who may read this later) you might be best to handle the tracking information in the original domain. ie. instead of a mod_rewrite redirection you have a php file that grabs the tracking data, stores (and possibly manipulates) it, and then sends a proper http header..
header('Location:  http://whatever.com/index.php?MyOptionalData=foo');
to redirect the user to the real url. With php, there's always more than one way to skin a cat.


yoshi - 29.01.06 11:51 am

I recently changed url using ForceType Directive and old google url index to be redirect to new url.

i get internal server error with lines below, can anyone tell me how to go about.

old url
/detail.php?listing_ id=1

redirect to new url
/property/guide/1/

-----------------------------------------
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^detail\.php?listing_ id=([0-9]+)$ property/guide/$1/ [R=301]

-----------------------------------------

thanks



Mindless - 29.01.06 12:01 pm

Hi, i've been looking for a nice redirect script like i've seen on other community sites and your site helped me do this, thanks! but there's one question i'd like to ask.

heres the code i'm using

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^~(.*) /profile.php?username=$1


currently tht code makes the url (http://mysite.com/~SomeUser) and it keeps that url *it does show the proper page, but I want to know if i can make it SHOW the url of that file also

so that
(http://mysite.com/~SomeUser)
goes to
(http://mysite.com/profile.php?username=SomeUser)
AND actually displays the go to url

If you can help me please email me, i have submitted my email

thanks in advance
-Mindless


Dan - 29.01.06 6:58 pm

Very nice article, and very well-written! The part I was looking for is the inheritance, and lo and behold, it's right here!


corz - 29.01.06 9:43 pm

Yeah Dan (another Dan!), inheritance catches out a lot of people, and this solution is far less confusing that mucking around with RewriteBase and what-not, I think. Cheers!

Mindless, simply add [R].

yoshi, you will probably find something like this..
Options +FollowSymlinks
RewriteEngine on
rewritecond %{QUERY_STRING} listing_id\=([0-9]+)$ [nc]
RewriteRule ^detail\.php(.*)  http://itest/property/guide/%1/? [nc,r]
more robust and functional; not to mention, easy to understand and hack. Note the "?" at the end of the substitution string, to prevent the query string being tagged back on.

You could go on to add checks for directory's existence and more, but I'll leave that as a fun exercise for you!

;o)


Jay - 30.01.06 3:30 pm

First off, thank you for the wealth of information you've shared with us. I've been searching the net for a solution to my problem and yours came the closest to touching upon it- however, I'm still having difficulties with getting it to work and was hoping you can help me out.

Basically, I have a PHP/MySQL dynamic site that offers a ton of downloads. Now, these downloads are linked as such: "http://myexample.com/file_download/23" where it actually stores download count and redirect the file to be downloaded at the actual location, which would be: "http://myexample.com/files/filename.zip"

Now, I've placed an htaccess file inside my files directory to disable hotlinking for zips- this works perfectly when someone is direct linking to my files via the actual location's URL. It, however, doesn't work when someone direct links using my redirecting URL "http://myexample.com/file_download/23". The problem lies in that since it's really a redirection, there's no actual "file_download" directory for me to drop any htaccess preventing hotlinking inside it. So I tried to do something like this:


RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?myexample.com(/)?.*$ [NC]
RewriteRule ^file_download/?.*$ http://myexample.com/nohotlink.gif [R,NC]


That didn't work. I also tried to do redirecting to my homepage rather than replace it with my nohotlink image...but that yielded the same results. What happens is that they get directed to "http://myexample.com/file_download/23" and the page states an error. For I.E., it states an error but if you just hit the refresh button while on the error page, the file downloads anyway. On Firefox, I noticed that it states an error but gives a "FOUND" notice and produces a link to "http://myexample.com/file_download/23" in which when you click on it, the file downloads as well.

I hope I'm not confusing you! All in all, I just really need to be able to successfully disable hotlinking to my files even when they're accessing it through the redirection URL. Any ideas? Any help you can give me would be great. I've been tearing my hair out trying to figure this thing for days now!


corz - 30.01.06 10:01 pm

Jay, my first instinct says "edit the redirection". You say this is a database driven site, so I presume that it's php, and not mod_rewrite, that's doing the redirection. Referrer information is available there, so why not use it there?

If it *is* mod_rewrite doing the redirection, then add rewritecond statements to *that*.

If the first isn't possible (and it sounds like a hack that would be welcomed by the creator of the CMS, or perhaps you might like to suggest it) or the second isn't the case, you could still work on the links afterwards with mod_rewrite. I would have thought something like this would work..
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com [nc]
RewriteRule ^file_download http://mydomain.com/nohotlink.gif [r,nc,l]
which looks remarkably similar to what you tried yourself. Why didn't it work? What happened? What errors did you get? etc.

At the end of the day, it's near-impossible to prevent a determined downloader from grabbing your files unless you control access in some way outside mod_rewrite. My own distro machine uses "passthru" to pass the data form the (totally user-inaccessible) folder and feed them to the user's browser. It's either that or else use some sort of authentication mechanism to control access to the files.

With more information about how the downloads are processed, perhaps I could come up with a better solution. If you still need help, get back to me with the errors you are getting from the rewrite rules, above. Perhaps even the name of the CMS, if it's something available.

have fun!

;o)


Dan - 31.01.06 10:38 am

corz

$_SERVER['REQUEST_URI']
show the error-page filename and NOT the not-found filename.


corz - 31.01.06 3:07 pm

Well, Dan, you (or rather, a mod_rewrite diective set by you, or someone) must have previously done some kind of redirection on the request. That's probably where your trouble lies.

Create a 404 page (whatever you set as the 404 page in your .htaccess) and put only this in it..

<?php
   $requri
= getenv ("REQUEST_URI");
   
$servname = getenv ("SERVER_NAME");
   echo
'<br /><pre>',"I can't find http://$servname$requri</pre>";
?>

If it doesn't show the name of the REQUESTED page, then you'll need to check for rewrite rules that might be catching the requests, or similar.

Keep us posted.

;o)


Greg - 01.02.06 5:46 pm

Great Site. Very helpful.

I would like to redirect:
http://www.mysite.com/gregkersh/
or
http://www.mysite.com/gregkersh

into
http://www.mysite.com/referral.php?K=gregkersh

using:
RewriteRule (.*)/ referral.php?K=$1

It works great when I have the slash at the end, but how do I tell it to look for the end of line instead of the slash?
When I use:
RewriteRule (.*)$ referral.php?K=$1
It passes "referral.php" into K.

Any help would be appreciated.
Thanks,
Greg


corz - 01.02.06 6:00 pm

Greg, you are close, something like..
RewriteEngine on
RewriteCond %{REQUEST_URI} !referral\.php
RewriteRule ([^\/]+)/? referral.php?K=$1
would do the trick.

The RewriteCond ensures you don't get in a loop. Note: we specify not "/" in the matched expression (.+) so that regardless of whether someone uses "gregkersh/" or "gregkersh", K will always equal "gregkersh", in other words, without the slash.

If they used "heellyeas/foo/my/bar/baby!", K would equal "heellyeas".

;o)


Greg - 02.02.06 4:08 am

Hot Dog! That worked perfectly.

Thanks a bunch!


help - 03.02.06 3:18 pm

hi i have 5 applications in different folders in my htdocs.I need to redirect all but one of them to a different location.

ex. http://www.localhost.com/TEST should remain the same
where as any other http://www.localhost.com should redirect to a diff site say http://www.moved.com
Please help!!!!


asdf - 03.02.06 11:50 pm

working on mod_rewrite means using ones location bar! quit the crap!

you should be aware that your javascript on this site not only functionally breaks the location bar in firefox 1.5 but also all the buttons up top, not exactly prime time for working on mod_rewrite


corz - 04.02.06 1:16 am

asdf, I don't use Firefox 1.5 normally, because there are no updates for extensions I need, but I'm posting with it right now. Also, I came via Google, so that every ounce of JavaScript could be activated (including the funky highlighting).

Result: everything is 100%, as expected. The location bar is also unaffected.
I knew this, but I had to check.

asdf, maye you need to check your browser settings (or possibly your firewall/AV). Feel free to chuck specific errors, screen captures or code blocks my way, though probably email is best, yeah? This has nothing to do with mod_rewrite.

help, you're just not trying!

I'm not going to create a rule, you can scroll up easily enough for that, but I'll give you the essential clue: you need a rewritecond line that checks for NOT http://www.whateveritis.com/TEST and then rewrites only for that match.

Like I say, scroll up through the comments; it's all there.

;o)


onk68 - 08.02.06 3:41 am

hello all
ok this is the problem that i am having i have a site but i want ot move the entire site down one directory i tried this


RewriteCond %{HTTP_HOST} ^(www\.)?myexample.com$ [nc]
RewriteRule ^(.*)$ http://myexample.com/Dream/index.html [R=301,L,NC]

but i always get an error that my server has tried to redirect that will never complete i know that it is because i am redirecting to the same url with just a sub folder but i dont know how to make it not do the redirect after the redirect

any help would be greatley apresheated dont laghf i cant spell very good if it ant code it dont make cence


commssioned_rewrite - 08.02.06 12:40 pm

This is a Question:

I have a problem with google sitemaps feature and I am wondering if this is because of the use of apache mod rewrite on my site.

I am not the programmer of the admin - I am the business ownner. I am doing the sEO and basically google wants a HEAD command to http://www.mysite.com/somefilethatdoesnotexist.html to return a 404 instead of a 200. My site returns a 200. Is it related to this?

Sorry if this turns out to be a bozzo type question . . obviously this is not my area of expertise. :)


corz - 08.02.06 10:32 pm

commssioned_rewrite, until now, I knew absolutely nothing about google sitemaps, even what they were, but after some light reading, it looks something like the idea I suggested back in Oct 2004. (combined with my funky robot links page) Nice one Google! smiley for :D

In answer to your question, of course a missing page should produce a 404 error1! That's the whole point! Your problem isn't google sitemaps, as such, but with whatever it is you are doing with 404 errors. What are you doing with them?

When you say "google wants a HEAD command", what do you mean? Google emailed you and asked you for a HEAD command by return? Or you are getting an error somewhere? Where? Exactly. Is it the software that creates your sitemap that's failing?

If you want me to look deeper, you'll need to be more specific, or else just go fix the 404 handler!

onk68, you need to add a condition line, probably one that checks the request does NOT contain the term "Dream/", something like..
rewritecond %{REQUEST_URI} !dream/ [nc]

;o)

references:
If you use Firefox, with the Web Developer extension, go into the information menu and choose "View Response Headers" (the bottomost option) which will tell you what page gives what headers.

And anyone who develops web sites in any way and doesn't use Firefox with the Web Developer extension should probably go and sort themselves out right now!



onk68 - 08.02.06 11:44 pm

thanks man works perfectuly can i use this methad to do more than one site on a singel server i hope so ill play with it and see thanks again


corz - 09.02.06 12:51 am

Yes!

If your host uses CPanel, (at the time of writing) you have to use the "add-on" domain, then simply redirect your newly aquired domain to otherdomain.maindomain.org - using the following rules..
rewriteengine on
rewritecond %{http_host} !^$ 
rewritecond %{http_host} ^(www.|otherdomain.)?maindomain.org [nc]
rewriterule ^(.*)$ http://otherdomain.org/$1 [R=301,NC]
to keep everything ticking from the main TLD. It really lives in a subfolder called "otherdomain", but see what happens if you try..

http://maindomain.org/otherdomain/

in your browser. Basically, once your DNS is pointing in the right direction, and Apache is ready to play (i.e. there's a valid host entry in its config somewhere) you can do all sorts of funky stuff inside your own space. Imagination is the only limit.

Have fun!

;o)


kai - 15.02.06 2:15 pm

hi, great write up... i've been trying to figure out for a few days now how to do the following redirect using htaccess, hope you can help!

I want to rewrite :

http://www.mysite.com/index.php?option=com_smf&Itemid=71&topic=385.0 to
http://www.mysite.com/forums/index.php?topic=385.0

I know this is probably super simple, but at the risk of embarassing myself, here's what I've tried:
RewriteRule ^/index.php?option=com_smf&Itemid=71&$ /forums/index.php$1 [r,nc]


smiley for :eek:


corz - 15.02.06 5:09 pm

You tried, kai, that's the main thing! So many don't…
And anyway, it's not super simple; unless you know how..   smiley for :ken:

Here's what you need..
RewriteEngine on
RewriteCond %{QUERY_STRING} ^option=com_smf [nc] 
RewriteCond %{QUERY_STRING} Itemid=71 [nc]
RewriteCond %{QUERY_STRING} topic=(.+) [nc]
RewriteRule ^index.php /forums/index.php?topic=%1 [r,l,nc]
It's fairly self-explanatory, though a couple of parts are worth a mention.

The ^ in-front of option=com_smf instruct mod_rewrite to only process the rule if option=com_smf is the first variable. If your variable order is likely to change, remove that. It doesn't matter too much if you remove it now; but I wanted to add that because we don't have many examples of this kicking around here.

I'm thinking I might do a part three that is only examples from these comment Q&A's.

So the three rewritecond lines check that each of the variables exists and the first two are com_smf (simple machines forum? good choice!) and 71, repectively. The third also captures the value of topic, and that value is then available as %1 in the rewrite rule itself.

I also added an "l" to the flags, for "last rule", so no further processing continues after this rwrite. You could have many similar rule blocks in one htaccess file.

have fun!

;o)

ps.. I notice that your rewrite rule begins with a forward slash ^/index.php…, so perhaps you are doing this in httpd.conf or a vhosts file or something, as opposed to an .htaccess file. Yeah?

If that's the case, simply add the forward slash back in.. RewriteRule ^/index.php /test/etc. … I'll leave the above example without the slash, though, because many will copy-and-paste, and most folk are using .htaccess to make these sorts of changes.


kai - 16.02.06 2:27 am

Man, Cor, THANKS SO MUCH! Like I said I've been slaving away the past few days trying to figure it out, scoured SMF & Joomla forums all over... and you answered it with one post. smiley for :geek:smiley for :cool:smiley for :D

Yes, I'm running SMF smiley for :cool: but moving away from Joomla (towards Tinyportal actually), hence the need to redirect all com_smf links to default SMF URLs.

My next step is to redirect SEF URLs, which would be in the form of http://www.mysite.com/component/option,com_smf/Itemid,71/topic,385.0. Let's see if I can figure this one out...

ps.. I notice that your rewrite rule begins with a forward slash ^/index.php…, so perhaps you are doing this in httpd.conf or a vhosts file or something, as opposed to an .htaccess file. Yeah?

Err, nope, sorry I guess the ^/ wasn't supposed to be there and threw you off? smiley for :lol: I'm just doing this in 'plain ol' .htaccess.

I'm thinking I might do a part three that is only examples from these comment Q&A's.

^^ would be a great idea!

Thanks again, cor :)


kai - 16.02.06 4:56 am

After spending a bit of time tinkering, I realized redirecting http://www.mysite.com/component/option,com_smf/Itemid,71/topic,385.0 is a bit different than my previous URL. For starters, there are no variables, so I don't need as many QUERY_STRING bits, correct? (no ?option=abc)

I'm trying something like this without the RewriteCond:
RewriteRule ^/component/option,com_smf/Itemid,71/topic,(.+) /forums/index.php?topic=%1 [r,l,nc]

What I want the above to do is to capture the part after "topic," and rewrite it as %1... but apparently it's not happening.
....
THEN I went on to try RewriteCond, with something like


if
{URL matches component/option,com_smf/Itemid,71/topic,X}
then
{rewrite as /forums/index.php?topic=X}
endif


the not working code:
RewriteCond %{QUERY_STRING} ^/component/option\,com_smf/Itemid,71/topic\,(.+) [nc]
RewriteRule ^(.*)$ /forums/index.php?topic=%1 [r,l,nc]


Am I anywhere close? smiley for :erm:

Cheers


corz - 16.02.06 12:48 pm

Hey! Thanks kai!

Your second challenge is quite interesting. As you say about the variables, technically, there are none. It's a lot simpler than you imagine; this should work just fine..
RewriteEngine On
RewriteRule ^component/option,com_smf/Itemid,71/topic,(.+) /forums/index.php?topic=$1 [r,nc,l]
Crucially, note the use of $1, not %1. The latter is for braces matched in RewriteCond (conditions) lines. $1 is for braces matched in the RewriteRule line.

Basically, you had it to start with, except with %1 instead of $1, and, of course, you added that bloody slash! smiley for :roll:

have fun!

;o)


howto? - 16.02.06 2:08 pm

My server is not allowing me to upload htaccess file.
So, I have managed to create the htaccess file thro' a php code, but the url rewriting stuff is not working.
Any suggestions, pleaseeee.






corz - 16.02.06 2:20 pm

howto?, quite possibly your host does not allow .htaccess files. Otherwise why would they prevent it?

Or possibly your ftp client is incorrectly setup; have you uploaded .htaccess files before?
being mindful that many visitors are beginners! smiley for :D

One simple test: put the following command into your .htaccess file..
87w98n7y(*&BY()*&
9876jkfdskddkj 
;o)
Which is a special command designed to produce a "500 error". It's foolproof. Drop that command into your main .htaccess file, and then load your front page.

If you DO NOT get a 500 error, and your page loads just fine, then .htaccess is disabled on your site, and you'll have to ask the server admins about it. Tell them to get their act together, or whatever.

for now..

;o)


howto? - 16.02.06 2:46 pm

Thanks a lot Cor,
First - its a yahoo server.
Spoke with them. They do not support .htaccess.
Secondly - I have used htaccess before. I tested it out the same code on my other sites (not hosted with yahoo) and its working fine.

My problem -- Client does not want to shift from Yahoo! server as his site is running there for quiet some time and yahoo does not support htaccess.

Am I in a soup??



corz - 16.02.06 3:09 pm

Ahh, Yahoo! That same company that helped the Chinese government to prosecute a reporter for publishing "dissent". I'd get as far away from them as possible. smiley for :blank:

Be straight with your client. Tell him that if he wants this functionality, he will have to move to a real web host. There are loads of great deals kicking around.

Use the trick I just used on page one, say "If you are serious about your website…", or something like that. Headology.

If you have a few clients, consider getting yourself a sub-hosting deal on a server with gobs of space and bandwidth, then you can run all your clients domains inside it, in an environment you know and trust, as well as give them a great deal on their hosting, better for everyone!

l*rz..

;o)


Bilio - 18.02.06 2:21 pm

hi, it's possible makes ?id=x&y to x.html&y.html
www.site.com/?id=one to www.site.com/one.html ??

if that is possible, please gives me one example

Thanks a lot


corz - 18.02.06 5:17 pm

What did you try?
and what happened?

;o)


Bilio - 19.02.06 12:58 am

i tried
<IfModule mod_rewrite.c>
Options + FollowSymlinks
RewriteEngine on
RewriteRule $1.php ^(.*)\.htm$
</IfModule>

but doesn't work. I don't know if is that.

if not.. please show me the correctly code.

remember, www.site.com/?id=name to www.site.com/name.html
Thanks a lot!



corz - 19.02.06 2:45 am

<IfModule mod_rewrite.c>
  Options + FollowSymlinks
  RewriteEngine On
  RewriteCond %{QUERY_STRING} id=(.+) [nc]
  RewriteRule ^(.*)$ /%1.html? [r,nc]
</IfModule>
will work.

;o)

ps.. in case you wondered, the "?" at the end prevents mod_rewrite tagging the variable back on the end of the new URI, prevents it getting itself into a loop.


Bilio - 19.02.06 11:07 am

Hello. Thanks!
between very test i don't can work that.
i tried with
<p><a href="?i=asdasd">asdasd</a></p>
<p><a href="site.php">site.php</a></p>

it shows in the same one.

the link ?i=asdasd had to pass asdasd.html or not?


Bye!smiley for :lol:
tks!


AMJ - 21.02.06 3:40 pm

Hi,

I am trying to permanently redirect our PHP scripts to a more search engine friendly all HTML format. However, when I put "R=301" in any line other than the first RewriteRule, the site stops working and FireFox displays "The page isn't redirecting properly".

Can you help? Any assistance would be greatly appreciated.

Here is the contents of our .htaccess file:

.htaccess:
DirectoryIndex index.php

AddType text/css .css
AddType text/x-server-parsed-html .html
AddType text/x-server-parsed-html .htm
Options -Indexes

RewriteEngine On
RewriteBase /

RewriteRule ^index.php$ gb/index.html [R=301,QSA,L]
RewriteRule ^(.*)/index\.html$ home.php?sl=$1 [QSA,L]
RewriteRule ^index\.html$ home.php [QSA,L]
RewriteRule ^(.*)/product/(.*)\.html$ product.php?productid=$2&sl=$1 [QSA,L]
RewriteRule ^(.*)/category/(.*)\.html$ home.php?cat=$2&sl=$1 [QSA,L]
RewriteRule ^(.*)/pages/(.*)\.html$ pages.php?pagecode=$2&sl=$1 [QSA,L]
RewriteRule ^(.*)/(.*)\.html$ help.php?section=$2&sl=$1 [QSA,L]



desperate - 22.02.06 3:01 pm

Hi Cor,

The site is very helpful. Thanks a lot. Here is my problem:

I want to redirect www.servername.com/index.htm to www.server2.com/index.php which was pretty easy.

Redirect /index.htm http://www.server2.com/index.php

This also redirects test.servername.com/index.htm to www.server2.com, while I just want www.servername.com/index.htm to www.server2.com


Sooo out of luck and sad (Soolas) - 22.02.06 3:31 pm

Good lord. I beg of you to help.

I have a forum and I want it to have a myspace look.

http://members.mysite.com/your_name_here.

this works (There is a google site map thing there I hope that isnt affected as well):

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(sitemap.*\.(xml\.gz|txt))$ vbseo_sitemap/vbseo_getsitemap.php?sitemap=$1 [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_HOST} ^members.mysite.com$
RewriteRule ^$ http://www.mysite.com/forums/index.php
RewriteRule (^[-_A-Za-z0-9\ ]*$) http://www.mysite.com/forums/member.php?&username=$1 [nc]
RewriteRule (^[-_0-9\ ]*$) http://www.mysite.com/forums/member.php?userid=$1 [nc]


but EVERYTHING goes thru to it. Some urls are not for example:

http://www.mysite.com/forums/login.php?a=pwd&u=38553&i=27923248

gets confused somehow as a uers... and of course this user does not exist... the proper "user does not exist" page shows but what if the person needed to change thier login... they cant get to the page.

HELP!

If you have the time thanks second question is what if you have multiple things in your .htaccess how does the server reconize what is for what?


corz - 22.02.06 4:11 pm

Bilio, ferfuxake! if your URL's are using i="" then use THAT in the rules. You asked for id="", so that's what you got. If you don't want "id", remove the "d" !!! smiley for :roll:

AMJ, firstly you wrongly assume that something.html is more "search engine friendly" than page.php?section=something. It isn't. Why not save yourself a heap of unnecessary work and go concentrate on some content, the number one search engine friendly thing you can do!

As for the rules, as the Americans say "Don't even go there!". I could spend the next half hour going through each line of the .htaccess asking "wtf is this for???", but what would be the point? For instance, is "gb" a file? Do you really want QSA here? Why is is the last rule [L] when you *need* the rules after it to also run, etc. etc. etc.

Start simple. Remove rewritebase and other crap you don't need and do this one rule at a time.

Or a *much* better idea: forget it, it's completely and utterly unnecessary.

desperate seems to be a common posting nickname! smiley for :lol: Helluva state to be in!

I'll assume you are working in your main site httpd.conf, how else would rewrite rules span domains?

That's a clue, see? In short, put the rewrite rules for a domain INSIDE THE DOMAIN. Use a vhost block, .htaccess, whatever.

If you must put them in the main config, use rewritecond lines to catch only the requests for particular domains. Plenty of examples above.

Soolas, your second question is directly related to your issue. When I look at your rules, *I* don't know how mod_rewrite will figure it out, either!

Go read the documentation before trying something this funky, and then start simple. Remember, rules are processed in order. When a rule is matched, mod_rewrite goes back through the rules looking for any rewritecond lines preceeding it. If they match, it processes the rule, if they don't, it doesn't.

In other words, only your first rewrite rules is covered by the above rewritecond lines.

Start again!

;o)


Soolas - 22.02.06 8:30 pm

So should I make two .htacess files? This folder is not at the root.

Ok. you know what. LOL. I have no idea what to do.


shakeer c k - 28.02.06 6:20 am

I want to redirect ^/myco/USeng/nav/-536884605.0/pc.html to
myco/product.jsp?cc=US&lc=eng&nav=-536884605.0 , the nav is optional.
If nav is there i should include in the new url, else dont should include in the new url. And more over i want to display the requested url on the browser address location , not the redirected new url. If anyone can help , please respond and mail me to shaki_ck@yahoo.com


Deb (plain and simple) - 28.02.06 9:00 pm

I've spent most of the day trying to find out how to do with my addon domain what you did with your addon domain for Karma. I finally found it here in your post of 9th Feb, so I tried your code on mine but with names replaced obviously. Alas, it didn't work - it redirected my main site to the addon domain. I've changed it back and it is back to normal, thankfully.

So please tell me where I went wrong. I hate to see:

addonfolder.mainexample.com

The two sites are totally unrelated. I managed to change mainexample.com/addonfolder by a simple redirect to go directly to its own domain name, but it didn't work for the other way too. smiley for :roll:


Deb (plain and simple) - 28.02.06 9:52 pm

btw, I did read all of part one and two before posting :)


corz - 01.03.06 9:51 am

Deb, I can't see what you are doing wrong, unless I can see what you are doing! Show us yer code!

akalias, I moved your comment to page one (makes a change; it's usually the other way around!)

;o)


Deb (plain and simple) - 01.03.06 3:52 pm

Ok, are you ready?

I've got the main site, then I've got two addon domains in folders. Main site is in no way related to the other two so I want to avoid such things as: addon.mainsite.com or mainsite.com/addon

I want those redirected to their own domain names.

I managed to do the mainsite.com/addon with:

Redirect 301 /addon/ http://addon.com/

but cannot get rid of the addon.mainsite.com (it's driving me nuts! lol)

This is where I tried your code that you had used for Karma's site (lovely dog btw):

rewritecond %{http_host} !^$
rewritecond %{http_host} ^(www.|myaddon.)?mainsite.com [nc]
rewriterule ^(.*)$ http://myaddon.com/$1 [R=301,NC]

What that did was redirect everything on mainsite.com to addon.com

Another thing I'd like to know is this bit of your code: ^(www.|myaddon.) is that like a variable between having someone type the www or not? i.e. it can be either way?


corz - 01.03.06 5:20 pm

I need to create a smiley for :gutted:
I finished a cool reply for you, hit "publish", and my browser promptly crashed. smiley for :aargh:

/me gets refreshments, meditates for a minute, attempts to reconstruct...


Right Deb, it's clear to me now! smiley for :D

You are putting these directives in your main .htaccess file!  That's why they are interfering with your mainsite.com requests. These rules belong inside the subdirectory! i.e. /path/to/mainsite/addon/.htaccess

Remember, the addon domain is a real domain, and so the .htaccess file inside it becomes its "main" .htaccess, and the /addon/ subfolder is its root.

And yes, we don't want to deal with http://addon.mainsite.com or http://mainsite.com/addon (or even http://www.mainsite.com/addon), but those are, in fact, exactly what the server has to deal with. So we have to catch them all, but only once the request is inside the subfolder.

In effect, there are four browser routes into that folder. That is why we have the (www.|myaddon.)?mainsite.com which catches all three incorrect routes, and permanently redirects them to the correct, cute TLD, which becomes the only way in.

Hopefully that makes things a little clearer.
And hopefully it will now work!

;o)


Deb (plain and simple) - 01.03.06 6:29 pm

smiley for :eek: yes I was! The simple redirect: Redirect 301 /addon/ http://addon.com/ worked fine from there. Should I move that to the htaccess for the addon or leave it where it is or delete it? (I can just see me redirecting visitors of all my sites to the wrong ones! I can picture it now: they want a local place but they end up on the moon! smiley for :lol:) hmmm, now there's a thought for the sp*mmers smiley for :ken:

As you've been so helpful to me, thank you very much, I shall return the favour if I may. I have included my email this time and if you go to that domain >forums> guest/visitors> start topic, you will see a whole load of smileys on the left - feel free to snag any you want.




Mark - 01.03.06 6:50 pm

Great article. But something isn't working for me. Here's what I have:

Options +FollowSymlinks
RewriteEngine on

# Set the RewriteBase to "/" for the root level to base everything on... I guess.
RewriteBase /

RewriteRule ^oldpage.php(.+) newpage.php$1&otherid=0 [R]


The $1 should have all the variable stuff in it from the php.

So I'm looking for it to do this:

oldpage.php?id=1 redirects to newpage.php?id=1&otherid=0


Why is this not working? Any help would be appreciated.

Thanks


Deb (plain and simple) - 01.03.06 7:18 pm

oh heck, I tried it in the addon htaccess:

rewritecond %{http_host} !^$
rewritecond %{http_host} ^(www.|myaddon.)?mainsite.com [nc]
rewriterule ^(.*)$ http://myaddon.com/$1 [R=301,NC]

Result was that it redirected the addon.mainsite.com to the addon url - Great!

but.....

it did weird things to mainsite.com/addon which had been ok before using the simple redirect in the mainsite htaccess. I tried removing that completely, but it made no difference. What I was getting when trying: mainsite.com/addon was:

addonURL/home/CPusername/public_html/addon folder

I still need to get rid of the www too as the addon site can still be accessed by that.

My poor little brain is now boggled smiley for :eek:


Deb (plain and simple) - 01.03.06 7:43 pm

Just got rid of the www :)

...and put it back again cos it looked nayyyked! smiley for :lol:


corz - 02.03.06 3:00 pm

Deb, I'm not entirely sure what your current situation is, but to recap; you need NO RULES WHATSOEVER in the mainsite .htaccess. As far as your main site is concerned, the other domain does not exist. period. If you have any rules anywhere else except inside /addon/, delete them right now!

Whether you use www. or not in the final URL is irrelevant, simply add it or don't, to the RewriteRule line. I don't dig it, but you're right, some URL's look nekkid without it. The www inside the condition ONLY catches requests for http://www.mainsite.com/addon/…, it won't interfere if you add www to the start of the addon domain.

Mark, try something like this..
rewritecond %{QUERY_STRING} id=(.+) [nc]
RewriteRule oldpage.php /path/to/newpage.php?id=%1&oldid=0 [r]
That should do the trick alright.

;o)

ps.. I'll pass on the smiley offer, Deb - you surely weren't serious! smiley for :eek: - but if you really want to give something back, PayPal is good! smiley for :ken:


Cornfused - 07.03.06 5:02 am

Okay now that I've read this whole page I'm even more lost than I started.

I migrated one of my forums from phpBB to vBulletin..

But alas all of my indexed pages are as www.myexample.com/viewtopic.php?t=xxxx and of course vB looks like myexample.com/showthread.php?t=xxxx

sync'ing up the topic numbers won't be too difficult, just time consuming, and a lot of rules, so in the interim I was trying just redirect all requests for /viewtopic.php to the index.php, seemed simple enough, obviously I'm cornfused...

This is what I was trying to use(from your example above.


Options +FollowSymlinks
rewriteengine on
rewriterule ^(.*)\viewtopic.php$ http://myexample.com/index.php [r=301,nc]

Please feel free to tell whatI hosed up!

Then if I can impose a little further an example of how to properly write a rule for changing viewtopic.php?t=1234 to showthread.php?t=5678

Thanks!



Jack - 08.03.06 5:28 pm

I think i made a post on the wrong page, so I reposted on this pages. I added to my initial post; if you can take a look at the questions I have.

Sorry for the double post.

Thanks for your help


---
(Q1)
I am trying to change all the extentions of my .php files to read .htm but on my site nothing changes in my browser with the script that I am using. Can you take a look at it?

current display: www.mysite.com/shop/home.php
what I need: www.mysite.com/shop/ home.htm

My current .hta access script:

Options +FollowSymlinks
RewriteEngine on

#Parse pages with extension PHP into HTM
AddType application/x-httpd-php .php .htm



(Q2)
I am trying to set up a store and I have 2 addresses on a site.
The homepage address: http://www.mysite.com
The store address: http://www.mysite.com/store/home.php

I want the keep the www in the http://www.mysite.com address
But I want to hide the www with the “store address location” only with the files displaying from the (eg. “shop” folder)

Can you display how I can do this?


(Q3)
Like mentioned before I have 2 addresses on a site.
The homepage address: http://www.mysite.com
The store address: http://www.mysite.com/store/home.php

How can I for the store address display the word “shop” in front of the web address name.
http://shop/myste.com/shop/home.php

Can you display how I can do this to?



Vikas Amrohi - 09.03.06 5:10 am

I wanna use url rewriting for my website.
[edit] link removed. ;o) [/edit]

Can any one help me how to proceed with this.


Thanks
Vikas Amrohi


corz - 11.03.06 2:43 pm

Ahh, got here at last! smiley for :D

Vikas Amrohi, it depends what you want to do. What do you want to do? (apart from link to your SEO site, that is)

Jack..

A1: that isn't a rewrite rule, that simply instructs the server to parse .htm files as php. In other words, you can put php code inside file.htm and it will be processed as php code.

If you want to rewrite all requests for something.php to something.htm (and I question the sanity of anyone who would want to do this!) then you need something roughly the opposite of the example at the top of the page. Something like..
Options +FollowSymlinks
 RewriteEngine on
 RewriteRule ^(.*).php$ $1.htm [nc]
but again I ask, WHY?

A2: There are examples of this further up the comments. I can write the rule for you, but it's better you do it yourself. Basically you need a rewritecond line that checks for "/store/" and only applies the transformation (rewrite) if it finds it. What have you tried so far?

A3: that simply isn't possible. But you could have http://shop.myste.com/home.php. You'll need to create that in your admin panel (CPanel, or whatever) before you can play with that.

If you do create this subdomain, scroll up the comments a bit for some groovy ways to operate such a scheme. type-to-find: karmathedog

Cornfused, did you try reading it twice?
That sometimes helps. smiley for :ken:

l*rz..

;o)


Olly - 11.03.06 3:00 pm

I've just completed a site build which involved maintaining the existing site structure (products.html becomes products.php etc) to allow the use of php.

The site ranks well in the search engines, and I originally thought the best way to address the change from .html to .php file endings was to use:

RewriteRule ^(.*)\.html$    $1.php      [L,NC]


However, i realise that there is a good chance that Google etc will also index the 'new' .php versions of the pages which might be seen as content duplication. I don't really want to use:

RewriteRule ^(.*)\.html$    $1.php      [R=301,NC]


as i'm not sure that all the links PR will be passed on, losing position in the search engines. I read in a post on another forum that the following could be used to ensure that all pages are seen as only .html and yet still processed as .php, but when i tried it, my browser returned an error (too many redirects).


RewriteEngine On

RewriteRule ^(.*)\.php$        /$1.html   [R=301,L]
RewriteRule ^(.*)\.html$       /$1.php    [L]



Any advice appreciated.


corz - 11.03.06 3:13 pm

Olly, If you use a proper 301 redirect, you google ranking should be unnaffected. It may blip for a few days, but probably not even that.

As an added measure, drop a nice links script into your site (click the "corz.org" at the very foot of this page for mine) so that google can find everything, or use a google sitemap.

A 301 is the correct way to do things. To google it's just the exact same page in a new location. If the only difference is the extension, google isn't going to penalize you. Content is what counts.

Don't go doing retrograde stuff like using .html links for .php pages, it only creates complications. Make a clean break, you'll feel better!

And, of course, ensure all the links on your own site link to the new address a.s.a.p.

Read this.

;o)


Olly - 11.03.06 7:13 pm

Thanks for the advice - another question... smiley for :D

The new pages, whilst containing essentially the same content (keyword
density, and paragraphs of text etc), aren't *exactly* the same; part of the
redesign was to recode the site in XHTML and CSS from tables, in addition to
using PHP, and so layout is quite a bit different. With this in mind, would
you still advise using the redirect below in terms of risk of being
penalized by Google and losing PR/position?

RewriteRule ^(.*)\.html$    $1.php      [R=301,NC]


Thanks,

Olly


corz - 12.03.06 1:07 am

Olly, if anything, switching your design from tables to an xhtml/css scheme will probably help your ratings. Suddenly everything is so accessible, your pages are easier to spider, easier to navigate, all the good stuff.

And Google won't penalize you for outdated links on other sites, either, it's knows you've moved the content with a proper 301, so those links are still valid. But do what you can to get them updated, a.s.a.p, anyway.

Personally, I wouldn't hesitate to go through with the xhtml/301 strategy. Like I said, you may experience a Google blip, but it will be a short-term effect, if it happens at all (get a sitemap). In the long run, moving to lightweight xhtml/css can only improve your ranking, in many ways.

I switched these htaccess pages to xhtml about two months ago; Google: htaccess tricks
As you can see, my ranking is unnafected. smiley for :ken:

Of course, I'm assuming your new pages are well formed, validated xhtml, and you aren't trying to trick Google in any way; always a bad idea.

Content is what counts.

;o)


Jack - 13.03.06 5:09 am

RE1:
I added the script to the .hta access file to rewrite all the .php requests to .htm but, the page never displayed; an error message is all that showes up displaying a page error. Do you know what that could be?
All the .php extentions are from a small online cart that I am building, do you think that some other scripting needs to be modified?

i.e.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*).php$ $1.htm


---

RE2:
I didnt know what to add/change with the script can you help me out with this?
Currently I have what you had posted on your site hiding the www in the address bar in its entirity.

rewritecond %{http_host} ^www\.mysite\.com
rewriterule ^(.*)$ http://mysite.com/$1


corz - 13.03.06 8:49 am

Jack.

1. what error message, exactly?

2. try something like RewriteCond %{REQUEST_URI} store [nc], remember, you'll need an "r" inside the rewriterule [flags] if you want to change their address bar.

;o)

ps.. you keep calling it ".hta access", I assume that space is just your repeated typo. If not, remove it!


Jack - 14.03.06 4:57 am

Thanks for getting back with me; I attached the error messages that I am getting. The port 80 error is consistent, What is causing that in your script? If you can please take a look at the responses below.

(1.)

ERROR MSG:
Not Found
The requested URL /xcart/home.htm was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Apache/1.3.33 Server at www.michaelmoskva.com Port 80

SCRIPT USED:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*).php$ $1.htm [nc]


(2.)

(a)
ERROR MSG:

Not Found
The requested URL /xcart/home.htm was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Apache/1.3.33 Server at www.michaelmoskva.com Port 80

Using your current SCRIPT:
rewritecond %{http_host} ^www\.mysite\.com [nc]
rewriterule ^(.*)$ http://mysite.com/$1 [r=301,nc]

(b)

ERROR MSG:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, wwwadmin@rmbmco.com and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/1.3.33 Server at www.michaelmoskva.com Port 80


When this SCRIPT is USED:
RewriteCond %{REQUEST_URI} store [nc],
rewriterule ^(.*)$ http://mysite.com/$1 [r=301,nc]


corz - 15.03.06 4:20 am

Jack, that isn't my script, it's just apache directives. And they were just an example. The error.. The requested URL /xcart/home.htm was not found is fairly explicit; the document isn't there. If your rewrite works, and you request /xcart/home.php, and there's no actual real file at /xcart/home.htm, then you will get a 404, of course.

Additionally, your 404 page is missing. Fix that!
All regular web servers run on port 80, that part of the error isn't significant.

Are you new to all this?

Error b - you could have simply said "a 500 error", but then, I did say exactly smiley for :roll: - is probably caused by that comma you mistakenly included with the directive, which again was an example (spot the words "i.e." and "like" preceding them), something to consider and work from, not meant to be just copied blindly into your .htaccess file.

If you just copy stuff blindly into your .htaccess without understanding it, you are a) asking for trouble, security-wise, and b) likely to add punctuation not intended to be in the directives, like, for instance, a comma.

l*rz..

;o)


Jack - 15.03.06 6:11 pm

(1)
Actually, yea I am new to this. I though that it would just overite the .php extension to an .htm guess I was off on that.

- Currently I only have php files, what do I do to make an .htm file display the php information?

- You know in the address bar if I key in a different location that isnt located in the site directory is it possible to reference the user back to the page they were at befor they keyed in a invalid web location?

ex.
IF mysite.com/store/login.php exists in the directiory
and I key in mysite.com/store/logininfo.php (this directory does not exist) THEN the customer will be referenced back to the mysite.com/store/login.php page instead of a error page displaying?

So insted of error page displaying, a client side error is redirected to the page they were viewing befor the error occured.

(2)
I understand your concern with copying files, Thank you.

You were write with the comma, it was causing the error. The page doesnt display errors but in the address bar the http://store.mysite.com/... doesnt display.
I dont understand what you mean with (spot the words "i.e." and "like" preceding them. Confused on doing this?



corz - 15.03.06 11:13 pm

Jack, if you only have php files, WHY ON EARTH are you trying to rewrite links to .htm??? In a word: insanity. Scrub the whole (1) idea and leave the php extensions alone!

I'll get to the question you sneaked in here, after (2)

(2)
I was concerned with you blindly copying commands into your apache configuration, NOT files!

And, to get the new address to display, as I said before, add an "r" to your flags. (that's the bits inside [square brackets] at the end of the rewrite rules)

Jack said..
I dont understand what you mean with (spot the words "i.e." and "like" preceding them.

What I mean is, "Something Similar To", or, if you prefer, "Something LIKE This". In other words, the commands I gave you were Starting Points where you could begin to develop the exact rules you need. They were not the complete rules. Not for off it, sure, but not meant to be taken as-is, you know. aka "examples".

(3)
Jack said..
is it possible to reference the user back to the page they were at befor they keyed in a invalid web location?

Hmm. This Q&A might have some value after all! Yes, it is possible (what isn't?), and it's an interesting approach to 404's. There are drawbacks; the user is unlikely to know they got a 404, and when they land back on the old page, they may be slightly confused, and quite probably click the link again, just to see.

Essentially, this is how you would do it..

1. ensure you have a valid 404 script location set in your apache configuration (either in the master httpd.conf, or else in the main .htaccess for your site). If your 404 page lived inside a folder called "err" in the top level of your site, and was called "404.php", that line would look exactly like this..
ErrorDocument 404 /err/404.php

2. Create a 404 script with something like this inside it..

<?php
if(!empty($_SERVER['http_referer'])) {
    
header('Location: '.$_SERVER['http_referer']);
} else {
    echo
'404. Page not found. sorree.';
}
?>


3. save the file inside the top-level "err" folder and call it "404.php".

The script will redirect the user directly back to where they came from. If, for some reason, the referrer information is missing (rare, but it does happen) the script(Yes, THAT *is* a script!) simply reports the 404.

You may want to add a timer to the page, and allow the user to see a message for a few seconds before redirecting back to the referring page, but I'll leave that as an exercise for you.

Of course, the very best way to deal with 404s is to not have any broken links in the first place!

;o)


Jack - 16.03.06 1:17 am

Thanks for your comments, I relly appreciate you getting back with me on that added question, I will apply it and see how it works.

RE(1)
a.
I have a cart I'am building using .php extention but the rest of the site is all .htm; with my questions they were all referenced with editing the store with the php extentions.

I just want to have all the extentions display the same; is their any tutorial or directions you could provide me with that could do this referencing .php extentions to .htm?

b.
Also is it possible to hide the .php extention. I know of the integration of an <iframe> but is their any way using .htaccess?



corz - 16.03.06 5:36 am

(1)
It's a trivial rewrite, Jack. Roughly the opposite of what I first interpreted as your desire. Try this..
 Options +FollowSymlinks
 RewriteEngine on
 RewriteRule ^(.*).htm$ $1.php [nc]
That will do the job. The user requests whatever.htm, and in the background it is translated to whatever.php, but, remember, you still have to present those links as something.htm in the first place. Can you CMS1 do this?

mod_rewrite is only one half of the equation.

(2)
See (1). If you silently rewrite all requests for .htm to .php then the user never sees the .php extension, period. The crucial question is still, can your CMS present its links as .htm instead of .php?

your CMS is the other half of the equation.

;o)

ps.. I'm tired of removing the [sm]small[/sm] tags around every one of your posts, can you please respect the page font size and only use [sm]small[/sm] tags around things you really need to be small, i.e. not the whole post, thanks.

references:
CMS = Content Management System; shopping cart, blogger, wiki, whatever.



smithdiesel - 16.03.06 9:44 am

I didn't know what rule to have for redirecting the following

index.php?main_page=index&cpath=31_14 to category/31/14.html

cpath will have values like 31_14, 31_14_21 etc.


corz - 16.03.06 4:27 pm

What have you tried, smithdiesel?

You want me to help fix your code, I gotta see it!

;o)


mike - 16.03.06 10:26 pm

First off, great article, easy to understand.

Unless I missed it, I haven't seen this addressed:

I'd like to change my root directory with a redirect. My current root directory and main site is located in /public_html/ but I want it to be /public_htm/folder/.

My .htaccess file, which is in /public_html/ :

Options All -Indexes
Options +FollowSymlinks
RewriteEngine on
RedirectMatch permanent ^/folder$ http://myexample.com


When I go to my site I receive a 403 Permission Error.

How do I fix this?

Thanks


smithdiesel - 17.03.06 6:37 am

Cor,

if the cpath value is simple a number (31) then the rule is

RewriteRule ^index.php(.+)main_page=index(.+)cpath=(.*)$ category/$1/$1.html?

i need the rule if the value changes to 31_14 or more sublevel values like 31_14_16 etc.

Can you help me.


Dino - 18.03.06 2:30 pm

Hi

I need help for my site.

I want when someone click exactly on this link http://www.mysite.com/category.php?id=0 he gets www.mysite.com ?
Can you pleaase post your solutions.
Thanx


fruit loops - 18.03.06 8:26 pm

Hi, Nice tutorial.
Im running into a few problems of my own.
Currently i have a site thats in "C:\apache2\htdocs\mysite"

So when i acces the site...i do localhost/mysite/homepage.php

Im trying to rewrite so that i can just do localhost/homepage.php

this is my rewrite rule, but its not working.

rewriterule ^(.+)\.php$ /mysite/$1.php


Am I doing somkething wrong? I get an internal server error.

But if i do this..

rewriterule ^(.+)\.htm$ /mysite/$1.php

it will work.

Why is that?


Also, my hompage.php files has images that is being referenced like images/image.gif. So actually, w.o the rewrite rule, the images will load properly because the relative-ness of the directories are correct. With the rewrite rule, they become unloadable ..they are being referenced as localhost/images/image.gif when they should be localhost/mysite/image.gif.

Is there a rewrite rule to fix the image issue or do i have to go through the source code and modify the path?

Thanks




tony - 19.03.06 4:58 pm

I have links on a page that are derived from db field values like http://www.myexample.com/myfolder/myurllinks.html?parameter=3003.
Once the link is clicked I'm trying to direct the user to predefined pages with html anchors like http://www.myexample.com/myfolder/myurllinks.html#a3003.
What is the best method / regex to include in my htaccess that would replace '?parameter=3003' with '#a3003'.

kind regards.


Dep - 20.03.06 1:45 am

Hi!

I want to redirect users like this:

If user requests any of this:
http://mypage.com/folder
http://mypage.com/folder/
http://mypage.com/folder/?sometging=somethin
http://mypage.com/folder?somethnig=somethin

then apache should redirect to:
http://mypage.com/?accessed=folder
http://mypage.com/?accessed=folder/
http://mypage.com/?accessed=folder/?sometging=somethin
http://mypage.com/?accessed=folder?somethnig=somethin

I experimented ~2 hours but no results.. :(
This is not working: RewriteRule ^(.*)$ ?accessed=$1

Where is error? Thnx.


Jonathan - 20.03.06 3:27 pm

Hi,

I have a nice site, just as I want it.
I changed it from non sef url's to sef urls (joomla)
but the old links from google don't work,
here's what goes wrong:

Not Working
http://www.johannes-multimedia.nl/content/view/182/38/Lang,NL/

Working
http://www.johannes-multimedia.nl/content/view/182/38/Lang,NL
So the trailing slash is the problem with these URL's

I however would prefer to redirect to:
http://www.johannes-multimedia.nl/content/view/182/38/
Which also works.(completely removing the Lang,NL/ part

Basicly I only want to rewrite when "Lang,NL/" is in the URL
.htaccess
RewriteCond %{QUERY_STRING} Lang,NL/
Rewriterule ...

I have tried stuf, and at my last atemt my site went completely down, the root, and al subddomains and subfolders.

Sory I couldn't figure this out myself, even with all of these examples posted here.

My current .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /index.php/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
This .htaccess file makes all of the links on my site work, now I need to get old URL's from google and other sites working again.


jignesh - 23.03.06 8:00 am

it is really nice matiral. This all content help me a lot.


Dennis - 23.03.06 8:31 pm

Hello, and thank-you for one of the better examples on the web of this mod, I have a problem though, my site http://mister.mine.nu:8080 seems to block all images when I apply the rewrite rule for hotlinking images, this is what I am using below:
_______________________________________________________
Options +FollowSymlinks
 # no hot-linking
 RewriteEngine On
 RewriteCond %{HTTP_REFERER} !^$
 RewriteCond %{HTTP_REFERER} !^http://(mister.)?mine.nu/ [nc]
 RewriteRule .*.(gif|jpg|png|jpeg)$ http://mister.mine.nu/upload/hotlink.txt [nc]
_______________________________________________________
can you please help, I tried various formats, none do the trick, all my images are being blocked, page layout as well, is what I have correct? do i need to specify special access to specific folders?

any help would be much appreciated.

Regards
Dennis


Dennis - 23.03.06 9:07 pm

I hope you can see this: Reminder i am on port 8080 thx
this is a free domain redirecting to my ip address, forgot to mention that.

#php_flag magic_quotes_gpc off
Options +FollowSymLinks -Indexes
<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(mister.)?mine.nu/ [nc]
    RewriteRule .*.(gif|jpg|png|jpeg)$ http://mister.mine.nu:8080/hotlink.txt [nc]
	RewriteBase /
	RewriteCond %{REQUEST_FILENAME} -f [OR]
	RewriteCond %{REQUEST_FILENAME} -d
	RewriteRule ^.*$ - [S=35]
	RewriteRule ^article/(.*) blog.php?id=$1
	RewriteRule ^archive/([0-9]{4})/?$ index.php?date=$1
	RewriteRule ^archive/([0-9]{4})/([0-9]{2})/?$ index.php?date=$1$2
	RewriteRule ^archive/([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ index.php?date=$1$2$3
	RewriteRule ^category/(.*) index.php?cat=$1
	RewriteRule ^comments/(.*) comments.php?id=$1
	RewriteRule ^trackback/(.*) trackback.php?blog_id=$1
	RewriteRule ^page/(.*) static_page.php?id=$1
</IfModule>




corz - 24.03.06 12:41 am

Yikes!

Sorry dudes, I've been a bit indisposed this week.

mike, you change your root directory in httpd.conf. It's a server directive, not a rewrite. Something like this..
rewritecond %{REQUEST_URI} !folder/ [nc]
rewriterule ^(.*)$ /folder/$1 [nc]
would work, but really, you want to do this properly in your httpd.conf or control panel; rewriting every single request is simply wasteful.

smithdiesel, this may be a first, but, I look at what you want and I can't think of an elegant way to do it, and besides, your question is back-to-front! Isn't it? I might have a wee mess around over the week-end and see if I can work something out. By the way, if you figured it out in the last few days, post the solution! Except the other way around, right? I might wait until you back to me on that. smiley for :ken:

Dino, that's too easy! There are dozens of examples above!

fruit loops, see my notes to mike, above. clearly you have access to the main httpd.conf, so why not set the document root there, or better yet, create a "mysite" virtual host. (navigate up to corz.org/serv/ for details of how to make a virtual host).

You definitely don't want to go editing path statements ANYWHERE in your code. Good code will work wherever you drop it. Relative links are just fine, so long as everything else is working.

Yes, you can do this with mod_rewrite, and have all the images working perfectly (there's a good example further up the comments) but you want to avoid that route if at all possible, not only because it's wasteful, it's just plain silly!

tony, again, some great examples exist in the comments here (I really need to get around to packaging them up into a third page, because folk just don't seem to able to click the "previous page" or "show all comments" links above. smiley for :roll: Not to worry.)

You capture variables like this..
rewritecond %{QUERY_STRING} id=(.+) [nc]
rewriterule ^(.*)$ /%1.html? [r,nc]
the variable in this case being "id". And you slam it into the rewriterule with "%1". It's fairly straightforward once you know the mechanism. The "?" at the end of the rewriterule prevents Apache adding the original variables back on, by the way.

Dep, your targets are invalid, the last pair, anyway. The first set you deal with like this..
rewritecond %{QUERY_STRING} !accessed [nc]
rewriterule ^([^/]+)/?$ /?accessed=$1 [r,nc]
You can remove the "/?" from the rewrite rule condition if you want the trailing slash to carry through in the query, I just figured it was best to demonstrate how to remove it, which is what most people would want, I reckon.

Jonathan, use the principle I just demonstrated (SEE! Dep!), making your rule something like..

RewriteRule ^(.*)/? index.php

Dennis (I see you discovered the [pre] tags, good work! - by the way, I fished out your code form my handy post dumper dump and re-inserted it into your post, in [pre] tags!)

Okay, to the rule, by the looks and sounds of things it's all getting very complicated when probably all you need to do is.. wait for it.. add the name of the "free domain redirecting" HOST! Think about it, they are the referrer!

for now..

;o)


Jonthan - 24.03.06 3:44 pm

Hi,

RewriteRule ^(.*)/? index.php

I tried that, but it still won't work,
I still get a message that I would not be authorised to vieuw this content.
Removing Lang,NL from any url would also do,
reulting in a url like:
http://www.johannes-multimedia.nl/content/view/182/38//
(two ending slashes without the Lang,NL. So the previous URL should be redirected, to this url.

Even when I try something like:
RewriteRule ^(Lang,NL)?/? index.php
my url from google still won't work.


corz - 24.03.06 7:16 pm

Jonthan, perhaps there's funky stuff happening inside your php. I'm not familiar with joomla, so I can't say what that might be.

But that rule will certainly lop off a trailing slash, so long as other rules aren't interfering with that. This..
RewriteCond %{QUERY_STRING} Lang,NL/
would mess up everything, as would this..
RewriteRule ^(Lang,NL)?/? index.php
because a) "Lang,NL" is not a part of the query string, it's a regular path part of a regular URL, and b) "Lang,NL" is not at the start of the query (which is what the "^" is all about).

check that, and remove any such junk. If you want to post your entire .htaccess (removing any data you need to keep secure, of course) I could have a look at what *might* be interfering.

Another thought; you can remove individual variables with mod_rewrite, too, so perhaps you could catch it after the main joomla rewrite (so long as it has no "L" in its flags)..
rewritecond %{QUERY_STRING} (.*&)?LANG=NL(&.*)? [nc]
rewriterule (index.php$) $1?%1%2 [l,nc]
which would turn a request for http://example.com/index.php?var=foo&LANG=NL&cor=bar into http://example.com/index.php?var=foo&cor=bar.

There will be a way, though you better be quick; very soon Google will index all these 404's and you'll be reconsidering the term "search engine friendly", which dynamic variables ARE, by the way.

;o)


AQ - 28.03.06 9:51 pm

Hi Cor - Great article. Maybe I overlooked it somewhere, but here's what I am trying to do.

I have a url that reads http://mysite.com/blah.php?id=yourname

I'd like do a ReWriteRule that would convert make this URL: http://mysite.com/yourname

I have a feeling that it's something simple that I am overlooking but I can't quite get the syntax down. Any help that you could provide would appreciated immensely.

Thanks!
AQ


corz - 29.03.06 2:10 am

Yup AQ! You overlooked it!

Scroll up slightly to this post for the most recent example, right after where it says..

You capture variables like this..

have fun!

;o)


tony - 30.03.06 10:23 pm

cor
You're a hero. Your example gave me the heads up to solve something I have struggled with for sometime now. Of course I changed it to suit my needs so I include it here for completeness.


rewritecond %{QUERY_STRING} kua_num=(.+) [nc]
rewriterule ^(.*)$ http://www.myexample.com/acatalog/$1#a%1? [r,nc,ne]

Great article. I've taken the next step and ordered myself a book on mod_rewrite from 'A'.
Keep up the good work.
Kind regards.
Tony


bcwebstudio - 30.03.06 11:22 pm

Great page! I like your style, but I fear your response...

O Wise One of the Rewrite.. I beseech thee with what to me has been a life-long quest but for you is less than but a blink of the tip of a single eyelash. My rewrites work, but I can not seem to get them to hide the rewrite browser-side.

An example of what I wish to duplicate is your blog/2003-nov example above, so I even copied that line from your example into my .htaccess and commented it out, so I could be Just Like You.

Options +FollowSymLinks
  RewriteEngine on
  RewriteRule ^monitor([0-9]+) http://bcweb/apps/docketscroller/showdocket-js2.php?monitorid=$1 [nc]
# RewriteRule ^blog/([0-9]+)-([a-z]+) https://corz.org/blog/index.php?archive=$1-$2 [nc]


I'd like the visitor to see only, for example: http://bcweb/apps/docketscroller/monitor1

I didn't realize it until now, but you've been bombarding us subliminally with a big ;o) symbol in the bg. Puts me in a sarcastic mood.


bcswebstudio - 31.03.06 5:19 pm

my solution.. changing:
RewriteRule ^monitor([0-9]+) http://bcweb/apps/docketscroller/showdocket-js2.php?monitorid=$1 [nc]
to:
RewriteRule ^monitor([0-9]+) showdocket-js2.php?monitorid=$1 [nc]
did it for me.

So why does your example:
RewriteRule ^blog/([0-9]+)-([a-z]+) https://corz.org/blog/index.php?archive=$1-$2 [nc]
seem to work for you?


hafio - 02.04.06 5:35 am

Hi, i was wondering if it's possible to prevent people from saving a image from your webpages? I know there's the "anti-rightclick" method and the "hotlink protection" method. I have successfully done both but whenever i am inside my own pages, i can still save the images. Any idea? Thank you very much in advance.

ps: great tutorial site you got here, cheers!


corz - 03.04.06 3:42 pm

bcswebstudio, I'm guessing because "bcweb" isn't a real domain.

My own (new) blog permalinking uses directives placed inside the local folder, allowing me to have permalinks like this..

https://corz.org/devblog/2006-Q1/phpsuexec

which actually point to..

https://corz.org/devblog/index.php?archive=2006-Q1&blog=phpsuexec

The directive lives in /devblog/.htaccess and looks like this..
rewriterule ^([_a-z0-9]+)-([_a-z0-9-]+)/?(.+)?   index.php?archive=$1-$2&blog=$3 [nc,l]
I've had mixed results using domain-less targets inside folders, and it's usually easiest to just supply the full url, though the above works just fine with or without the "https://corz.org/devblog/" part. On some web hosts, you can even use "http://{HTTP_HOST}/" and it works great!

As we techs are fond of saying.. your milage may vary.

hafio, unless you want to password protect the image directory, there's isn't an easy way to do it, and even then, once logged in, they can save the images easily enough.

Apparently some porn sites manage this (so I'm told) so possibly there is some kind of javascript method kicking around. If you took the image data, encoded it with something simple (at the server end), like a binary rot-13, and then had javascript decode the data before displaying it as an image, then perhaps you could protect the image itself, but someone determined could still figure out your encoding method, by simply looking at the javascript.

Perhaps you might like to do some field research? smiley for :lol:

Seriously, though, there are better ways to do this. Content creators have access to all sorts of ways to embed copyright and other information in their images. If such things are important to you, google for "image watermark" or "free Steganography" or something.

If you sell images delivered over the web, you might want to check out something like CatchLock.

have fun!

;o)




Daniel - 05.04.06 2:43 am

Can I rewrite (redirect) to a different root directory?
Let's say example.com root directory is /here/is/domain
If a browser enters http://example.com/zzz then he will be forwarded to the directory /home/of/zzz where an index.html resides
^(.+)zzz$ /home/of/zzz

Thanks


corz - 05.04.06 5:23 am

Daniel, yes, you can use an internal redirect (no "r" in the flags), so long as you don't go outside the server's root, or your domain root, if you are on a shared server.

There are so many factors which can affect your access to various paths on the system, this one is best done with a suck-it-and-see approach, using some obscure filename as your test.

Looking at your question, however, I'd probably guess that it's NOT possible.

for now..

;o)


hOzEr - 05.04.06 11:08 am

hi.
how i can make one regex to accept a-z,A-Z,1-9 and accept "%" a-zA-Z1-9% , don't work. ?? example: ol%ds (it has of accept the %)

thanks a lot. smiley for :D


corz - 05.04.06 3:46 pm

Good question, hOzEr!

You probably just need to encode the "%" symbol for transport. In other words, your actual links would be http://myexample.com/ol%25ds.php, or whatever. You can't send a raw % in a URL; it has a special meaning; the server will only gronk. So encode the links. Then, this..
rewriterule ^([a-z0-9%]+)$  index.php?var=$1 [nc]
works just fine.

The captured variable would be ol%ds, as expected.

;o)


Gigi;) - 06.04.06 9:50 am

Hi,

Hot-linking,

is it possible (& how smiley for :)) to allow hot-linking, but the pics will have a nice watermark/text on itself?

THX! Great stuff in here!

Gigismiley for ;)


corz - 15.04.06 10:02 am

I could have sworn I answered this days ago, and a nice full reply, too. damn!

I explained how you'd need to serve them up with a php handler, and use GD to overlay the text on the image. I even gave a code example! wtf! smiley for :aargh:

och well, that's what you do! smiley for :roll:

Essentially this is what ampsig does, except you probably wouldn't want as much text, or as varied. Grab the source for lots of text-on-image type code examples!

;o)

[edit]

Hey! I fished my old reply from the post-dump (see! this is why I created that thing!)..


Sure, Gigi, that's possible, and a GREAT idea! But we've left the realms of .htaccess and dived into the wonderful world of php GD (or ImageMagick, if that's your bag); ie. server-side image processing, something I rather enjoy doing.

Instead of redirecting to a "hot-link image", You simply pass the requests through a php image handler and get it to overlay your watermark. Very clever!

I altered my own hot-link image a few weeks ago, and I have to say, it was a similar stroke of genius! Most hot-linking images are plain things, "no hot-linking" type messages and what-not, not only very boring, but they DO NOTHING.

Mine is now an image of the some text. It reads..

corz.org

So, instead of stealing images, my hot-linkers do free corz.org promotion! Just a pity you couldn't generate an image with an embedded link. Although, of course, you can embed lots of other things in them. smiley for :ken:

With php and GD, it's very easy to place text on an image, like this..

<?php
// 'watermark' in the upper left (x=6, y=3) of an image..
$my_text = 'watermark';

// open image and lay white text on it..
$img = imagecreatefromjpeg ('example.jpg');
$white = imagecolorallocate($img, 255, 255, 255); // white
imagestring($img, 4, 6, 3, $my_text, $white); // size 4 built-in typeface

// send the image
header("Content-type: image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>


Of course you could go on to use semi-transparent TrueType fonts and do all sorts of other fancy stuff, but that's it in a nutshell.

Have fun!

;o)

[/edit]



Milos - 17.04.06 2:41 pm

Hi,

I've created the .htaccess file with following lines:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^index\.html$ http://sto-posto-zabava.com/wap/index.wml

And placed it in my wap subdomain folder.

So, I wanted to redirect my subdomain wap.sto-posto-zabava.com to address sto-posto-zabava.com/wap/index.wml

The problem is that it won't work if you just acess the address wap.sto-posto-zabava.com, it'll only work in case you type in wap.sto-posto-zabava.com/index.html

Can I make the redirection work when you just acess wap.sto-posto-zabava.com?


Thanx,
Milos smiley for :)


Good Sit - 18.04.06 7:13 am

tell me use of .htaccess file


corz - 19.04.06 3:12 pm

Milos, that's because "index.html" is specified in the rule.
simply remove that. something like this..
RewriteRule ^(.*)$  http://sto-posto-zabava.com/wap/$1 [nc,r]
would be better. Check back through the comments for some very good sub-domain examples.

Good Sit, start at the top of the page!

;o)


Milos - 19.04.06 4:05 pm

Yes, it works. smiley for :D

Thank you man, you'r in my favorites now!


corz - 20.04.06 1:36 am

You mean I wasn't already? smiley for :eek:

smiley for :lol:

;o)


rajesh - 20.04.06 3:04 pm

hi,
i need help in writting a mod rewrite rule for a domain.
the problem is i have the url:
http://example.com/valentine/valentine-cakes . i don't want the folder/valentine to appear in the url.

smiley for :roll:


Dennis - 20.04.06 5:34 pm

Hello Corz.org,

Me again,

quick reminder, you suggested that my redirect host was the actual referer for my hotlink problem, well I contacted them and have seen other examples of redirect sites and my actual redirect hostname is the referer.

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mister\.mine.nu:8080/ [NC]
RewriteRule .*\.(jpg|jpeg|png|gif)$ [NC,F,L]

is not working for me, hotlinking continues, can you please guide me as to where my problem is?

your last suggestions was to replace http://(www\.)?mister\.mine.nu:8080/ with http://(www\.)?my\.redirecthost.org:8080/

if you can clarify this for me it would be greatly appreciated, you have my email address, contact me if you dont mind.

Thank-you


stu - 21.04.06 2:12 pm

Hello.

Im trying to make example.com/index.php to redirect example.com/ but it comes with double slashes like example.com//

Im using this: RewriteRule ^index\.php$ http://example.com/$1/

Is there any way to fix this problem.

Thanks.


Parikesit - 21.04.06 2:38 pm

Dear All,

Can I use mod rewrite for mirroring my website?

I have 2 server, server1 (ip=202.*.*.*) and server2 (ip=198.*.*.*). I want to split the visitor by ip-address.

When it comes from ip=202.*.*.* it will be redirected to my server1, otherwise it will be redirected to server2.

Thanks,
Parikesit


corz - 21.04.06 5:53 pm

Parikesit, it's really too late. By the time we get to reading .htaccess files, we are already performing the request. Setting up a permanent "round-robin" with .htaccess is probably doable, but a crazy waste of resources.

Your best bet would be to catch this sort of thing at your gateway, google for "balance" (along with a few web server related keywords) and you should get your man. Squid could probably do this, too.

From the balance manual..
A "%" instead of a "!" as a group separator declares the previous group to be of type "hash". This means that instead of a round-robin algorithm, a hash distribution based on the client ip address is used to determine the destination channel.

In other words, balance can do what you need.

rajesh, simple answer: move your document root to /valentine.

Or is this an "add-on" domain of some kind? If so, valentine shouldn't be in the URL, unless something is wrong with the setup.

If you want a more intelligent answer, give me some details about your setup, and *exactly* what you are trying to achieve, and *exactly* what the trouble is.

stu, try taking that extra "/" off the end of your rule, that should fix it. smiley for ;) By the way, the rest of the rule sucks, too. Where DID you get that thing!? This is enough..
RewriteRule ^index.php$ http://example.com/ [nc,r]

Dennis, that server is password protected, no way anyone can hot-link images from there! Your rule looks to specific to me (you don't need port numbers, for example), but if anything that should make hot-linking more difficulty, not the other way around. I'm surpised you can load images at all, even on your own pages!

If you want to mail me with the actual URL of your site (as accessed from the outside world), and a copy of your .htaccess, and the actual URL that is hot-linking your images, I'll definitely look into this for you, get back here with the results.

l*rz..

;o)


DavidR - 21.04.06 9:31 pm

Can you quickly tell me how to modify the following bit of .htaccess code derived from your site:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www\.davidr\.com [nc]
rewriterule ^(.*)$ http://davidr.com/$1 [r=301,nc]


...so that it not only redirects 'www.davidr.com' to just 'davidr.com', but so it also redirects 'www.davidr.net', 'davidr.net', 'www.davidr.org', and 'davidr.org' *all* to 'davidr.com' ? (Just three domains, but six possible variations, and I'd like them all to redirect to and use just 'davidr.com'.)

Thanks sincerely!
-David



Dennis - 22.04.06 12:06 am

Hi Corz,

I sent you all the information via email.

Thank-you


corz - 22.04.06 12:10 pm

DavidR, try rewritecond %{http_host} !^davidr\.com [nc]

Quick enough? smiley for :)

Yup, Dennis, some of it. I await the rest!

;o)


DavidR - 22.04.06 6:56 pm

sorry, now I'm confused. wouldn't it still need the variables and stuff so that www.davidr.net/some/stuff.htm still goes to davidr.com/some/stuff.htm ?


1840697e13@lpv.lv - 23.04.06 1:22 pm

Fabulous site.


Dennis - 23.04.06 4:02 pm

sent missing info

thanks


J-J - 24.04.06 12:44 am

Great info, has helped alot, although I do have a request.

I am trying to hide a URL, its long and its uggly. I have managed to redirect myexample.com/forum/car-1 with no problem. It leaves "myexample.com/forum/car-1" in the browser address bar but shows the page correctly.

I really would like it though to be able to have myexample.com/forum/car/1 - however when it takes me to the page, everything thinks its in mydomain/forum/car/ which obviously it isn't so pictures dont load and links don't work. Surely it can't be far off as the script is generating the page, just not correctly. Is there any way round this?

Also, when someone clicks on a link that has been auto generated, its the 'ugly' URL, is there a way to catch that, and send it to the clean address, incase someone wants to copy and paste the link? The urgly URL looks like:
myexample.com/forum/garage.php?mode=view_vehicle&CID=1
I want it to look like:
myexample.com/forum/car-1
OR
myexample.com/forum/car/1


Dennis - 24.04.06 2:42 pm

Corz,

I have successfully created my rule,

Thank you for your help.



corz - 25.04.06 1:44 am

I'm not sure what the state of play is, Dennis, time-zones and what-not, but hopefully your recent post was after your recent email! smiley for :lol:

My thoughts are that though the rule was shakey, it would work, the trouble is most likely further up the .htaccess file.

And it's "cor"!!! smiley for :aargh: For a capital "c", I use a left bracket, but that's just me. "corz.org" is like "cor's org", as in MINE, but you can't have apostrophes in TLDs. cor's == corz, see? smiley for :ken:

DavidR, yeah, but you still need the rewriterule! I would have explained more deeply, but you wanted a quick answer! That rewritecond should handle the whole show, though, unless I'm missing something.

J-J, the most important part of all this is the generation of the links in the first place. That's where you need to make the changes. Once you have your software producing the flat links, it's a trivial matter to redirect them to the *real* dynamic links in the background. It's irrelevant which delimiters you use (the characters separating the data, "-", "/", etc), so long as you match them in your rewrite rules.

Then you simply *don't* use the [r] flag, and keep everything transparent (the user ALWAYS loads the page's dynamic link in reality, only on the surface does it ever look flat, like the ocean). Drop something like this inside your forum directory..
rewriterule ^car/([0-9]+)/?  garage.php?mode=view_vehicle&CID=$1 [nc,l]
because the only value that changes is the number, the rule itself is very simple. If you replaced "car" for ([a-z]+), you could process that, too, have "view_vehicle" be something else, but you didn't specify.

But most importantly, you need to generate these links in the first place, which means inside your content management system. I'm stressing this because in your post it's almost an afterthought, an added bonus or something, but THAT is where the fundamental changes must be made. If you can't generate flat links, forget it. But if it's php or something like that, dig in!

If it's not your own CMS, contact the developer and ask if there's an easy way to do it. In corzblog it's a simple checkbox to switch between flat and dynamic links, but probably most CMS aren't so easy.

have fun!

;o)


nagendra - 25.04.06 8:42 am

I have two problems

1. I don't want to show my link or url to any one.
It should be static like www.hostrev.com
but now it is showing like www.hostrev.com/or_list.php, www.hostrev.com/or_create.php,etc

2. I have the url as www.hostrev.com/nare. I have to redirect it to www.hostrev.com/index.php. At the same time i don't have the folder nare, and i should have the nare will be in name variable

Thanks
Nag


corz - 25.04.06 1:24 pm

No, nagendra, you have at least three problems!

1. If you "don't want to show my link or url to any one", you might as well not bother having a web site! and www.hostrev.com/or_list.php and www.hostrev.com/or_create.php ARE static links.

Unless your site is all on one page, or controlled from one file, you need to have more than one link! And people *will* need to see them! Really, I don't know what you are trying to do, but if you show me the code you've tried so far, I might get some clues.

2. You don't need the folder "nare" to exist, because you are redirecting..
rewriterule ^nare/? index.php [nc,l]
something like that, yeah?

If you want help with 1.,provide more information and code, or a clearer explanation of exactly what you are trying to achieve.

;o)


nagendra - 25.04.06 1:50 pm

I have the url as www.hostrev.com/nare. I have to redirect it to www.hostrev.com/index.php. At the same time i don't have the folder nare, and i should have the nare will be in name variable.

for the above problem, it will not be nare all the time. That is also dynamic which is retrieved from the database, so i want code for that


Next,
I am having the whole site www.hostrev.com which has some 20 files, but i don't want to show all the urls in the address bar. I have to show url only as www.hostrev.com. I can call it internally know. I want code for that


corz - 25.04.06 1:59 pm

Show me your code!

I can't help unless I know what you want to go FROM AND TO. ie. examples.

Scroll up through the comments here to see how people usually frame these things. Even if it's back-to-front, at least I'll get an idea about *exactly* (and it must be exactly) what you are trying to do. "I want code" is not a big help to me, in fact, it's a real turn-off.

I want money!

see?

Next, so, how will those twenty files be called? Is there some dynamic link? What are the variables? What are the target URL's? What do you have generated in the pages, what does the user see? etc., etc.

I'm 100% certain that a) what you want to do is really simple, if only we knew what it was, and b) the answer is most likely already here.

As for the "nare" thing, if "nare" will be a variable, simply make that rule..
rewriterule ^(.*)/? index.php [nc,l]
done!

But, don't you want the "nare" to appear anywhere in the target URL???
Perhaps index.php?raboof=nare, no? Something like that?

I'm not a mind reader, at least, not unless you are sitting in the same room, which you are not.

;o)


DavidR - 25.04.06 7:13 pm

When I asked if it could be done quickly, what I meant was the equivelent of "could you [if it won't take you too much time] tell me..." as in, I didn't want to impose on you but figured it would probably take you two seconds. My apologies if you interpreted it otherwise.

Could you please explain to me why this doesn't work?

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www\.davidr\.(com|net|org)/(.*)$
rewriterule ^(.*)$ http://davidr.com/$2 [r=301,nc]




corz - 25.04.06 10:42 pm

With ye, David! Well, you're right, it only took me a couple of microseconds to work out your solution. On the other hand, explaining everything that's wrong with your latest ruleset is gonna take ages!   smiley for :lol:

For a start, it (the rewritecond) demands that the URI have "www" in it, so any http://davidr.com, http://davidr.org, and http://davidr.net links will not be caught. Then it goes on to to be specific about the rest of the URL, that it must have a forward slash after the TLD, so hits on plain http://www.davidr.com, net, and org etc are now also lost to the rule.

The rewriterule itself creates a captured "$1", but you then go on use "$2" in the target, which doesn't exist. In short, it's a mess! This is all you need..
rewritecond %{http_host} !^davidr.com [nc]
rewriterule ^(.*)$ http://davidr.com/$1 [r=301,nc]
(assuming mod_rewrite is enabled, etc). The condition catches absolutely every URI that isn't http://davidr.com*something* and redirects it to http://davidr.com*whatever*. Requests to http://davidr.com*etc.* pass clean through.

So long as your DNS is setup, and those other TLD's actually point to your site, it should all work swell.

;o)


Dennis - 25.04.06 10:56 pm

my sincere apologies, I just hate it when ppl get names wrong, it was not intentional, yes my email was sent before my post, all is fine and thanks again.


corz - 25.04.06 11:35 pm

No worries, Dennis, it doesn't really bother me much, but I enjoy dropping that in every now and again! smiley for :lol: Really, I am my org, and vice versa, so it's a kinda compliment, really. smiley for :)

All the best, dude!

;o)z


Lou - 26.04.06 3:08 am

Wow!!! Great Stuff! I'm gonna try a few things in here to see if I can fix a problem.

Trying to rewite a vhosted directory on an SSL site to another directory on an internal (non-SSL) server. Am having lots of problems with either Access Denied (Follow Symlinks?). I've done a similar thing for non-SSL pages for the rest of the site with Reverse Proxying - no prob!

But can't get it to work with SSL -> non-SSL Proxying or Redirecting.

Thanks for the great site! Lou


Paresh Parihar - 26.04.06 7:20 am

I am getting Internal server error if i uncomment the followsymlinks line.

# Options +FollowSymLinks
RewriteEngine On
RewriteBase /usr/local/httpd/htdocs/1247/1204/beta/

Any reason why this is happening. If i keep it commented the site works fine.



stu - 26.04.06 12:19 pm

"stu, try taking that extra "/" off the end of your rule, that should fix it. smiley for smiley for ;) By the way, the rest of the rule sucks, too. Where DID you get that thing!? This is enough..

RewriteRule ^index.php$ http://example.com/ [nc,r]"

That gives me an "Couldn't redirect properly" error with Firefox.

And removing that http://example.com/$1/ last slash like /$1 gives same error.


corz - 26.04.06 2:39 pm

Well, stu, if two perfectly good rules throw your web server into a spin, clearly the trouble is elsewhere. Really, mod_rewrite doesn't get any more basic than this! Check the rest of your .htaccess. If that's fine, mail your web hosting company and ask why even simple redirects don't work.

Paresh Parihar, then leave it commented! Most likely your server configuration is set higher up (in .httpd.conf, by the admins), and certain overrides aren't allowed. Or something else!

With mod_rewrite, as with many other things, when it works, you stop tweaking!

Lou, best of luck with that! I've not played with redirecting between ssl and non-ssl, so anything you come up with, tricks or whatever, will be useful, for sure. If anything above inspires a solution, all the better.

But if you do find yourself banging your head against a wall, bang it against mine, instead. Most things are doable, we could probably figure it out.

;o)


stu - 26.04.06 5:21 pm

My full .htaccess file is:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www\.domain\.com [nc]
rewriterule ^(.*)$ http://example.com/$1 [r=301,nc]
RewriteRule ^index.php$ http://example.com/ [nc,r]

With the last line comes this error: "The page isn't redirecting properly - Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

I'll contact my host if you confirm that it's not my code that is causes this.


corz - 26.04.06 10:59 pm

stu, smiley for :lol: No wonder Firefox is giving you an error!

That's what we call an "infinite loop". Anyway, you don't need the last line, just delete it.

If you really must have index.php redirected back to the plain TDL , then first, realize that you are doing two separate things here; the first ruleset removes the "www", the second redirects the "index.php" hits back to the plain TLD. They aren't connected, so I recommend you put a couple of linebreaks between these two sections!

To prevent browsers entering a loop, simply add rewritecond line for this second ruleset, like this..
RewriteCond %{REQUEST_URI} ^index.php [nc]
RewriteRule ^index.php$ http://example.com/ [nc,r]
TADA!

;o)


DavidR - 27.04.06 3:28 am

Thanks for everything, you've been awesome.

I just have one concern - won't the following code (written by you, except I escaped one period you missed) break calls to other, valid subsomains of davidr.com ? Like won't it take blog.davidr.com and make it read just davidr.com?

# corz
RewriteCond %{HTTP_HOST} !^davidr\.com [NC]
RewriteRule ^(.*)$ http://davidr.com/$1 [R=301,NC]

I guess my full original request should've asked to redirect *.davidr.(com|net|org) to either davidr.com if the asterick is www, or else to *.davidr.com if the asterick stands for something other than www. I don't even know if this kind of complication is possible in regexp w/out multiple lines.



corz - 27.04.06 6:34 pm

Ahhhhhhhhh! I see!

Yes, Davidr, it would. Like I said, that one will redirect absolutely everything that isn't exactly "davidr.com" (the dot doesn't need to be escaped, "." == "any character", so I often leave them unescaped in places where it could only be a dot).

There's always more than one way to skin a cat, but if you want to use subdomains, you would probably be best to break this up into two section - as I was just explaining to Stu. First the "www" thing..
RewriteCond %{http_host} ^www [nc]
RewriteRule ^(.*)$ http://davidr/$1 [r=301,nc]
That's probably the best way to deal with that, seeing as you have so many domains. Your subdomains won't get caught with that, but any request beginning www will be sent to davidr.com. Then we deal with the other domains..
RewriteCond %{http_host} ^(.*)davidr.(org|net) [nc]
RewriteRule ^(.*)$ http://%1davidr/$1 [r=301,nc]
These are just off the top of my head, but should work. Yes, it's possible to bundle both sections together, but why create confusion? This is easier to understand, and importantly, understand six months later!

have fun!

;o)


manny - 28.04.06 1:11 pm

Hello. Can you help me make my urls that look like this:
http://www.flookie.net/index.php?c=7&s=11

look something like this:
http://www.flookie.net/personal/page1

and for categories with 2 or more words:
http://www.flookie.net/fabric-hobbies/

??




corz - 28.04.06 6:07 pm

manny, I already have! see above!

If your rules aren't working, show us the code, and I could help to fix them. If you'd like me to just do it for you, simply deposit £100 in my PayPal account!

;o)


Matt - 29.04.06 10:16 am

Can you help me with a sub-domain rewrite?

This is my current .htaccess file

Options +FollowSymlinks

RewriteEngine on

#Redirect http://*.example.com to http://www.example.com/sub-domains/*
rewritecond %{http_host} !^www\.domain\.com
rewritecond %{http_host} !^domain\.com [nc]
rewriterule ^(.*)$ http://www.example.com/sub-domains/$1 [r=301,nc]

This works fine, but what I'd like to do is keep the sub-domain showing in the browser's address bar, i.e.

http://sub.example.com/somepage.php
rather than

http://www.example.com/sub-domains/sub/somepage.php

I think the rewrite rule should be

rewriterule ^(.*)$ http://$1.example.com/sub-domains/$1 [r=301,nc]
as I have my DNS set to redirect *.example.com to my domain, but all that happens is I get a page not found error. Could it be that the DNS mapping is via CNAME rather than via A?

I'd go ahead and try this, but the server is down atm and I can't try it directly!

Cheers


Dennis - 29.04.06 1:06 pm

Hi cor,

can you help me create a rule to deny all "connect" requests. In the rule i sent you it does not seem to be working, they get a 403 from the denied list, I would like apache to drop the request without processing request.

thank you


zissakos - 29.04.06 1:58 pm

Hi,

thanks for the great article!

I have a problem that (I think) hasn't been mentioned yet:

Rewriting works fine and the desired page is loaded, but: the images and links in the resulting page are broken. No images are shown and the links dont't work, because the script seems to think it's inside "article/" but of course it is not.

What is wrong?

My .htaccess is:



Options +FollowSymlinks
RewriteEngine on
RewriteRule ^article/([0-9]+)$ showarticle.php?ID=$1



=> I want people to use
www.greekstuttgart.de/article/333
instead of
www.greekstuttgart.de/showarticle.php?ID=333

Take a look on your own here



corz - 30.04.06 1:28 am

No, Matt, don't do that rule! Bad things will happen. All you need to do is remove the "r-301" from your flags. TADA!

Dennis, I don't know what you mean by the "deny list", but the "- [f]" combination is supposed to give them a 403, that's as good as it gets; unless you want to direct them to yahoo.com or something. You can't tell apache to just "do nothing", as far as I know. It lives to serve!

Of course, if you have a big ole 403 page with fancy graphics and oodles of text, well, that kinda defeats the purpose, doesn't it. Ensure you setup a very basic 403, a single word perhaps "bye!"..
ErrorDocument 403 "bye!
Yes, the single double-quote is intentional. The absolute least you can do is..
ErrorDocument 403 "
So they get nothing a blank page. The connection wasn't "dropped", as such, but it's near-as-dammit.

Anyway, clearly you are becoming completely obsessed by all this hot-linking stuff; why not just get a bigger hosting package? smiley for :lol:

By the way, my own new hot-link protection method is working extremely well1, there are "corz.org" adverts all over the place these days! The great thing about hot-linked images is that generally the webmaster (the guy who actually nicked it) thinks everything is fine and dandy, because he is seeing the image from his cache (cache being the bane of .htaccess tweakers everywhere).

Sometimes it can take weeks for him to realize that he has been advertising the org, and their evil hot-linking plan has been foiled, that's when it hasn't scrolled off the page already. My current favourite corz.org hot-link site has two big corz.org adverts in their forum posting form, right there amongst the smilies. You just can't pay for that kind of coverage! smiley for :lol:

zissakos, erm, gimme a sec, I know this one!

This is nothing to do with .htaccess, well, not much. I believe we did cover this, but it would be a real scroll to find. Essentially, it's the links themselves.

Note how your main logo works; if you look at the link you will see that it is "absolutely" linked, that is, it starts "/", and tells the browser that the image location is relative to site ROOT. The other links will be linked "relatively" (I haven't actually looked at the source, but they will be), ie, they will *not* begin /, but instead be ../yadda/img/some.gif or img/some.gif or whatever.

Because you are doing an invisible (background) rewrite (no "r" flag) the browser really believes that it is inside /article/ so when it sees the relative image link (say, img/some.gif), it looks for /article/img/some.gif

Does that make sense?
Basically, you have two choices..

1) use the "r" flag, like this.. [r] probably a bad idea in your case, as you are trying to NOT show the *real* path.

2) re-link your images.

Option 2 is the best bet, but may mean no small amount of work. If the links are generated, there may be a way to do it very simply. As I mentioned above, my own CMS can switch back and forth with a single checkbox, but that sort of stuff needs to be coded into the back-end.

If they are not generated, then either, a) forget the cute new rewrite altogether, or b) get cracking in your text editor!

for now..

;o)

references:
an image
1. come to think of it, it was probably on page one I mentioned this, but here's how it works. Instead of a 403, I send them a cute image that simply reads "corz.org".. So, whenever someone hot-links, they do free advertising for me! hot-link away! As mentioned above, because of the "cache effect", these adverts usually slip right past the webmaster doing the hot-linking, sometimes permanently.



Dennis - 30.04.06 3:05 am

Thanks cor,

what I meant was I am seeing this in my log:
210.51.162.253 - - 28/Apr/2006:17:59:37 -0400 "CONNECT 210.51.162.99:25 HTTP/1.1" 403 202

the 403 is fine but I dont know if the 403 is coming from my "deny from" or from:

RewriteCond % REQUEST_METHOD ^CONNECT
RewriteRule .*$ - F

I thought the RewriteCond would just drop all "connect" requests without processing and providing a 403.


Dennis - 30.04.06 3:22 am

cor, sorry for a double post but wanted to add this from the log, its kind of like being taunted,

67.153.2.194 - - 29/Apr/2006:17:10:44 -0400 "GET /w00tw00t.at.ISC.SANS.DFindsmiley for :) HTTP/1.1" 400 226,

now dont get me wrong I don't care if they get 400 errors, but it seems I've been targeted it began with hotlinking, but since I solved that problem, they have started DOS attacks, and the "connect requests" from anonymous proxies is my new problem of the week. I had a 15MB log file of connect requests, from different ip's changing every 3 seconds. 80% of the ips originated from China and 20% from the US. They brought apache to a crawl and was hoping first: is my connect rule correct?, and second: is there a better one for my dilemma?

Thank-you


corz - 01.05.06 6:39 am

Hmm. If it were me I'd redirect all CONNECT requests to a php handler. The php handler then takes the IP address and automatically adds it to the list of denied IP's. If I were feeling fruity, I'd setup a database (flat-file, of course) to have them drop off again after a week or so.

Apache can't just not process, that goes against its nature. Only by processing can it know how to process the request, or not to. The least it can do is a blank 403.

The answer to your first question is whichever comes first. Both produce exactly the same result, a 403. If you use the 403 I put in my last post (a totally blank response) these requests aren't really a problem, they take almost no time to process, and it would take zillions of such hits to pose a serious DOS attack.

That just leaves the logging. Why not flag those requests with some sort of environment variable, and then add that to your logging directive (really, this is stuff for page one)..

SetEnvIf %{REQUEST_METHOD} CONNECT go-away

And then in your logging directive..

CustomLog logs/access_log env=!go-away

Of course, you need access to the httpd.conf/virtual host setup for logging directives, so you might have to ask your admins to do this. If that's not possible, tell your log viewing software to ignore it, if possible (analog, whatever). Or if you just want to save the bandwidth and time in downloading the logs, maybe run some sed script on them first.

But at the end of the day, if all you actually send to the clients is nothing at all - a blank page..
ErrorDocument 403 "
it shouldn't become a major issue. Annoying, yes, like the many-MegaBytes-a-day of almost-spam that doesn't fill the corz.org comment pages, thankfully, but does waste a tremendous amount of bandwidth; life is too short to worry about these things.

Basically, unless you have access to the ipchains (erm, firewall) settings on the server or its gateway, sending a blank page is the best (read: least) you can do.

for now..

;o)

ps.. have you considered <LimitExcept>?


corz - 01.05.06 6:46 am

Of course, you may wish to pass these requests to a php handler, one designed to DOS-the-fuck out of whoever sent the request, or worse.

I'm not saying that's smart, or clever, or condoning this cource of action in any way; it would likely get you booted from your web host, for starters, possibly thrown in jail, who knows. Do spammers prosecute? smiley for :lol:

But on a home server, with a sympathetic ISP, it might be fun. smiley for :evil:

Of course, if it's a home server, your black ice isn't restricted to php. smiley for ;)

;o)


Dennis - 01.05.06 12:01 pm

Understood, thanks again!


Jan - 01.05.06 11:17 pm

Does the .htaccess file have any influence on the way a bot sees a robots.txt file?

It seems like googlebot can't reach my robots.txt, it keeps getting a 5xx error.

My .htaccess file looks like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|gif|png|css|js|pl|txt|xml)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php

I have no idea what this do, but is it making trouble??


corz - 02.05.06 12:16 am

Jan, remove "|txt" from the RewriteCond, and google will once again be able to access the robots.txt file.

In fact, it's probably best to remove the whole lot (comment it out, by putting "#" at the start of each line) and get a real 404 page!

l*rz..

;o)


DragonFlyEye - 02.05.06 3:57 am

Hey cor, no posts in a while, not sure if you will even find me, but this tutorial is a big help! One thing, though: I am wondering if there are any particular security risks in rewriting URLs from static to non-static extensions? Meaning a URL that looks like http://somewhere.com/index.htm being rewritten as http://somewhere.com/index.php. I've seen other sites that say there is, such as this one:

http://www.thescripts.com/forum/thread12193.html

Hoping for an opinion on this before I go ahead and do it. Blogging software rewrites URLs as a regular function, but they don't usually go from .htm to .php, so I'm leery about doing this.

Thanks again for the great tutorial!


corz - 02.05.06 5:09 am

Hey DragonFlyEye, that's not the same thing!

That thread is not about rewriting URL's it's about fooling Apache into parsing html files as php code (more .htaccess page one stuff), aka. forcing a new "handler", and yes, there are certain vulnerabilities in doing this. I don't do it much at all, not because of vulnerabilities, it's usually just messy, or lazy.

Here at the org, .blog files are parsed as php; in case anyone were to load them directly, they would see HTML formatting as opposed to the plain text. There's nothing insecure about that. The trouble with setting the .htm or .html extensions to be parsed with php is that web applications often produce files with these extensions, and if their content isn't known, parsing those files is risky.

There isn't a risk parsing .htm or .html as php so long as it's only done in specific folders containing known, non-user files. ie. not globally, which would be silly. By non-user files I mean files where guests couldn't possibly put arbitrary data. Request logs are a prime example.

On this page, we generally do the opposite, that is, redirect away from the .htm extension to a real document ending .php, so, if someone requests a file called page.htm, even if it exists, they are redirected to page.php.

In most areas of the org, it's simply not possible to load a file with an .htm or .html extension, though there are hundreds. The data for this very page is in a file called "htaccess2.htm", but you can't access it directly, it is "included".

Two different thing, see!
Hopefully this helps you make a good choice.

;o)


DragonFlyEye - 02.05.06 12:50 pm

Ah! I see! That's why I asked, so I could get good advice. Thanks so much!


DragonFlyEye - 03.05.06 3:45 am

Doh!

Perhaps I'm just missing something here, but my .htaccess file is giving me a 500 error. Mind you, I already have an .htaccess file on the root of the server that looks like this:

# BEGIN DragonFlyEye

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [l]
</IfModule>

# END DragonFlyEye

There is a subdirectory called "vblog," and I would like to push any requests for, say, "/vblog/thevideo.htm" to "/vblog/vblog.php?yt=thevideo" and this is where I'm getting my 500 error. The .htaccess file for the /vblog directory is as follows:
Options +FollowSymlinks
RewriteEngine on
RewriteRule (.+).htm vblog.php?yt=$1 [nc]
Grrr! What am I doing wrong? Thanks again for the help!smiley for :cool:


corz - 03.05.06 8:05 pm

The top-level rule is a bit OTT, but it's your server. Plenty spare CPU and memory, I hope!

And the next rule looks fine. There's a slim chance that Options +FollowSymlinks is bringing up the 500 error, on rare servers, overrides have been disabled, and it can cause errors, very rare, though.

Try removing it.

;o)


John - 05.05.06 9:06 pm

The code you have for lose the WWW is not compatible with most of the code generated by password protection scripts.

IE: If you put
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www.corz.org [nc]
rewriterule ^(.*)$ https://corz.org/$1 [r=301,nc]

in an .htaccess file in the public_html directory and below that directory you have a password protected directory called blabla with the following code in a .htaccess file
AuthType Basic
AuthName "Restricted Area"
AuthUserFile "/home/corz/.htpasswds/somewhere/passwd"
require valid-user

and you navigate your browser to https://corz.org/blabla

everything works as expected, but if you navigate your browser to http://www.corz.org/blabla

then you end up with a 404 page instead of being redirected to https://corz.org/blabla as expected.

Can anything be done to make this compatible with .htaccess password protected directories?

-- John


corz - 05.05.06 11:56 pm

In response to your first statement: neither is the twenty-first century, then. Nor mod_rewrite! And the phrase "code generated by password protection scripts" just makes me shudder. smiley for :eek:

I must assume you are using "basic auth", and so I must strongly recommend: DON'T! There are so many better ways to authenticate users these days; throwing the password over the wire in plain text hundreds of times a minute isn't very clever. We demonstrate the technique, as a learning example, but no one sensible actually authenticates with it!

And the "lose the www" technique is meant to catch the odd stray request from new (read: unaware) users. It's certainly not designed for requests from users familiar enough with your site to need to login!

Having said all that, if, for some crazy reason you are locked into an antique server; maybe an ageing boss, or something; and your error documents are kept in /err/ and you put..
rewritecond %{request_uri} !^err
before the other RewriteCond lines, you will get your login dialog. Once authenticated, the rewrite rule will guide them to the www-less version.

As will every other node between user and server, in plain text, over and over again.

In short; TADA!

;o)


John - 06.05.06 4:41 am

First I mistakenly said a 404 message it's actually a 401 message.

I'm not sure what you consider an antique server but this is a shared server running red hat 9 and cpanel.

I asked my host about the error logs and they replied.

<pre>
The error logs for the domains are at root: /usr/local/apache/logs

You can view errors for your domain by pulling them up through cpanel, but they aren't kept locally in your web space in a directory such as "err"
</pre>

So I asked if they could make a symbolic link from /home/myAccount/err to the relevant error log in /usr/local/apache/logs

their reply was
<pre>
The error log is for all domains on the server. They aren't separate logs per domain.
</pre>

Am I hosed or is their hope?

BTW I love your little penguin with the apache feather.


corz - 06.05.06 12:16 pm

John, I mistakenly forgot to mention that I knew you meant a 401 page. smiley for ;)

That's not an antique, so you have no excuse, get a better authentication system, make it a priority!

And who said anything about logs??? smiley for :eek:

I meant YOUR ERROR DOCUMENTS! aka "error pages", eg. "401.php", "404.html", or whatever. Any user can set those in .htaccess (see page one of these articles, and my musing about the empty 403 page, above), this has nothing to do with your logs, though while I'm here I have to say I'd run a mile, and fast, from any host who didn't give me access to my own logs1. How are you supposed to ... (insert a dozen essential webmaster tasks) ? smiley for :eek:

So, setup custom error handlers, 401, 403, 404, etc..

ErrorDocument 401 /blabla/401.php

And at the start of your rules, put..

rewritecond %{request_uri} !^blabla

And you're done.

;o)

ps.. there's a few versions of the penguin server image in the archives, here.. /public/images/powered by penguins/, photoshop document, that sort of thing. Feel free to use it for your own Linux promotional needs!

references:
They just mean that the logs are at first created in one big directory. If it's CPanel, you probably do have access to your own logs; check out the "Raw Access Logs" option.



John - 07.05.06 6:35 am

Thanks for the response cor.
I misunderstood part of your previous response but think I understand better now and will delve into it more tomorrow.

Do you have any suggestions for better authentication system?

I also thank you for the offer to use the graphic.

I and a group of 3 others are working together to bring an older, but at one time very good bbs back to prominence.
It's a PERL script that runs on both nix and doze. I myself will have absolutely nothing to do with setting it up on a doze server though. Too hard to communicate with doze admins (who all know everything or think they do) on how to get the right permissions set on directories.

Obviously your graphic could only support Linux servers though. Just wanted to make that clear before using the graphic and hope the offer is still open.
An alternative idea I got (though graphics are not my forte) was to get someone to do a similar penguin wearing the apache feather in an indian headband.

-- John


corz - 07.05.06 2:56 pm

For authentication, if very much depends on the web-side code you are using. I use php, so I cooked up something for that, but perl is outside my ken, so I have no idea what's available.

If it's a bbs, doesn't it come with its own admin structure?? Surely!

If not, for a perl coder, it shouldn't be too hard to knock up something. A user database, a "session" to work with, coupled with pajamas-like client-side hashing, bingo! Of course, there's always SSL, and "https://...", if you can afford a certificate, or don't mind "dodgy certificate warning" at your logins.

For "root" access, to change files, directories, etc., you want to use ssh, sftp, scp, etc. Though if it's running on a windoze server, most of these things are not available without first being separately installed. Does it have to run on a Windoze box? Why not just keep the servers strictly *nix?

As for the graphic, really, I retain no hold over the work, do what you like with it! It would be silly to use it on a windows-served site, though, agreed.

for now..

;o)

ps.. I make icons and "wee graphics" as a form of occasional meditation (how all the graphics here at the org got made), if you like, I can have a crack at the headband thing some time; it's a nice idea. I wish I'd thought of it! smiley for :ken:


John - 07.05.06 3:39 pm

Up until recently the BBS was shareware. The original author no longer has the time to update and support it.
People who have purchased it in the past have it installed and running on both nix and doze servers.

If you'd like to take a shot at the headband graphic that would be great, but I must say that at this time our little group has no funds to pay anyone with.

-- John


corz - 07.05.06 4:32 pm

I seeee!
So you will be continuing the development, excellent!

If you are distributing this thing, folk will start asking for a serious authentication fairly soon; "basic" auth and its counterparts simply don't cut it these days. You might want to take a look at pajamas (link from last post), with perl's string handling, it wouldn't be too tricky to create something like that. Tie it into your user database and you're done (yeah, like it'll be that easy! hahah)

As for the graphic, definitely no charge either way. If I made it, I'd be putting it up here for all penguin-lovers to use, us included!

Good luck with the perl!1

;o)

references:
I don't believe in luck, but when I look at perl scripts, I just know that it's a factor!



adil_rauf - 09.05.06 6:46 am

hello.

i have a problem in which i have to make subdomains using just folder creation method using php and then whenever the user enters the foldername as subdomain ie http://test.abc.com it gets to folder named 'test' and shows further traversal of site with pages like aboutus.php etc as http://test.abc.com/aboutus.php etc

please reply i am in need thanks i advance


Frank - 09.05.06 9:52 am

Thanks! Never understood much of rewriting. Now I think I begin to grasp the basics (read many tutorials, but only yours makes any sense to me). I'll be trying my own stuff soon, now that I understand it.

Thanks! I think I'll check out your site for more useful information smiley for :D


NiteOwl - 09.05.06 7:15 pm

Nice article
Being a newbie with Apache and just learning PHP I have a dilema that I am hoping to find a better direction for and the info presented here might point a direction but not the specifics.

I want to redirect all requests to sub folders of a web site so that I can process them through my own template page.
For instance a request to mypage/subfolder/somefile.htm should get redirected to mypage/index.php
The index.php page is used to load the requested page within a templated design keeping the site consistent.
Content pages are created by various individuals at a local middle school so I cannot rely on them following special formatting rules for the page and it's links.

I setup a .htaccess file to deny access to all sub folders then trapped the 403 error and executed error.php which evaluates the requested URL and then redirects to index.php?requestedpage.htm

This works except for one thing. Each sub folder has it's own Images folder and though the primary page loads the images cannot load because access to them is stopped by the htaccess file.

I could use LOTS of htaccess files to start/stop denying access but it seems kludgy at best and the content providers may create new folders on their own which would never be able to get access without a local htaccess file to allow it again.

Is there a better way to do this? It seems like rewriterule might do it but I do not understand it well enough. All the examples deal with specifying a specific filename and in my case I want to just saying any request below this path needs to be redirected and I need to get the requested path so I can parse it out to determine where it should point.

Thoughts?
Thanks.


Koki - 10.05.06 2:42 am

Hello Cor,

I really need some help here please. I keep reading but it seems the rewrite rule I need is just too hard for me to figure it out by my self.

What I have as code on my sites is this:

www.myexample.com/t/out.php?url=http://example.com/foo/bar/index.html&link=text&s=60&f=1

and because is a big, ugly SE unfriendly URL I would like to change it a bit..like this:

www.myexample.com/40540/ ( 40540 is unique ID, all the links have one) or
www.myexample.com/link/40540/ or something like that.

Now let me explain this:
The out.php is part of a counting/trade script so all the links have to pass through it.
The variable "url: can be like this ( examples:
url=http://domainA.com/foo/bar/
url=http://domainB.com/foo-r/bar-s/car.html
url=http://domainC.com/foo-r/bar-s/dars/car.html
and this that the urls are always different makes it difficult for me to figure it out how to rewrite.
The variable "link=text" can be changed to "link=40540" that will be the unique ID for that particular link if this is from some use to making the rewrite rule....what else.....ah yes, I am on 1.3.34 Apache core.
Your help is greatly appreciated!

Koki


corz - 14.05.06 10:26 am

heh, it's always guaranteed that if I'm away for a few days, I come back to a nice wee pile of .htaccess "challenges" smiley for :cool:

adil_rauf, yours is the biggest challenge, because I haven't a clue what your issue is. After many reads, it seems as if you want me to code some php for you. smiley for :eek: If that's true, feel free to get in touch by the usual methods!

Cheers, Frank! That's the main plan! As time permits, I sprinkle my magic learning dust on many interesting subjects, because quite frankly, I know everything!1 smiley for :lol: Check back regularly!

NiteOwl, I agree, planting .htaccess files all over the place isn't the way to deal with this. Certainly not when content comes from these sources. You probably want to deal with this one time, in the root, essentially capturing everything, and passing it to one Master php handler. This simple rule..
RewriteRule ^(.*)$ /index.php?mypath=$1 [l,qsa]
will pass ALL requests to a php handler in the root of the site (index.php, though it could be any file, anywhere), and passes the entire query string to the handler as a variable "mypath" (I would avoid using "path" on its own, being a regularly "reserved" word). An other variables in the original request are also passed through ([qsa])

Once inside php, your options are wide open, and I'd definitely recommend handling these complex path manipulations inside php rather than .htaccess; as well as the considerable performance advantages, php has superior string handling capabilities, so you can easily manipulate links, image locations, etc, before spitting out the page. There was another good reason that I've forgotten since reading your post! smiley for :blank:

Note, there is no "r" in the flags, so it's a completely background rewrite. The user's original URL is still in the title bar, so you will want to decide what that will be before you begin. As it's completely fictional, it can be as "cool" as you like.

I really must get a beta of the new corzblog code up asap (I had to postpone development of the latest version at around 80% completion, but it works brilliantly) if only to demonstrate how I do the permalinks. One simple rewrite rule passing all the necessary information to php, and php doing clever stuff thereafter. With the right code, you can create a truly flexible system thats easy to maintain (always a level-one priority!) and easy to expand; I can switch from regular to "short" links with no more than the click of a checkbox.

Koki, first, understand that long, variable-filled links are NOT search engine or SEO unfriendly2, but www.myexample.com/40540/ most definitely IS!

The second thing you have to realise is that these are OUTBOUND links, so they have no impact on your SEO, only that of the sites the links point TO. The link text can be whatever you like, the visible part, and you could perhaps work on that, but it makes no difference what the actual links are.

Essentially, this is a waste of your time and effort, and I'd recommed you forget it, go do something productive instead, like fix your page titles, or whatever.

Okay, I admit, it could affect your SEO, but probably not to anything like the degree you imagine. If you really want to make any impact at all with these outward links, you need rewrite the links not to codes and digits, but to long descriptive plain english titles. You could even use variables to HELP your SEO efforts. This link..

out.php?link=http://fastcars.com/&vars=regualar-vars-here&section=speed&sub=motors&engines=power" is actually quite a good outward link, SEO-wise. So long as the linl pointed to a relevant site with reasonable PR, etc.

But the links all being different makes this a huge task, with very little bang for your bucks, so I say again, FORGET IT! You could impact your SEO efforts in infinitely better ways for *much* less effort.

I'm serious about the page titles.

;o)

references:
  1. Well, between me and my brother, we know everything smiley for :ken:
  2. that automatic router scripting thing I put up last week is already top of google over 6 million other pages for a variety of search queries, so I ken fit I'm talking about!



Koki - 15.05.06 7:13 am

Thansk for the reply Cor. I thought that this is gonna be some more complex task. Actually the idea behind this rewrite was to make all those links to appear as content (subpages) that makes part of my site. As they say "... content is king....."
Anyway, thanks again,
Koki


corz - 15.05.06 9:39 am

Koki, yes, content is king!

And when the user, google, whatever, clicks the link, it takes them off-site, to someone else's content.

So, no way you can fool google, or anyone into thinking those pages are part of your site (and therefore bestow PR on you) unless you use some kind of handler to grab the page, parse it, and represent as an actual page on your site.

And then it's no longer an outward link, but a rip-off!

I recommend, spend the time making some unique content of your own. One paragraph of good, unique text, is better than a dozen pages nicked from elsewhere, PR-wise.

Google sees all!

;o)


NiteOwl - 15.05.06 10:25 pm

Thanks for the info, I will give it a try.
A universal redirect is kind of what I was hoping for up front but others pointed me in other directions.

I have tried a variety of rules and can manage to pull back the originally requested path and any querystring parameters but I cannot retrieve anchor tags.

This is my latest attempt:

RewriteRule \.(html|htm|php|php4|php5)$ /jfk/urltest.php?&url=%{REQUEST_URI}&%{QUERY_STRING} [R,NE]


I have searched around for info on retrieving anchor tags once intercepted via an htaccess file but nothing I have read has been encouraging. The only reason I have not given up is that none of the people saying it could not be done seemed to truly know for certain.

Also, when I use a RewriteRule like above, the anchor tag get's passed along so that when the urltest.php page loads the address line still includes the anchor (in Firefox but not IE) though the url has been drastically altered which makes me think the text must have reached the server at some point so there should be a way to retrieve it.

I will play around with the rule above and see what I can get it to do. I am newb with Apache so...

As for the end-result URL, I just need to keep two parameters in there when finished as flags for the navigation system. I will be handling some of it server-side but want to keep bookmarkable addresses without a huge amount of translation and the variables tell the top and mid navigation controls how to set themselves and which folder group to work out of.
Learning the ins/outs of Apache and PHP at the same time is really a hard way to go about an already complex project. smiley for :)
Thanks.



justmakeitup - 16.05.06 6:48 am

Hey,

I have a weird problem.

I have been doing some mod-rewriting to create subdomains on the fly. In essence my code works. I had the hosting provider enable wildards and I added some .htaccess code.

Now here is what is happening...

The code does create a virtual subdomain
RewriteEngine On
RewriteRule ^.htaccess$ - [F]
RewriteCond %{HTTP_HOST} !^www.myexample.com?$
RewriteCond %{HTTP_HOST} ^([^.]+).myexample.com?$
RewriteRule ^$ /%1/$1 [L]

I know I miss REQUEST_URI in thios code but no matter what I did something didn't work.

What I get once there is a subdomain is the following.

www.myexample.com/subfolder

turns into subfolder.myexample.com

and this is fine...

BUT when I click on a link within the page it is supposed to take me to:

subfolder.myexample.com/somefile.html 


however, it does not. Then out of curiosity I tried:

subfolder.myexample.com/subfolder/somefile.html
and

naturaly it worked... :S

I had a fellow programer who is much more savy in htaccess programming look at it and he said that my version of apache doesn't get the backtrail to accomplish what i want.

He came up with this:
RewriteEngine On 

RewriteRule ^.htaccess$ - [F] 

RewriteCond %{HTTP_HOST} !^(www.)?DOMAIN$ [NC] 
RewriteCond %{HTTP_HOST} ^([^.]+).DOMAIN$ [NC] 
RewriteCond %1---%{REQUEST_URI} !^(.+)---/1(/.*)?$ 
RewriteRule .* /%1%{REQUEST_URI} [QSA,L]

But no luck.

How could I accomplish having links on my subdomain actualy stay in the sudomain?


This on the other hand
RewriteEngine On
RewriteRule ^.htaccess$ - [F]
RewriteCond %{HTTP_HOST} !^www.myexample.com?$
RewriteCond %{HTTP_HOST} ^([^.]+).myexample.com?$
RewriteRule ^$ /%1 [L]

works,, but the absence of the / in the end of the rewriterule only allows for pointing links to the subfolder on the main domain :(

Help ???


Neil - 18.05.06 11:18 pm

Well, I'm a beginner at this and was feeling very pleased that I'd managed to set up a .htaccess file to direct mobile users to a separate version of my home page "mindex.xhtml" instead of "index.html".


RewriteEngine on
DirectoryIndex index.html
RewriteCond %{HTTP_USER_AGENT} "Windows CE" [nc,or]
RewriteCond %{HTTP_USER_AGENT} "Palm" [nc,or]
RewriteCond %{HTTP_USER_AGENT} "Nokia" [nc,or]
RewriteCond %{HTTP_USER_AGENT} "BlackBerry" [nc,or]
RewriteCond %{HTTP_USER_AGENT} "MOT" [nc,or]
RewriteCond %{HTTP_USER_AGENT} "LGE" [nc,or]
RewriteCond %{HTTP_USER_AGENT} "SIE" [nc,or]
RewriteCond %{HTTP_USER_AGENT} "Sony" [nc,or]
RewriteCond %{HTTP_USER_AGENT} "SAMSUNG" [nc,or]
RewriteRule ^index\.html /mindex.xhtml [r,l]
ErrorDocument 404 /404.shtml


It works OK when going to the root of the site firefox and IE go to "index.html" and my phones go to "mindex.xhtml", but if I follow a link from any of my pages with any browser to "index.html" I'm sent to "mindex.xhtml"smiley for :erm:
Obviously for some reason the RewriteRule is working without the RewriteCond operating, but why?

Any ideas?

Thanks


Juergen - 22.05.06 9:25 pm

Hi,

I just want to do replace a .gif file with a php script.

rewriterule ^test\.gif$ http://www.testserver.com/test.php

This works fine, but I want to avoid that test.php is shown in the adress bar instead of test.gif. Any ideas ?


corz - 25.05.06 11:27 am

FUX!

Okay, like fourth try at getting up-to-date here smiley for :aargh: Gonna go for a short version. smiley for :roll:

NiteOwl, good luck with that! (though I don't believe in luck, you get my drift). It's good to get those Apache skills while you can, and even better if you can do it on someone else's expense account!

If you get stuck with any more specifics, jump back here (though don't expect a same-day, I'm snowed under!)

justmakeitup, managing access to *real* subdomains with .htaccess is simple and painless. Creating them with .htaccess rarely is. This (essentially your first example, with some minor edits)..
RewriteEngine On
RewriteRule ^.htaccess$ - [f]
RewriteCond %{HTTP_HOST} !^www.example.com
RewriteCond %{HTTP_HOST} ^([^.]+).example.com
RewriteRule ^$ /%1 [l]
should work okay. Pay careful attention to the bits I removed.

Neil, try removing the last "or". smiley for ;)

And finally (w00t!).. Juergen, looks like in your real rewrite rule you are using the [r] flag. Answer: don't! If not, definitely get back here!

PUBLISH!

;o)


Neil - 26.05.06 10:37 am

Now that makes sense smiley for :D
I really must start thinking some time.
Thanks very much.


corz - 26.05.06 2:39 pm

No problem Neil, always glad to help, especially folk improving accessibility for mobile devices.

You also might want to consider doing something like this..
RewriteCond %{HTTP_USER_AGENT} SIE|LGE|MOT|BlackBerry|Nokia|Palm|Windows\ CE|SAMSUNG [nc]
which I was gonna set as an "exercise", until I realized that figuring out how to get the space in "Windows CE" would probably just give you a headache. (you could also use "." instead of "\ ", meaning "any character". Using quotes would most likely give an error.)

l*rz..

;o)



Kjetil B Moe - 26.05.06 5:42 pm

Hi, great site! I makes it much easier to understand the htaccess magic. I have a problem here though:

I am currently having a virtual domain at lets say www.mysite.university.com. The site is also available at the address org.university.com/mysite, but the cms-system requires one address and returns errors when trying to use this url. I would like all requests to org.university.com/mysite to be redirected to the mysite.university.com. Apaches redirect will not work here, and I've tried with the following

Options +FollowSymLinks
RewriteEngine On
rewritecond %{http_host} ^org.university.com [nc]
rewriterule ^/mysite/(.*)$ http://www.mysite.university.com/$1 [r=301,nc]


Could you please give me a hint here? I'm stuck.


Kjetil


Bumble Bee Tuna - 30.05.06 8:46 am

We both need help.

The google translator link you demonstrate only works with computers in English. My computer is from the USA and I now live in Germany. Therefore it works great for me, but when I would tell a local to go to my website and they can just hit the German translation link, it never worked. I tested this theory at a dozen different internet cafes. It makes complete sense too for the reason of when you access the Google search engine from your laptop in different country, your laptop still displays results in your native language. But a local internet cafe will have them in their language.

That was your help, now mine.

I'd like to take a website like jobpostings.ws with the projects at the bottom, and not have the search engines crawl the project links at the bottom as I believe it's too many links leaving the page for SEO purposes. But at the same time, I like the page updating regularly with related content so that the search engines constantly come back for updates without needing to specify a return time in meta. And if it's not used for this purpose right now, maybe someone else could find it useful for promoting another website from their indexed one that they don't want search engines indexing but still want their visitors to check it out. How could we do this in .HTACCESS?

I've been referencing your website for several months now and just love it.

- Bumble Bee Tuna!


corz - 30.05.06 10:02 am

Kjetil B Moe , my first hint is remove the "=301" until you get it working! See the notes in the main article about this; it's much more important than most people imagine, I imagine.

Next, lose the "www". I mean drop it altogether. If you want to add it back later, mindfully, cool.

Now that rule. Taking into account all the above, you probably only need to make one small change, that is, remove the "/"..
rewriterule ^mysite/(.*)$ http://mysite.university.com/$1 [r=301,nc]

Bumble Bee Tuna! Glad you finally got around to adding some text on-site!

Yes, the Google links aren't exactly perfect. If I were being kind on myself; something I certainly need to do more; I would say that it was a "proof of concept", and leave it at that! smiley for :lol: Actually, a little bit of tweaking with the links, and they could probably do what you want, if you want something else, that is.

Now to your links..

Firstly, unless you have a *LOT* of links, I mean thousands, it's unlikely to have a negative impact on your SEO efforts. Navigate to /links.php here at the org; that page does have thousands (probably), and Google doesn't mind at all; gives my site a thorough indexing, thank you.

I don't entirely understand the next bit, about someone else using something. Using what? The more I read it, the more confusing it gets! Anyways, unless you specify a return time (and I recommend that you do) then search engines will come back more or less as frequently as you update the page.

Most search engines like pages that are "active", but not too active. Ideally you want some fixed content at the top (I mean the first paragraphs) and then something moving underneath, like this page is, or indeed, jobpostings.ws, except done better.

Frankly, I wouldn't bother. You don't want to start messing with cloaking, or any other dodgy "SEO Techniques", hiding content from google, false redirects and shit; it's just asking for trouble.

Feel free to come back and explain..
And if it's not used for this purpose right now, maybe someone else could find it useful for promoting another website from their indexed one that they don't want search engines indexing but still want their visitors to check it out. How could we do this in .HTACCESS?

l*rz..

;o)


Bumble Bee Tuna - 30.05.06 12:38 pm

Feel free to come back and explain..

And if it's not used for this purpose right now, maybe someone else could find it useful for promoting another website from their indexed one that they don't want search engines indexing but still want their visitors to check it out. How could we do this in .HTACCESS?

What I mean by this is say you take corz.org and you are working on a new page for it. like corz.org/htmltricks

you want your visitors to perhaps give feedback or input as to what you should be adding to it. besides providing text on the referring page saying "enter corz.org/htmltricks" to get a sneak preview. you'd rather have a hyperlink but not have it indexed by robots/crawlers until it's finished - is this possible?

this is what I was inquiring about and I would not think it dodgy unless you are selling PR links from your site and people aren't getting the status they paid for. smiley for ;)

I could be wrong, but I'm pretty sure I've seen websites doing this.


corz - 30.05.06 1:12 pm

Ahhh.. I see what you mean. And it's an interesting idea; I often have pages that are in-progress and up on the live version. Google finds them all (via abovementioned links page) but I don't mind at all; it's fun to watch it climb the ranks. Why would you want to prevent google finding these pages?

As to the "solution", I'd add the page to my robots.txt file, which is designed for doing exactly this sort of thing. For starters, the syntax is a lot simpler than mod_rewrite!

;o)


kai - 01.06.06 1:46 pm

hi cor, I'm back... from 15.02.06 smiley for :)

I now want to reverse my URL re-writes the OTHER way, and change:

http://www.kaitech.hk/forums/index.php?topic=751.0 to
http://www.kaitech.hk/component/option,com_smf/Itemid,71/topic,751.0

-and-

http://www.kaitech.hk/forums/index.php?action=post;topic=751.0 to
http://www.kaitech.hk/component/option,com_smf/Itemid,71/action,post/topic,751.0/num_replies,0

The latter rewrite is a bit challenging. If there is a number other than ".0" at the end, I would want to change:

http://www.kaitech.hk/forums/index.php?action=post;topic=758.1 to
http://www.kaitech.hk/component/option,com_smf/Itemid,71/action,post/topic,758.0/num_replies,1

Are these changes possible with .htaccess?


Sean - 02.06.06 9:45 pm

Hi Cor,

Great site! I'm learning. smiley for :)

I was wondering if I could get your specific optinion to a topic which relates to one of yours you touched upon above. I am re-working a site and am concerned about inboud links and search engines pointing to the old pages once I change them. Instead of having a .htaccess line for each permanent redirect page, I was hoping for a generic solution that will apply to all of them. Here are the 2 format specifics...

The Site is www.PourLeCorps.com

Currently the pages show up like this in the URL address bar as...

www.PourLeCorps.com/index.shtml?welcome
www.PourLeCorps.com/index.shtml?contactus etc.

they will get changed to this format/convention...

www.PourLeCorps.com/welcome.htm
www.PourLeCorps.com/contactus.htm

Is there a generic permanent redirect option I can use in the .htaccess file which will work for them all? Can you help me?

Thanks very much.

Regards,
S



justme - 03.06.06 9:31 pm

I have rewriting to do 'subdomains to folders'. How can I add check that when there is no folder called subdomain it will go to example.com instead of trying to go to folder that doesn't exist?

I'm using example above
RewriteEngine On
RewriteRule ^.htaccess$ - [f]
RewriteCond %{HTTP_HOST} !^www.example.com
RewriteCond %{HTTP_HOST} ^([^.]+).example.com
RewriteRule ^$ /%1/$1 [l]


There should be something like this
RewriteCond ^www\.domain\.com/%1 !-d
but can't figure it out.

tell me someone


corz - 05.06.06 7:51 am

kai, it's not quite reverse, more like the same but different, and it's easier than you think. We already covered how to capture variables. You could do something like..
RewriteCond %{QUERY_STRING} ^topic=(.+)\..$ [nc] 
and to only perform a particular rewrite when the last digit is zero, or not, simply add that to the rewritecond lines, either rolled into the above, or on it own .
RewriteCond %{QUERY_STRING} !.*0$ [nc]
or something like that.

You might just want to capture it, and use it as-is..
RewriteCond %{QUERY_STRING} ^topic=(.+)\.(.)$ [nc]


Sean, yes, same story as above (about 2 inches)..
RewriteCond %{QUERY_STRING} ^(.+)$ [nc]
now whatever was after the "?" in your original query will now be stored in "%1", so you can redirect with that.

It depends on the format of your other site links, but assuming they are all something.shtml like your examples, you could do..
RewriteCond %{QUERY_STRING} ^(.+)$ [nc] 
RewriteRule ^index.shtml /%1.htm [r,nc]


justme, the "-d" checks for the existence of a directory, NOT a URL path, or domain+directory, just the directory, so you only need to test "%1" on its own.

;o)


hjb - 06.06.06 10:01 am

Hi,

I am having problems with a long set of redirects.
The [L] command seems to have no effect is some cases.
.htaccess works from top to bottom yes?
So if a redirect is done with the [L] tag then everything below it should be ignored yes?
Strange because that doesn't seem to work here:

some code..
Options +FollowSymlinks
RewriteEngine on
DirectoryIndex index.php

#log out rule
RewriteRule ^logout/?$ /?logout=1

# redirect all view profile pages
RewriteRule ^profiles/([0-9]+)/? http://%{HTTP_HOST}/profiles/?user=$1 [QSA,L]

#no www!
RewriteCond %{HTTP_HOST} ^www\.example\.(hjb|com)$ [NC]
RewriteRule ^(.*)$ http://example.hjb/$1 [R]

# if no subdomain then do nothing
RewriteCond %{HTTP_HOST} ^example\.(hjb|com) [NC]
RewriteRule .* - [QSA,L]

# if we DO have a subdomain
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.(hjb|com)$ [NC]

# ONE - redirect subdomains (with no other stuff) to ham
RewriteRule ^(index\.php)?$ /ham/?hamurl=%1 [L]

# other redirects
RewriteRule ^addwidget/(index\.php)?$ /ham/addwidget/?hamurl=%1 [L]
RewriteRule ^admin/delete/?$ /ham/admin/?delete=1&hamurl=%1 [L]
RewriteRule ^admin/cars/delete/([0-9]+)/?$ /ham/admin/cars/?delete=1&car_id=$1&hamurl=%1 [L]
RewriteRule ^admin/home/delete/([0-9]+)/?$ /ham/admin/trips/deletehome.php?hamurl=%1&trip_id=$1 [L]
RewriteRule ^admin/edit/([0-9]+/?)$ /ham/admin/edit/?hamurl=%1&car_id=$1 [L]
RewriteRule ^admin/?$ /ham/admin/?hamurl=%1 [L]
RewriteRule ^admin/([a-z]+)/?$ /ham/admin/$1/?hamurl=%1 [L]
RewriteRule ^admin/(.*)$ /ham/admin/$1?hamurl=%1 [L]

# TWO
RewriteRule ^([_0-9a-zA-Z]+)/(index\.php)?$ /ham/cars/?hamurl=%1&carname=$1 [L]
It seems to be doing TWO even after ONE has been done?!
When I check hamurl and carname on the cars index page I am getting ham for the the value of hamurl and carname is null.

Why is TWO happening after ONE has been done?

Could it be anything to do with this being tested on localhost?

Thanks


REGIX - 06.06.06 10:41 pm

Hey, cor, (or anyone smiley for :D ), can you send me a htaccess file that'll not display certain files, such as CSS or JS files? But, the scripts inside the server still need to access the files. Is there a way? smiley for :erm:

e-mail


corz - 07.06.06 10:00 am

REGIX, htaccess only controls access for surfers, it has nothing to do with your server-side scripts access to those files. In other words, assuming the files' permissions are correct, scripts can always access the files, no matter what you put in your .htaccess file.

hjb, the [L] flag is supposed to prevent further rule processing IF the rule is successfully applied. I started at the bottom, working up, and first I see %1 being used, and I wonder, where is that supposed to come from? There's no rewritecond line before it, and therefore, no bracketed expression to match!

Back to the drawing board!

;o)


REGIX - 07.06.06 11:49 am

Unfortunetly, my scripts don't get what is expected. See, I have a simple list of redirects, such as this:


Redirect ^.*\.css /index.php


It'll stop anyone from viewing the code directly, but the scripts are getting the redirected page as well, not the actual CSS file.

What I'm aiming for is no viewing the file, but my scripts can still access it. Is that possible?


hjb - 07.06.06 12:01 pm

I did indeed go back to the drawing board and now have a condition for every rule.

All is now working nicely.


corz - 07.06.06 12:21 pm

Good news, hjb!

REGIX, you misunderstand how this all works. Scripts don't use css files, browsers do. They are loaded directly by the browser. You can't redirect them, the browser needs to access them directly. It's not like a php "include", where the file is pulled in server-side. But it can be..

The only way around this (and why you would want to prevent someone downloading your css files is quite beyond me) is to use php (or similar) to inlcude the css file and spit it out as part of the page, inside style tags. But, the user still sees the css code, only not directly.

Bottom line, unless the browser has direct access to the css code, in one form or another, the page will have no styles.

You could perhaps use hot-linking code to prevent access from browsers that aren't loading the page associated with the stylesheet, but even that isn't foolproof, not by any means.

l*rz..

;o)


kai - 07.06.06 12:39 pm

hey cor, I am getting somewhere!


1. To redirect
# http://www.myexample.com/forums/index.php?topic=786.0 to
# http://www.myexample.com/index.php?option=com_smf&Itemid=71&topic=786.0

I did this:

RewriteCond %{QUERY_STRING} ^topic=(.+)\.(.)$ [nc]
RewriteRule ^forums/index.php /index.php?option=com_smf&Itemid=71&topic=%1.0 [r,l,nc]



2. To redirect
# http://www.myexample.com/forums/index.php?action=post;topic=786.2 to
# http://www.myexample.com/index.php?option=com_smf&Itemid=71&action=post&topic=786.0&num_replies=2

I did this:

RewriteCond %{QUERY_STRING} ^action=post\;topic=(.+)\.(.+)$ [nc]
RewriteRule ^forums/index.php /index.php?option=com_smf&Itemid=71&topic=%1.0&action=post&num_replies=%2 [r,l,nc]



I have added the above 4 statements into my htaccess and it appears to be working, but wonder if you have any comments.

Thanks so much for your help! smiley for ;)


Sean - 08.06.06 1:26 am

Hi Cor,
Thanks..just a quick follow up if I may...

-Old Path-
http://www.PourLeCorps.com/index.shtml?contactus

I place this in the htaccess file...
Options +FollowSymlinks
Rewriteengine on
RewriteCond %{QUERY_STRING} ^(.+)$ [nc]
RewriteRule ^index.shtml /%1.htm [r,nc]

Now when I type this in the address bar the old page...
www.PourLeCorps.com/index.shtml?contactus

I get this in the address bar of the redirected page...
http://pourlecorps.com/contactus.htm?contactus

The new page does show up however what I want to show up as the new address in the address bar (which is the real new address) is this...
www.PourLeCorps.com/contactus.htm

Advice please.

Also what do the search engines think the address of the page will be going forward?

Best Sean


pratik vanol - 08.06.06 6:58 am

hi, I m trying to redirect the URL.
suppose I will write in address www.subdomain.domain.org , I want redirect it to
www.domain.org/subdomain.

I am not able to understand how I write the .htaccess for this. where I put .htaccess file in public_html folder or subdomain's folder.....
plz help me..
thanks


CamihuVn - 08.06.06 7:11 am

Hi All
I have read about make htaccess
I want changer: http://www.myexample.com/index.php
http://www.myexample.com/index.vip
When i mouseover link at status Bar changer .vip
Thank so much....



corz - 08.06.06 11:43 am

Looks fine, Kai!

Sean, you mean, you want to remove the query string from the end of the URL? If so, simply add a "?" to the end of the rewrite rule..
RewriteRule ^index.shtml /%1.htm? [r,nc]
As for search engines; they will follow your redirections in exactly the same way as browsers.

pratik vanol, assuming the subdomain is correctly setup in your hosting/admin panel, then the rules go inside the subfolder.

CamihuVn, I can't help you with your code, unless I see your code. What have you tried?

l*rz..

;o)


pratik vanol - 08.06.06 2:57 pm

I want to create througth programming. What can I do......
I want to create .htaccess file for that.
but dont know what write inside it.

www.subdomain.domain.org

www.domain.org/subdomain
url redirecting.


Sean - 08.06.06 9:59 pm

Hi Cor,

I get this in the address bar of the redirected page...
http://pourlecorps.com/contactus.htm?contactus

so I want to get rid of the query string AND the rest ( contactus ) so that it is just this...

http://pourlecorps.com/contactus.htm


Help! lol smiley for :)

Sean






justme - 09.06.06 7:37 am

"the "-d" checks for the existence of a directory, NOT a URL path, or domain+directory, just the directory, so you only need to test "%1" on its own."

Ok. So can you or anyone tell me what I have to exatly include in the following code, im rid of regexps+htaccess etc:o . My problem was how to point subdomain.example.com to example.com if there is no subdomain folder in sub directory. Now when accessing random subdomain it is just trying to go to path that doesn't exist. Tell me this
RewriteEngine on 
RewriteRule ^.htaccess$ - [f]
RewriteCond %{HTTP_HOST} !^www.example.com
RewriteCond %{HTTP_HOST} ^([^.]+).example.com
RewriteRule ^$ /sub/%1/$1 [l]



corz - 09.06.06 4:18 pm

Sean, dude! I answered this already; add a question mark!

justme, first, I don't see how this is a problem. On what internet do people just enter random subdomain names into their browsers? Surely these come from links.

Anyways, when I said that the "-d" checks for the existence of the directory, I meant in the server's file system; you need to use a real path, for example..
rewriteCond %{HTTP_HOST} ([^\.]*).example.com$
rewriteCond /home/sites/example.com/public_html/%1 -d
If you don't know the real path on your server, find out (a phpinfo() is useful here).

At the end of the day, I don't like the idea of rewriting arbitrary subdomains to possibly-existant folders, and if I were stuck using mod_rewrite to create my subdomains (which, thankfully, I am not), I'd create a single folder in the root for ALL my subdomains to live in, and then (incorporating the above folder existence check) do something like this..
rewriteCond %{REQUEST_URI} !^/subz
rewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.org
rewriteCond /home/sites/example.com/public_html/subz/%1 -d
rewriteRule ^(.*)$ /subz/%1/$1 [l]
The folder being called "subz", and all the actual subdomains would live comfortably inside there.

Any requests to non-existant folders, say http://foo.example.org/bar.php are redirected to the root version, i.e. http://example.org/bar.php, while correct requests, for example http://yes.example.org/foo.php, would be directed to the real file at /home/sites/example.com/public_html/subz/yes/foo.php, "yes" being one of our actual allowed subdomains.

Of course, I'd likely add a second wee set of rules to catch direct requests to those folders and redirect them to the proper subdomain, but I'll leave that as an fun exercise for you!

;o)

ps.. if you use pre tags, you don't need to do that [[double square brackets]] thing, which turns regular bbcode tags into visible square brackets (i.e. fex them up).



Sean - 09.06.06 7:07 pm

Hi Cor,

Sorry about the last question, I thought the "?" was the query string. ;(

Anyway if you could just bare with me for one last question? I'm a newbie and this is really helpful so thank you.

The last question is this...

The new redirected path is now correct except that there is no "www" in the new path. For example this is showing up...

http://PourLeCorps.com/hairremoval.htm instead of....
http://www.PourLeCorps.com/hairremoval.htm

Also are these considered "Permanent redirects"?

Thanks so much.

Best,
Sean


justme - 09.06.06 10:02 pm

This one was correct, thanks for your help.
rewriteCond c:/progra~1/apache~1/apache2/htdocs/sub/%1 -d


justme - 12.06.06 9:08 pm

Well none of subdomain-to-folder rewritecodes have worked yetsmiley for :eek:

For example none of these have worked properly:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com?$
RewriteRule ^$ /%1 [L]

This is doing: sub.example.com --> sub.example.com/sub (WRONG)

One did 'forward' sub.example.com correctly but when accessing sub.example.com/test (test -fol inside sub -fol) it took me to sub.example.com/sub/test (WRONG) but still sub.example.com/test/ was working, note that ONE slash. And if I was trying to access something.php inside subfol(even if sub.example.com was working) it accessed root folder's one(or did 404).

Wtf is going, none of the trillions have worked yet.
So can anyone please provide GOOD WORKING ONE!


Hamid - 13.06.06 7:45 pm

The rewrite url shorted up my URL as I wanted, but it affected image URLs as well, so none of my images appear. Can you help me please

Thanks

Hamid


luke - 14.06.06 2:28 am

Hi,

I am a total novice. My first .htaccess file is not working:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^.htaccess$ - [F]
RewriteRule ^site/(.*)/(.*)$ site.php?screen_name=$1&site_name=$2


It brings up site.php but does not pass the variables to the php file.

http://www.foliowave.com/site/foo/bar/

http://www.foliowave.com/site.php?screen_name=foo&site_name=bar

Any idea appreciated!
Luke


bcswebstudio - 15.06.06 9:33 pm

I've implemented your great suggestion under "automatic translation" above. When I'm at work on our org network, it works great. When I'm off the network (ex. at home), however, the page translates only when it's a directly requested page (ie. www.myexample.com/faq.php-es)-- when the english page is viewed and you click the link to get /faq.php-es, the page that turns up is /faq.php w/o rewrite! I suspect it has to do with outgoing http_referer being blocked from within our network and not from without-- any thoughts on this?

wonderful page/info.. I've been here before and stumbled across again-- bet you never thought you'd still be answering questions so long after having posted the info!


Kmax - 16.06.06 6:13 am

Hello all,

I am trying to pass an ip to an activex control.
But I want to put a "fake" or "proxy" ip in there instead of the real one... so when you view the source you just see 111.111.111.111 or whatever.

Then I would like apache to send the data to the real ip and not the fake or proxy one.

Do you know if mod_rewrite can do this?
Send one ip to another one with all the http:// on it?

thanks


mremptywallet - 24.09.07 11:16 pm

I have no idea on what to do with my htaccess file. I am currently running a wordpress site and it has a default htaccess file. I am hosted on Go Daddy and i am trying to test out a new blog setup called Habari the requires PHP 5.1 or higher. I am on Go Daddys 2.0 hosing for Linux that supposedly allows you to user PHP 4 and 5 with 2 lines of code,
AddHandler x-httpd-php5 .php
AddHandler x-httpd-php .php4


My questing is i added the 2 lines of code but it does not work. I cant run the new Habari system. Can anyone here let me know how my htaccess should look?

<snipped - Cor>




corz - 25.09.07 12:31 am

mremptywallet, I snipped your code, it was just confusion. However, that was your only link to mod_rewrite, which is what this page is about. If you want to post it again, fee free; but please use [pre] tags to post code, then you can use any characters. like so..
<FilesMatch "\.(php|inc)$">
    SetHandler application/x-httpd-php
</FilesMatch>

Though I'm not sure how relevant it is; all the useful information was missing. I'd try commenting out the entire mod_rewrite section, unless you absolutely need it. At least while you test to see if your AddHandler command is working.

As to the AddHandler command, I do know that GoDaddy's web hosting has minimal .htaccess support, but enabling php should be fine. Use only one line..

AddHandler x-httpd-php5 .php

Though I would probably recommend this instead..

AddType application/x-httpd-php5 .php

(that's a regular [code] tag, btw, "/" is just fine)

There's also "SetHandler", if you get really desperate. See my example at the top. Note: any one of these commands could be presented inside a <FilesMatch> block, or directly. You could also use blocks, and do it more safely like this..
<IfModule mod_php4.c>
    AddType application/x-httpd-php .php
</ifmodule>

<IfModule mod_php5.c>
    AddType application/x-httpd-php5 .php
</ifmodule>

Which would use php4 if php5 wasn't available.

Have fun!

;o)


noname - 29.09.07 10:16 pm

Well, THANKs for sharing!

Concerning your "www" rewrite rule, what do you think about this variant (which does not have the specific domain inluded)

RewriteCond %{HTTP_HOST} !^www\.      [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)        http://www.%{HTTP_HOST}/$1   [R=301]



corz - 01.10.07 12:11 pm

The first time I read it, I instantly saw the beauty of it. Then I looked again. smiley for :eek:

It seems to do the exact opposite of what is required; that is; ADD "www." to all requests that do not begin "www.". That's nuts! That's exactly what we are trying to get away from.

However, I'm willing to pretend that you did, in fact, post this wonderful snippet (inspired by your good self)..
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^www\.(.*) [nc]
RewriteRule ^(.*)	 http://%1/$1 [r=301,nc,l]
In which case, I would probably reply along these lines..

Ha! Everone's got a better "lose the www" rule! smiley for :lol: Just kidding. It's really a good thing; every time a fresh pair of eyes hits the page, the possibility of interesting new approaches exists. What do I think? I like it!

What I like best about it, is the portability; you could run the exact same .htaccess file on your mirror AND your live site, and get the same result. This is a highly desirable thing. I recently and happily discovered that my new server does not require domain names in r=301 rules* (my old server insisted on them), so I've been moving towards this state myself. Simplicity rocks!

Another cool thing about this rule is that it can be used to redirect multiple domains. I have a couple of other domains running inside corz.org, in their own sub-folders. This simple ruleset in the main .htaccess file would redirect ALL www.whatever.com domains to their correct whatever.com format. Pretty neat. I'm using it right now!

So I'd recommend your approach in a flash; however, it would need to first be tested on a particular server, pererably with your "test" domain..

I used to have access to a server where almost nothing worked. This sounds useless, but it was, in fact, quite the opposite, and I regularly dropped php and apache code on there to see how it would fare, a sort of crash-test-dummy-server. Your code wouldn't work on there!

I used %{HTTP_HOST} a lot more in rewrite rules myself, until I moved servers once and they all broke. Big fun! I tend to only use it in rewrite conditions these days, where it never breaks, so far…

Anyways, I like your variant so much, I might even do a "variations" section, with a few alternative approaches.

Cheers! smiley for :D

;o)

references:
I'm guessing this is because my current host has (correctly) setup my domain as "corz.org", wheras my old host had it set to "www.corz.org", so removing the "www" and redirecting to the HTTP_HOST would have created an infinite loop.



Terrykm - 15.10.07 2:13 am

My .htaccess has this rule

RewriteRule ^viewcart$ /store/cart.php?m=view [NC]

which works fine - that is, it loads the View Cart page.

However the url of that page is /viewcart

But in order for any subsequent clicks on that page to produce the desired outcome the url needs to be
/store/cart.php?m=view

Any way to do that with a rewrite rule?

Thanks!

T


Harsh - 15.10.07 6:40 am

hello every one,
i am new at apache,
and i want to rewrite my url through .htaccess ,
but it redirects my page to another directory,
so plz any one can guide me to solve my problem in which i can rewrite my url with out redirecting my page.

my url is like that
mysite.com/vox/code/index.php
i want to rewrite url like this
mysite.com/



adam - 15.10.07 11:55 pm

Hi Guys, I'm trying to figure out what exactly the following script does:

AddType   application/x-httpd-php .html
Options   +FollowSymLinks

RewriteEngine On

RewriteRule ^(.+\.(gif|jpg|css|png|exe))?$ - [L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/(index\.html)?$ index.html?url_path=$1 [QSA,L]


I guess the last line is the that is confusing me. I think that it is taking everything in the URL that is before the index.html part and rewriting it to index.html?url_path=xxxxxx.

Is this a redirect or simply passing a parameter into a script?

Thanks for any help in advance.


corz - 16.10.07 1:49 am

An intriguing little ruleset, that, adam. In fact, it looks more like two rulesets, joined together, with something wonky in the middle, somehting which would probably be happier in a block like this, like this..

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [nc]
RewriteRule ([^denied])\.(gif|jpg|css|png|exe)$ /denied.gif [l]

Which is another common Hot-Link prevention approach. As it stands, it will interfere with the following rules, though having said that, my test server doesn't respond well to the [L] flag, and so I rarely use it and lack a great deal of experience with it, and if I see "- [L]", I generally do [F], instead. It should stop processing rules right there, and send a 403. Is this what you want?

The first bit; parsing .html files as php; is nothing unusual.

The last part, is as you describe, except that it first checks to see if the requested resource exists as a file or a folder (-f, -d; a wasteful process, best avoided), and rewrites missing files to index.html?url_path=xxxxx. Interesting. It looks like a snippet you "found". Are you attempting to do something in particular?

Terrykm, YES! Whatever it is you want.

Harsh, scroll up, there's a whole article about this!

;o)


Murali - 16.10.07 9:44 am

Thanks a lot....




adam - 16.10.07 10:40 pm

Hey, thanks for the reply Cor. I figured the first part was to parse html files as php which is cool. So, the second part is related to blocking image hotlinking. For the site this is used in I don't think this is going to be an issue, really, but you never know. You are right the rules you have shown interfere with the rest of the rules and the site breaks!

I am doing some work on an existing web app and I have 'inherited' this htaccess file! Took me a while to figure out what is going on but basically all links are rewritten as index.html?url_path=xxxxxx. The index file resides in the apps root directory. So you can't access a file by typing the path of the script. You have to access it via the index.html file.

The xxxxx part is then processed by the app as an array and the script figures out which modules to load up etc. I suppose it would be neater to rewrite the xxxxxx part as a proper query string and process that? That would mean a rewrite of the parser bit though...does that make sense?

What do you mean rewrites missing files? So if a file doesn't exist it just rewrites toindex.html?url_path=? Also I am not clear on what the
[L], [QSA] and [NC]
flags are doing.

One last question, the %[SCRIPT_FILENAME] refers to the same resource that is being rewritten in the last rewrite rule?

By the way, I really like the site. Nice and original and a good colour scheme etc. Good stuff!

Thanks,
ads



corz - 17.10.07 2:13 am

Ahhh... inherited code. Ouch!

Yes, the index.html is the "master" page, by the looks of things. The previous creators probably added this catch-all facility to grab stray requests (missing files, etc.), and pipe them through the "parser", as you call it; perhaps hoping for a second chance at the URL's, or at least throwing up the default page, rather than a regular 404. Not a bad strategy, though wasteful; Apache1 has to check for the file existence, and folder existence before processing every single request. That's quite an overhead.

If I'd inherited that code, I'd probably pull off another .htaccess trick, do something like this, instead...
ErrorDocument 404 /index.html
BALLABANG!2

adam said..
The xxxxx part is then processed by the app as an array and the script figures out which modules to load up etc. I suppose it would be neater to rewrite the xxxxxx part as a proper query string and process that? That would mean a rewrite of the parser bit though...does that make sense?

Yes, it makes perfect sense, and I guess leads us right back to the start of my post. For me, writing code continually becomes more and more about writing maintainable code. It might be someone else, or it might be me, a year from now, maybe after six months working on songs and poems; either way; we're gonna need to be able to pick up the code and work with it, without having to spend days trying to get our heads around it. If only people could grasp the value of good NOTES, preferably right next the code they refer to.

Confusion is the enemy of time. With my own code, I become more than occasionally obsessed with making things simple. Simplicity if the friend of time, I'd have no doubts; no question at all, I'd rewrite the code so it made sense, nomatter how long it took.

However, in the real world, one needs to weigh up many things; do you have the time? Are the changes feasible? Will they take lots of effort to implement? Will they affect your inward links and SEO efforts? Will the content management still work, afterwards? smiley for :aargh: And so on.

Getting a clear and logical URL scheme is extremely important, so if it's a relatively new site, it's definitely worth taking the time to get it right from the start. That's always going to be easier, in the long run, than trying to retro-fit a site with improved logic some way down the line.

Consider nice flat links. I've been using them for my blogs, and find them most elegant.

Which brings me neatly to the site design. Thank You! So few people comment on it, and was such a lot of work! The switch to XHTML+CSS wasn't plain sailing, by any means, but the clarity of the current code, and how easy it is to maintain and add to the site, gives me a warm fuzzy glow every day! Simplicity all the way, baby!

for now..

;o)

references:
  1. As for the flags, I'll have to direct you to RTFM, not so much because it's easier for me, but because there are many other useful flags; and a knowledge of each is another string to your bow. There are a few places in these articles where I hint at possibilities, but don't delve there; hoping the readers' curiosity will be peaked enough for them to hunt some. It's worth the effort.
  2. I've not done this; it just popped into my mind as a cute solution to the file-checking overhead. It would work though, so long as you remember that, at least techically, regardless of the fact that it's the front page, the browser has received a 404 error, which just happens to also be the front page. As soon as they click somewhere, all this is moot, but it's good to know.



Anirban - 19.10.07 9:08 pm

Hello Cor,

Your article helped me a lot to understand how to use htaccess. There's a problem with what i am trying to do. This is what i wanted to do. I've got a page called profile.php which takes in the parameter uname as the username who's profile i need to display. So, what i wanted to do is to make the profiles to show like on myspace... like mysite.com/myprofile

But, the problem which is coming up is that it's now showing the index also as a blank profile page as it's technically not having anything on it. This is what i used for the htaccess.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)?$ profile.php?uname=$1 [L]
Options -Indexes


So, since it's supposed to take mysite.com/myprofile as a uname=myprofile so now it's taking blank as the uname for the index of mysite.com. I tried by redirecting to the index file but got a Internal server error. So, i'm in a messy situation. Can you help me out? If you can i'd be very thankful. Thanks for all the info you already provided till now.

Regards,

Anirban.


corz - 20.10.07 8:28 am

There are a few ways to tackle this, Anirban. The easiest is probably with a RewriteCond statement..
RewriteCond %{REQUEST_URI} !^index\.htm(l)? [nc]
before the RewriteRule line (or index.php, if that's what yours is). You might also consider removing the "?" from the RewriteRule command, so only requests with something in them get redirected, otherwise loading your TLD on its own will get you redirected.

You may also want to consider merging the -Indexes option into the first Options statement. While having two separate statements is fine; technically speaking; it's bad practice, and inevitably leads to confusion and errors later on. I generally do..
  1. caching
  2. errors & denials
  3. options
  4. rewrites
  5. extra stuff (php directives, etc)
And keep roughly the same system throughout all my .htaccess files.
Lots to consider!

;o)


Anirban. - 20.10.07 9:41 am

Hello Cor,

Thanks man the problem is now sorted out smiley for :)smiley for :)thank you so much. Now the files are working just fine.

Regards,

Anirban.


Harsh - 23.10.07 12:34 pm

hello every one,
i am new at apache,
and i want to rewrite my url through .htaccess ,
but it redirects my page to another directory,
so plz any one can guide me to solve my problem in which i can rewrite my url with out redirecting my page.

my url is like that
mysite.com/my_fold/code/index.php
i want to rewrite url like this
mysite.com/my_fold

for that wrote my .htaccess file as follows
Options +FollowSymLinks
RewriteEngine on
RewriteBase /code/
RewriteRule ^(.+)/([a-z]+)\.php http://mysite.com/my_fold/$1/$2 
RewriteRule ^(.+)/([a-z]+)_([a-z]+)\.php http://mysite.com/my_fold/$1/$2 

i have tried a lot with that code by adding and removing RewriteBase and but every time my page is redirecting instead of rewriting my url and give me error like this "Object Not Found Error 404"

So friends plz if any one can help me to resolve my problem then i will be very thakfull to you all,



scott - 23.10.07 1:51 pm

Okay, rewriting rules are OFFICIALLY, retarded.

^images/(.+).(.+) /images/fetchimage.php?fkey=$1.$2 doesn't work. (500 Internal Server Error)
^images/(.+) /images/fetchimage.php?fkey=$1 doesn't work. (500 Internal Server Error)
^images/(.+).([a-z]+) /images/fetchimage.php?fkey=$1.$2 doesn't work. (500 Internal Server Error)
^images/(.+).(jpg|jpeg|gif|png) /images/fetchimage.php?fkey=$1.$2 does work somehow?

If any of the top 3 were to work, I'd expect the SECOND one at least, it's not even remotely complex, but I'm convinced whoever wrote the protocol for these expressions seriously f'ed up somehow. But meanwhile my code is crippled and I'm pissed off for having to spent hours trying to figure this sloppy system out. smiley for :ehh:

OMFG and now your comment system is giving me lip about unbalanced BBCode tags smiley for :aargh:


corz - 23.10.07 2:07 pm

Harsh, "everyone" left the minute they got their solution. That's just the way thing are. You can thank them "all" if you like, but they won't hear you. It's just you and me, for now, and I'm not your free tech support guy.

I already spent a lot of time covering exactly your situation; I wrote a whole article about this stuff, in fact, and I already answered you, pointing you to that very article (above), which I recommend you read. I also recommend you read the Apache manual page for "RewriteBase", which you clearly haven't, at least if you plan to use it.

Your solution is the most basic of rewrite rules, explained, here.

scott, haha! synaptic breakdown, clearly overdoing the rewrite rules. But that's not rewrite code, none of it; there's no actual "RewriteRule" commands, for a start. If you want me to take a look, use [pre] tags and post the actual code you are using. Check below the comment form for more juicy tips like that.

And for the record, mod_rewrite is officially not fexed up, it only looks that way sometimes, until we figure out the rule exacitaly. Then it's a beautiful thing.

;o)


scott - 24.10.07 1:29 am

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^images/(.+).(jpeg|jpg|gif|png)$ /images/fetchimage.php?fkey=$1.$2 [nc]

I'm trying to capture all requests to /images/* and redirect to /images/fetchimage.php?fkey=*
It's not working so well smiley for :blank:

I think what is going wrong is it's getting confused and doesn't know what to associate with each $n reference, or something. Although I still don't get why
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^images/(.*) /images/fetchimage.php?fkey=$1 [nc]

doesn't work. I even tried ending it with a $ anchor, I have no idea how it's getting confused and can't enable logging. I really don't want it to ONLY access the script if I enter "/images/a.jpg", and then getting a normal 404 if I enter any other extension, or none at all. Makes everything seem so choppy.

And I just had a thought, if fetchimage.php is in the /images/ folder would it be possible it's getting into an infinite loop? Do rewrite rules also check the location they are rewritten to?


scott - 24.10.07 1:35 am

Yep, that was exactly what was happening. Moving the file to the root dir made everything peachy keen. I guess I just needed some sleep to see what the hell was going on there. Oh well, thanks alot for the tutorial here, never knew about rewrite rules, and despite certain frustrations it's certainly helped alot. And now I can continue on with my worthless quest to rebuild my website! Yay.


corz - 24.10.07 6:54 am

Good work, scott!

It's very like when I was at school, I'd put up my hand to ask the teacher something, and invariably, while I was explaining the problem, I'd come up with the solution.

I realized that often, all it takes, is to run through the problem AS IF you were explaining it to someone, and I started doing that instead, to great effect.

The "infinite loop problem" is a common one; I do it myself every once in a while, and go "Oops!". A simple RewriteCond statement will fix it, either checking for the existence of a real file, or better; that the request ISN'T for fetchimage.php..

RewriteCond %{QUERY_STRING} !fetchimage.php [nc]

If I'd looked more closely at your original post, I probably could have seen that; but I've learned the hard way that it's not worth trying to diagnose incomplete snippets, and I don't even go there! If I'm gonna frazzle my synapses, I'll do it with my own code! smiley for :aargh:

Putting the fetchimage.php file in a different directory would also simplify things; always desirable. I guess you now realize why that one line did work, yeah? If not, I'll leave it with you as fun for your brain.

And hey! The quest to rebuild a website is rarely worthless; in fact it's almost always the opposite, especially if you are taking some old HTML mess, and transforming it into lovely XHTML+CSS. Mmmm.. Simplicity rocks.

Have fun!

;o)


jim - 26.10.07 6:03 pm

Hi, I am trying to
rewrite my_domain_name/username to my_domain_name/tools/tools.php?uname=username

so that people can access their pages easily.

I am new to htaccess thing. Although I tried very hard, I really can not make it.
Could you please show me how to do it?

Thank you so much



jim - 26.10.07 6:12 pm

by the way, the username may be in unicode. I don't know whether it matters though.

Cor~ I need you...

Thanks a million in advance


corz - 26.10.07 6:16 pm

Did you know, you can edit your posts?

Anyways, show us what you've got already, the code that isn't working.
Use [pre] tags to post the code.

;o)

ps. I'm in the middle of moving the site to a new server atm, so I may not respond immediately.
A email from PayPal always gets my attention, though! smiley for ;)


Brasso - 27.10.07 7:06 am

Hi Guys,

I'm new to this so please help!smiley for :ehh:

Does anyone know how I can do the following rewrite? -

changing all urls that start domainname.com/hotel.php?****** to domainname.com

The text after /hotel.php? could be anything ie:

/hotel.php?The-Hilton-London-Code05228

basically, I need all urls that use /hotels.php? to be re-written to homepage

Hope this makes sense!smiley for :eek:
Thanks!


corz - 27.10.07 12:39 pm

Try reading the article (above).
That will help.

;o)


samorsomething - 29.10.07 11:23 pm

Thanks for the great tut, it's helped me out a few times and I been back 3 or 4 times... So far hasn't been anything I couldn't tackle.

Have to say though its funny reading all the comments and seeing all these please help me posts.
I'm gonna check out the rest of the site and hopefully find a way to play with this tagging system.


corz - 29.10.07 11:56 pm

You're welcome!

And yeah, they are a source of amusement, but more so; noise, and after strengthening the initial message a few times, I think they are slowly thinning out! smiley for :lol:

erm.. tagging system?

;o)


Abhijit Mukherjee - 30.10.07 6:36 am

hello,

I want to convert my URLs Querystring to Folder structure

currently I am having

http://192.168.1.150/projects/abc.com?id=1&gt=3

I want to make it

http://192.168.1.150/projects/id/1/gt/3 or
http://192.168.1.150/projects/1/3

How do I do that? can any body help me

Thanks in Advance
Abhijit


corz - 30.10.07 7:35 am

Or, perhaps not.
Do you see what we're up against?

This human seriously believes that by plainly showing that he couldn't even be bothered to read the HUGE notice above the comments, let alone the article itself, he will somehow entice me to donate my time to him, for free; something I have clearly already done, evidenced by that big wodge of text above these very comments, the part that so many seem to just skip over1 in their haste to ask for "help", something I'm about to provide, except in a deeper and truer sense..

Of course, regular readers know well that the reason this individual hasn't posted any code, is because he hasn't got any!  He didn't even attempt to get it working; figured someone else would just do it for him. Abhijit, do you have slaves in your locale? Is there some sort of cultural factor at work here? I don't think so.

Now, if this was a community, which it is not, some bod would definitely pipe in with a suggestion, and with some luck, you might even coax a solution out of your appalling situation. But relying on luck isn't going to get you, or your website very far, is it? Fact is, if every I-can't-be-arsed web master out there was to fuck off and do something he actually took seriously, the web would become an infinitely better place for us all2.

Let me guess, you're starting a mobile phone site? Because we definitely need more of those. Or maybe something truly revolutionary, but based-on youtube? Go away! You may find you have a real purpose in life. Something else. You could be living a charmed existence, benefitting mankind, and doing something you truly en-joy, and for all the right reasons, instead of just adding to the noise.

That's how I feel about the web; I'm passionate about it, I dream code. You obviously don't give a shit about being a web master, or else you'd right now be pouring through the basic documentation, checking out the recommended reading, learning all you need to know to do the task, pounding your home server, and sharpening your skillz until you know  the web.

If you had ever thought about it, you would have realised that the title "Web Master" implies that THERE IS SOMETHING TO MASTER! Mastery of begging does not qualify.

Hey! This is great getting, thanked in advance; basically carte blanche to say whatever I want and know that I already  have your "appreciation", which I've anyways come to learn means, "thanks. bye".

So, in the deepest and truest sense of the phrase, please..

FUCK OFF!

for now..

;o)


references:
1. Not all, though. I had an amusing morning recently, downloading copies of this very article from other web sites.
Amazingly, a couple of them actually completed the arduous task of mentioning where they stole it from.
2. Don't foget to take down your sites before you go!



Mato - 07.11.07 9:16 pm

Hello im trying and trying something but i cant!

First of nothing i need to say that i have a rule on .htaccess on my root folder (http://mysite.com/.htaccess), and it have the next code:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} www.mysite.com.ar [nc]
rewriterule ^(.*)$ http://mysite.com.ar/$1 [r=301,nc]

I say it because maybe it modify the new thing im trying to do.
I need a redirect from:

http://mysite.com.ar/forum/

to

http://mysite.com.ar/forum/index.php

This is possible?

Thanks in advance


corz - 08.11.07 12:29 am

Yes Mato, it's possible. In fact, it should happen automatically, without any rewriting. I just answered this on the other page recently, simply add..

DirectoryIndex index.php

To the top of your .htaccess file, and you're done, at least, you should be.

;o)


Justin - 12.11.07 4:56 am

Hello,

Thanks for your help. I have some problem, it would be great if you could help me here . . .
Options +FollowSymlinks
RewriteEngine on

addtype application/x-httpd-php .html .htm
RewriteCond $1 !^(index.php|js|images|listingImages|system/plugins|robots.txt|css/)
RewriteRule ^(.*)$ http://globalprompt.net/projects/affiliate/index.php/affiliate/$1 [L]

above is my htaccess file. and its works great. But i want a slight modification

What I want is if the above rule fails {the url does not exits } then use another rule. some what like this..

Options +FollowSymlinks
RewriteEngine on

addtype application/x-httpd-php .html .htm
RewriteCond $1 !^(index.php|js|images|listingImages|system/plugins|robots.txt|css/)
RewriteRule ^(.*)$ http://globalprompt.net/projects/affiliate/index.php/affiliate/$1 [L] 
#if this one fails{page not found} then below rule should be follow
http://globalprompt.net/projects/affiliate/index.php/affiliate/listings/$1 [L]

Please help me.

best regards,
Justinsmiley for ;)smiley for ;)



Luis - 12.11.07 12:33 pm

Hey Cor,

I don't have any question to bother you, but I couldn't help dropping a line to tell how how much I've laughed reading your answers to those i-am-a-webmasta guys. Very nice article. I have implemented a rewriter for ASP.Net, working in a very similar way in essence. The basics are easy, but ASP.Net deals with postbacks and there are a few things to consider. I recommend these links if anyone was trying to apply this to MS environments:

* Tip/Trick: Url Rewriting with ASP.NET
* MSDN: URL Rewriting in ASP.NET
* Jesse Ezell Blog: Fixing Microsoft's Bugs: Url Rewriting
* The Passion and the Fury of URL Rewriting

I have successfully implemented URL Rewriting on my website (it's in Spanish, though): www.respetame.com
The goal of rewriting the URL, in my case, was to make the site more SEO friendly, but I have other websites with copyrighted pictures and I think that rewriting can be very helpful. Also to avoid hot linking, etc..

Thanks Cor!



corz - 12.11.07 1:20 pm

Note to self: don't do comment replies when you've just woken up!

I posted this to my development mirror, earlier. Here it is again, except on the correct server smiley for :roll: ..

Thanks for your help. I have some problem, it would be great if you could help me here . . .

Oh! I intend to, Justin! There's a link at the foot of this post that will sort you right out! Let's have a look, shall we..

Okay, I see what you've got. (by the way, avoid RewriteCond $1, it only causes confusion later on, stick with RewriteCond %{REQUEST_URI}, which is readable)

And I see what you want.

What I don't see is, what you've tried.

;o)

ps. this might help.

--

And in the here & now..

Thanks for the links, Luis; it's a dimension of rewriting I rarely consider, being Linux across-the-board, at least for serving. I'm sure others will find them useful; I'll likely pop them somewhere in the main article. Cheers!

I am glad you enjoy the text, too; it's great to have an opportunity to really help people, dontcha think! smiley for ;)


Luis - 12.11.07 2:38 pm

Even if someone is lazy, if they don't even bother reading the basics, how can they think about having a decent website.

Hey I now have a question: how does rewriting affect a server's performance? If a shared server has lots of users with dozens of rewriting rules, does that slow down the server? Can be the case that a hosting provider denies the chance of doing rewriting? I understand that every http request is validated against the regular expressions we've created on our .htaccess file.

In IIS you don't have a built in option yet, but it's coming up in the newest version (IIS7). However one can create a custom module to do the rewriting. While I've been working with it I've come to think of all the code that is executed each time! And when it goes further than regular expression matching (security checking, DB connections) before rewriting, it is even worse.

Also we have a very particular situation for which I think I can't use rewriting with reg. expressions. We have to inject the language and the version & subversion numbers of the requested content in the URL. That means manually checking and messing with the path. But then again that is very particular, and for many people regex are just OK.

Well, I am going to read your cover of RSS.

Keep it up!

Regards from Barcelona (Spain)



corz - 13.11.07 11:17 am

Yup, good web sites generally represent hundreds, if not thousands of hours work on the part of their developers. Quite often with little reward, except the work itself, which is, thankfully, enough.

And yes, you are right to be concerned about rewriting affecting performance, because it shows you clearly understand the sheer volume of code involved in performing these manipulations. But mod_rewrite is extremely efficient, and in the real world, you won't see a performance penalty.

However, some mod_rewrite code can be extremely inefficient; that is, user-code. directives with a -f or -d switch are an instant performance hit. If such rules live in your main .htaccess, and are checked on every request (people do this! nuts!), it can have implications. Also, double (.*) sections in a single pattern can affect performance; mod_rewrite has to look twice, so to speak, so some thought needs to be applied on a busy server, for sure.

However, it's practically unthinkable that a host would setup Apache without mod_rewrite, and extremely unlikely they would deny this facility to their clients, unless they wanted to go out of business, or were just hosting for fun. mod-rewrite is too handy, and its usage too widespread, that you would be hard pushed to find a non-mod_rewrite enabled server to compare performance with!

What a web master needs to know is, will performance be an issue?
And the answer, as always, is Test Test Test.

It has to be said that rewriting in .htaccess is much less efficient than in a server context (vhosts, or httpd.conf), but still, almost miraculously efficient compared to what one might expect from the processing involved. I haven't studied the source code - one day maybe - but I'd wager it's full of juicy goodness!

Good luck with your IIS rewriting implementation; a task I do not envy.

;o)


Tiredwithrewrite - 14.11.07 3:43 pm

Hi,
I have read your article and have tried out a few things but Im still not able to accomplish my real task.

Below is the htaccess file that I have and out of that one condition works perfectly for me but the actual part where I wan to have directory type of URL it is not working smiley for :ehh: Can you please please help me?
Options +FollowSymLinks
RewriteEngine on 
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC] 
RewriteRule .? http://www.mysite.com%{REQUEST_URI} [R=301,L] 
RewriteRule ^partner_details/([0-9]+)/([0-9]+)/([0-9]+)$ partner_details.php?catp_id=$1&p_id=$2&mcat=$3

The first part works fine where in if user enters http://mysite.com then the URL changes to http://www.mysite.com
but the second part where partner_details.php has to change to partner_details/ is not working.
I am using Apache 1.3.37.


corz - 14.11.07 4:10 pm

Tiredwithrewrite, the rule looks fine. I'm assuming you meant to say..

but the second part where partner_details/ has to change to partner_details.php is not working.

And not the other way around. Right?

The only things I can think that might be wrong are..
  1. you are supplying the wrong kinds of URLs.
    Your rule REQUIRES that the URL is in the exact format..

      http://www.mysite.com/876/65476/987

    except with other numbers. Unless all three sets of numbers are there, the rule will fail

  2. you have no line break at the end of your .htaccess file after these rules. In other words, that rule is on the very last line of the file.

  3. something else

"something else" could be something real silly, like partner_details.php doesn't exist, or partner_details.php send a location header, or some other such tomfoolery.

It's more likely to be one of the first two, though.

;o)


Tiredwithrewrite - 14.11.07 4:28 pm

Hi Cor,

First of all let me really thank you for the quick reply.

Sorry about the confusion:
Yes I want my URL to be like
http://www.mysite.com/partner_details/2/33/2/

Ok as for the format of URL well it is exactly as it is. Meaning the three parameters are being passed. SO I can actually cross out the first one.

As for the second "Line Break". If that means [L] then I have tried that too and it still does not work.

And Im sure that the partner_details.php does exist because it is working normally:
http://www.mysite.com/partner_details.php?catp_id=38&p_id=27&mcat=1


So i too can think that its only the second point. Guess im doing it wrong there. Is there any special way to give a line break? As mentioned I have tried [L]


Thanx again for you reply.




corz - 15.11.07 6:08 am

A line break (aka "newline") is the character that is formed when you hit <enter> on your keyboard. Like <br /> in HTML, but for real files.

If the last rule is on the very last line of the file, place your cursor at the very end of that line (End key) and then hit <enter>, then save.

http://en.wikipedia.org/wiki/Linebreak

;o)


Tiredwithrewrite - 15.11.07 6:44 am

Hi Cor,
Well yes there was no line break and the rule was the last line. I have changed that and yet im not able to get the result. My URL still shows up as http://www.mysite.com/partner_details.php?catp_id=38&p_id=27&mcat=1

Is there something else that i need to do like restart Apache or the server or may be initialize the .htaccess file in any other way? I have currently just copied the file to the server. Any permissions to be set? Im quite in experience in these terms :(
In any case I really appreciate the help that you have given so far. Thanks a ton for that.


corz - 15.11.07 2:20 pm

My URL still shows up as
http://www.mysite.com/partner_details.php?catp_id=38&p_id=27&mcat=1

erm, isn't this what you want? smiley for :eek:

You seem to be confused.

What you have now achieved, is a rewrite FROM /partner_details/2/33/2/ TO /partner_details.php?catp_id=38&p_id=27&mcat=1, so if you type this into your browser..

http://www.mysite.com/partner_details/2/33/2/

It will be rewritten (in the background) to /partner_details.php?catp_id=38&p_id=27&mcat=1, and partner_details.php will load, with the variables catp_id, p_id, and mcat available to the script (though the browser will still display the flat link in its address bar).

You're done!

Of course, your page also needs to be producing these links in the first place. I'm assuming that whatever software you run on your site (or the HTML, if it's a static page that has these URLs on it) is actually PRODUCING flat links in the form of /partner_details/2/33/2/, right?

Your rewrite will translate the incoming /flat/link/ requests into ?variable=foo type requests.
But it can't create the links for you!

;o)


Tiredwithrewrite - 15.11.07 2:54 pm

No wonder Im really tired smiley for :blank:
I am actually looking for a way to transfer:
/partner_details.php?catp_id=38&p_id=27&mcat=1
TO
/partner_details/38/27/1/
OR
/partner_details/Models/Fiber/Lamp/

I still am under the impression that in the RULE you first specify the PATTERN then what you want translated.smiley for :ehh:

Cor Im really sorry if I have been silly and troubled you. But like i said I want to make by ugly looking URL to look beautiful by removing all the ? = & etc.

Im using PHP MySQL on my server.

I feel like a FOOL


corz - 15.11.07 3:11 pm

There's no need to feel foolish; your rewrite rules are 100%. That part is just fine. Trouble is, it's only half of the story.

For what you want, you do need rewrite rules, but you also need to create those flat links in the first place.

Perhaps you imagined that .htaccess could do both parts "automagically", and on-the-fly. No. That's just not possible. The flat links have to be on the page in the user's browser, and aside from special circumstances .htaccess has nothing to do with that. That's up to your web code.

I don't know what you use for a back-end on your site, but whatever it is; some php CMS, or shopping cart software, or whatever, THAT is where you need to make the changes, THAT is where the actual flat links are created. If the back-end doesn't generate a flat links on the actual page the user gets in their browser, then the user will never see, nor request /flat/links/, will they?

What rewriting does, is transparently converts the flat links the user clicks in their browser, BACK into proper ?variable=foo type links, for use in our back-end.

For the whole thing to work; you need both parts; back-end code, and .htaccess rewrites.

If there's still any confusion, please do get back here with another comment/question. I'd like to pin this down absolutely, so I can add a section about it in the main article; you are not the first to make this set of assumptions.

;o)


Chris - 15.11.07 3:51 pm

Just a quick line to say thanks for not only a couple of great pages on the mysterious .htaccess inner workings, but almost even more for the fantastic and entertaining comment section!

If you're ever in Zermatt for a bit of skiing, etc, give a shout -- a beer or three are reserved as a small thanks smiley for :cool:


corz - 15.11.07 4:26 pm

Cheers, Chris!
Czech Lager for me!

I'm building a list of venues. I reckon, in a decade or so, I'll have enough pit-stops lined up to do a world tour! smiley for :D Hey! I'm serious! smiley for :lol:

As for the comments, well, it's funny you should say that, because I was just having a read through some of the older ones myself, which I rarely do (break-time, following up a suddenly popular inward link) and thinking the very same thing! Some of them are rather good, aren't they.

Och well, back to work..

;o)


Jamie - 15.11.07 7:52 pm

Hi there!

First of all, great article!
It's the first useful resource I could find helping me with my htaccess problems.

But there is something I miss in the article. I noticed how you described the REQUEST_FILENAME in case of allowing (or denying) access to single files but I miss the flags you can put behind REQUEST_FILENAME.
i.e. REQUEST_FILENAME !-d or REQUEST_FILENAME !-f

Could you please explain some more on this because this is were I get stucked.

Why do I get stucked on this? Well I will explain with this example:

Options +FollowSymLinks 
RewriteEngine on 
RewriteOptions MaxRedirects=15

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*) /index1.php?p=$1 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^en/(.*) en/index.php?p=$1 


This should do 2 things:
1. Make sure all people who go to www.myexample.com/home are 'secretly' transfered to www.myexample.com/index1.php?p=home
2. Make sure all people who go to www.myexample.com/en/home are 'secretly' transfered to www.myexample.com/en/index.php?p=home

Problem is: the first one works but the second doesn't.
How is this possible?


corz - 15.11.07 10:20 pm

If you change everything after "RewriteEngine on", it'll work great!

;o)


Jamie - 16.11.07 9:50 am

Uhhh ok.... That's an easy answer.

Which lines should I change and why? I did as you described in the article and I honestly can't see what's wrong.



corz - 16.11.07 12:11 pm

smiley for :lol: At least you came back. Good for you! Lots of folk would have scurried off with their tail between their legs! (you get points for this) At any rate, the only lines that remotely resemble ANYTHING in my article are..
Options +FollowSymLinks 
RewriteEngine on 
That's it! Who knows where you go the rest! Hopefully not from any of the examples in these comments, which are specific to the poster being answered. For starters, I normally wouldn't recommend the use of -f or -d unless they were absolutely necessary. And this..
RewriteOptions MaxRedirects=15
is just another way of writing..
# my code sucks, I expect it to fail
which I never do! I haven't even given that to someone in a trick reply!!! Part of me wants to stop this post right now, because attributing that mess to me is tantamount to libel! smiley for :lol:   Now that we're done with the formalities, let's look at why your (someone's) code doesn't work..

The first and biggest problem is that the rules are back-to-front. How do you expect any request to ever reach en/ when you have already redirected everything* with the first set of rules?

Secondly, the -f or -d serve no real purpose here. The seem to have been placed there for no other reason than to slow down your page response time. For images, was it?

Thirdly, why is the main index called index1.php? That also smacks of luser mentality, like you can't get anything to work, so "let's try giving them different names and see if that helps". Dude!

I know it seems complex, but really it's quite simple if you take it step by step, and apply logic. Let's see what you are trying to achieve..

This should do 2 things:
1. Make sure all people who go to www.myexample.com/home are 'secretly' transfered to www.myexample.com/index1.php?p=home
2. Make sure all people who go to www.myexample.com/en/home are 'secretly' transfered to www.myexample.com/en/index.php?p=home


Basically, you want transparent translation from flat -> dynamic links. That's not so hard. By simply switching them around (putting the "en/" rules first) we have most of the solution. But we also need to get rid of those nasty -f and -d switches..

Instead of checking the filesystem for every existing file and folder (with every single request), why not just redirect everything that isn't index.php? Something like this..
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^en(.*) /en/index.php?p=$1 [l]

RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*) /index.php?p=$1 [l]
So far, so good. But you will probably want to allow direct access to images or other resources; no problem, you simply add conditions (RewiteCond statements) for those.

Finally, you probably noticed that the rewrites themselves can be compressed into a single rule, well spotted! So at the end of the day, the whole lot is simply..

RewriteEngine on 
RewriteCond %{REQUEST_URI} !index.php [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|png) [NC]
RewriteRule ^(en/)?(.*) $1index.php?p=$2 [NC,L]

;o)

ps. If index.php is the only .php file on-site, you could roll that extension into the second RewriteCond and do the whole lot in two lines.


Jamie - 16.11.07 2:04 pm

Wow... *silence*

Honestly I didn't expect a response to my question, I'm very suprised, this is great! :-D

First of all: yes I do suck in writing code (but I try)!
Second, lets comment back on some things you said:

The RewriteOptions MaxRedirects=15 line I just put in because my apache error log told me so. I now realise it's rubbish.

The structure of my page:
/index.php (a page where people can select their language (dutch or english).
/index1.php (the site in dutch)
en/index.php (the site in english)

That's the reason I have an index1.php. May not look nice but if the rewrites work my visitors won't even see it.

Other thing: I now really get why not to use -f or -d.

And no, I didn't notice the rewrites to be compressed to a single rule.

Well, did it work for me? Unfortunately no...

I now have this:
RewriteEngine on 
RewriteCond %{REQUEST_URI} !index.php [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|png) [NC]
RewriteCond %{REQUEST_URI} !\.(css) [NC]
RewriteRule ^(en/)?(.*) $1index.php?p=$2 [NC,L]
And it works fine for the english pages but not for the dutch. When trying to visit i.e. www.myexample.com/audio it puts me back to www.myexample.com/

What have I missed here?



corz - 16.11.07 3:02 pm

Ahhh.. What's missed? Indeed! .. STRUCTURE. You used the word yourself.

Yours is illogical; site structure, that is. I didn't know about the audio/ folder, nor any of the other goodies you might have in there. Of course there's almost always a rewrite solution, but as you soldier on, things could get more and more complicated for you. The real solution.. Fix the Structure.

If your site is in Dutch and English, why not have two folders, en/ for English, and nl/ for Dutch? If there's some Nationalism involved, forget it! We're all the same! And anyway, English r00lz! smiley for :evil:

With two folders side-by-side, life is *much* simpler. It means you can get rid of that index1.php (which still looks so wrong! hey! The webmaster will see it!). You can then create other folders in the root for images/, css/, whatever/, safe in the knowledge that won't be affected by your rewrite rules covering en/ and nl/. It will also make it easier to add other languages later on, if you so desire.

With good structure, your rewriting task is made easy..
RewriteCond %{REQUEST_URI} !index.php [nc]
RewriteRule ^(en|nl)/(.*) $1/index.php?p=$2 [nc,l]

Finally.. Ahhhhhhh!! This is for LANGUAGES!!!!!! smiley for :aargh:

Are you aware of Apache's content negotiation capabilities?

;o)



Fred - 18.12.07 2:34 pm

Hi!

I wonder, is it possible to rewrite the url of a page that has an iframe on it to the url of the iframe content?

In other words, I have this page: index.php?template=forum on it there is an iframe that is wraping a forum, so when the user browses the site the index.php?template=forum url doesnt changes according to the forum contents... of course... my idea is to replace the index.php?template=forum by the forum urls.

Is it possible to do this in the .htaccess file?

Thanks in advance smiley for :)

Fred


sex hikayeleri - 21.12.07 2:28 pm

thanks a lot.


John Norwegian - 22.12.07 12:25 pm

Hey sir!

Wonderfull reading, ive been rewriting for ages but never been hailing the creator of the mod_rewrite.

Do we have any statistics on how much all the rewriting we do slows the processing time down?

Anyways, i was wondering, and im positive ive seen this in some scripts, here goes:

My domain: test.com

On my domain test.com i have a static URL (in a php page):
http://www.bingo-bango-bongo.com/man/woman/redir.php?=http://www.wierd.com

How would i go on about rewriting this to the following URL:

http://www.test.com/redirect.php?http://www.wierd.com

?


smiley for :geek:


Rahul Roy - 27.12.07 12:43 pm

I just want to redirect my webdevelopmentexpert .com to http://www. webdevelopmentexpert .com using .htaccess. Please tell me the proper procedure.


kuku - 03.01.08 1:29 pm

Really good article. Thank you very much.

But i have two problems. i use below code to rewrite url. it is working fine.

RewriteEngine On
RewriteRule ^(.*)-(.*).htm siteview.php?pid=$1

original Url - www.example.com/siteview.php?pid=39&title=search%20engine%20optimisation

after rewrite - www.example.com/39-searchengineoptimisation.htm

Problems ------
1. How can i take off page ID ?
2. How can i use -, between two words(%20). like this -

www.example.com/search-engine-optimisation.htm

Thank you




Jerry - 14.01.08 9:51 pm

Hi,

I am trying to do a redirect from all index files to the root because of search engines and "duplicate content" as they call it even though it is a same page.

I have URLs like this

http://www.mysite.com/2007/12/index.htm

and and I want to redirect that to

http://www.mysite.com/2007/12/

(yup, just kill that index.htm)

But it just won't work.

I tried this:
RewriteRule (.*)/index\.html?$ $1/ [R=301,L]
and it errors
Same with this:
RewriteRule ^(.*)index\.htm$ http://www.mysite.com/$1 [R=301,L] 
Rewrite engine is on.
Any suggestion would be greatly appreciated.


rookie - 21.01.08 7:52 pm

Hi,
I have to put the .htaccess file at my domain root. Say suppose I want to put the .htaccess file in c:\a\b\c\abc.war and I installed Apache at "C:\Program Files\Apache Group\Apache2".

So what will be the setting I have to do to make Apache to understand that it has to refer .htaccess file in c:\a\b\c\abc.war and not the Default one that it uses.

I have made some changes like

AllowOverride All


in httpd.conf file to make it work.

I also put the following code in .htaccess file:

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{REQUEST_URI} ^/abc(.*)$
RewriteRule ^(this¦that¦other¦whatever)/(.*)$ http://www.abc.com/$1 [R=301,L]


But still I am not able to see the effect for this smiley for :roll:

I have gone through the whole article part I & II but I was not able to find any thing that tell the setting/Steps to be made at Apache Side.

Any Comments/help would be really helpful

Thanks


Alexandre - 23.01.08 12:17 am

thanks a lot Cor your article was super helpful!


Alexandre - 23.01.08 5:48 pm

there is one thing that remains unclear even after reading your article (which otherwise clarified most of the htaccess questions i had, thank you for that). I'd call it "htaccess subfolder inheritance":

Reading your article i understood that an .htaccess in the root folder will affect all its subfolder, unless overwritten by subfolder-specific .htaccess files. Yet that does not work for me so far.

Is this correct or not? Does it need a special setting so that it works this way?

just in case, here is my htaccess:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^projects/([a-za-z]+)/([a-za-z]+)/([a-za-z]+)/([a-za-z]+)$ projects/$1/$2/$3/$4/ [r]
RewriteRule ^projects/([a-za-z]+)/([a-za-z]+)/([a-za-z]+)/([a-za-z]+)/$ app.php?section=$1&sortBy=$2&dsAnchor=$3&item=$4


it returns "The requested URL /projects/time/2006/touch was not found on this server." whereas it should have added a slash and then redirected to app.php?section=projects&sortBy=time&dsAnchor=2006&item=touch

What i'm doing wrong?


MUSTAFA BASARAN - 24.01.08 8:27 am

thanks for more information..


Faheem - 24.01.08 10:05 pm

Hi Guys,

I have a simple condition to apply but its getting most complicated to me..

I want to redirect any user that comes to any sub folder of my site to a specific location with the name of that sub folder..
it works like that

RewriteRule ^([^/]+).com/([^/]+).php /website/$2.php?user=$1&file=$2 [L,QSA]

the prob is to make above statement work I have to brose it like that
http://mysite.com/user.com/index.php

the thing is that I want to remove (.com) from there
it should be http://mysite.com/user/index.php

and i need to get values like this..
http://mysite.com/webite/index.php?user=user&file=index

I tried it this way.
RewriteRule ^([^/]+)/([^/]+).php /website/$2.php?user=$1&file=$2 [L,QSA]

but this gives me a 500 error.

can anyone help please ?

also what if I want to use user as a sub domain ?
http://user.mysite.com/index.php
and this one should read as
http://mysite.com/webite/index.php?user=user&file=index

Please do help.. my whole project is depending on it..

Thanks in advance



Monika - 26.01.08 1:49 am

Great read, I've tried a few other resources/books to understand this topic. They didn't work for me. You not only managed to make it clear but also very funny. Thank you!


Losh - 29.01.08 6:38 am

Great tutorial, i finaly understand this whole Rewrite thing. Thanks!


Moaku - 01.02.08 8:44 am

Hi guys ! I've found many usefull informations that I could use later inthere but it doesn't solve the problem I was googling for...

I'm currently not using mod_rewrite and use an htaccess that redirects the 404 error to / so it points to index.php, my default site parser.

In the whole site from now, i'm using a basic get syntax so the url looks like "/?p=home".
For conveniance, I want some pages accessible via an easier URL, so I made a parser that can parse URLs detected in the "easy" way like "/wiki/article" for example... and makes it compatible with my $_GET based syntax internally, so both URLs types works (so, when /xx/yy is called, it triggers an error 404 which redirects to the main script that internally converts the URL).
All works just fine at displaying phase, but at processing phase, I quickly noticed that $_POST was dropped in the process. I googled and found no way to enforce the $_POST redirection.

After reading this page, I was wondering if some url rewrites with mod_rewrite could do the trick but I'm not familiar enough with regexps yet.

Or better... a way to toggle an "AlsoTransmitPostDataOnPermanentLinks" variable that I could have missed in php.ini ?

Well, any advice would be welcome smiley for :)


Harel - 11.02.08 10:15 pm

I have a problem I hope you can help me?

I have my WordPress (2.3.3) installed in A "WP_blog" directory.
For some reason when I click on tags on a post it adds the "WP_blog" directory and the index.php to the url and the results are lost.

Example: http://myexample.com/wp_blog/index.php?tag=sometag
If I delete (manually)the "/WP_blog/" part of the url the link is working fine.
So my question is how can I redirect the url results to http://myexample.com/tag/sometag ?

btw: I have two wordpress installation on the other one its working just fine.

Thank you
Harel


JohnnyTwoShoes - 14.02.08 7:33 pm

Hey man, thanks for the advice! Nice write-up.smiley for :D


corz - 26.02.08 7:33 am

Sheesh! I can't leave this page for five minutes! smiley for :lol:

Thanks for the flowers, guys! There's hope for the human race, after all!

Fred, yes, but I don't think that's what you want.

John Norwegian, the speed penalty is minimal. Unless you are running some HUGE volumes, or a slow server, I wouldn't give it a second thought. As to your question, why not simply change the URL?

Rahul Roy, no.

kuku, 1: if the target page doesn't require it, simply ignore it. 2: Exactly the same way you did with one, except add another bit..
RewriteRule ^(.*)-(.*)-(.*).htm siteview.php?pid=$1
Though I'd probably recommend (.+), which is safer (read: less "hungry"), or perhaps ([^-]+), if you are certain of the number of parts between the dashes in the URL.

Jerry, I do exactly this here at the org, and it goes something like..

RewriteRule ^(.*)/index.htm(l)? /$1/ [r,nc]
I say "something", because I use this only for directory listing readme.htm(l) files. Using .htaccess for index files is certainly doable, but if you have any ($POST) forms, you will probably run into troubles, and so instead I cooked up this..

<?php
if (substr($_SERVER['request_uri'], -9) == 'index.php' and empty($_POST)) {
    
header('Location: http://'.$_SERVER['http_host'].str_replace('index.php'''$_SERVER['request_uri']));
}

Which I insert into the top of my php init file (called at the start of most of the pages here). Works great.

rookie, it sounds like you simply need to specify a new DocumentRoot, or else create a virtual domain for that folder. Not really an .htaccess issue; more an Apache setup issue.

By the way, I notice "¦" characters in your code (pinching code from Webmasterworld?), remember to switch those for proper pipes "|" in any real .htaccess code.

Alexandre, your first rule is catching the request. To avoid this, use something like ([a-z^/]+) at the end of the first rule.

By the way, (a-za-z) is exactly the same as (a-z). Did you perhaps mean to do (a-zA-Z)?

Faheem, really, your whole project depends on this? Man! You're screwed! But if this is true, it definitely sounds like I should be charging for any reply! smiley for :lol:

Anyway, first realize that the TLD isn't part of RewriteRule matching (it's only looking at the part after the TLD). If you want to capture that, you will need to use RewriteCond %{HTTP_HOST}. More details above.

Then switch out those silly ([^/]+) for plain old (.+) and try again. Better?

Lastly, the &file= sections are redundant. You have already established what the page is. Why repeat it?

Monika, I'm toying with the idea of a new front page that uses clever Javascript to randomly pop up large-text versions of some of my best thank-you's, perhaps following the users mouse, or something. Yours will be there, no doubt! Quite possibly Losh's, too. Terse; I like it!

Moaku, as far as I know, there's no way to keep $POST variables alive during a redirect. You might want to consider converting them to $GET variables (as part of "the main script that internally converts the URL"). When it's all done, you could convert them back.

They could also be stored elsewhere. It sounds like you are doing some server-side scripting there, so perhaps you could grab them, and store them in a file/session variable/etc.

Having said that; I'm curious as to how $POST variables could be transmitted to a non-existent page. To me, the whole problem seems effete.

Harel, I'm not familiar with WordPress, but it sounds like a WordPress misconfiguration - try their forum. If you're sure it's .htaccess troubles, come back and post the actual .htaccess code.

*phew*

;o)

ps. if your comment was deleted, I recommend you read the HUGE notice above this comment facility before trying again!


Stefan - 28.02.08 3:55 pm

Thanks for this nice overview.

I have probably a simple question :-) hopfully.

I have an index.php which has a forward to a page.php its a kind of pre page for the cms for intros and stuff like this.

Now I want that the page.php will shown up like:

www.mysite.de/page.php

it should be just:


www.mysite.de/


Please can you help me?




corz - 28.02.08 4:28 pm

Yes, and without rewriting (what this article is about)
simply do..

DirectoryIndex page.php

;o)


Stefan - 28.02.08 8:42 pm

Thank you very much. Still I found some very interesting aspects on this site. But you are right I was looking for this.

Stefan


Neal - 29.02.08 4:36 am

There is a lot of very helpful information on this site. Thank you for making it available. I added the mod rewrite to part of my site where when you enter an addon.example.com it takes you directly to addon.com. My question is: How do search engines see this? Do they see addon.example.com or addon.com?

Thanks


corz - 29.02.08 9:35 am

Yes Stefan, there's lots of useful info kicking around. What you were after probably should have been added to part one (the first page of the series) a long time ago; I've had more than one question about it.

Neal, Google sees what browsers see (except as one long stream of text, as opposed to "seeing", as such). If the domain is ONLY available as addon.com, then that's all Google will see. However, if it's ALSO available as addon.example.com, then Google will see that, too. Simply ensure that's not possible.

As I like to say, "Google sees all".

;o)


Neal - 29.02.08 12:58 pm

I have the addon.com working as addon.example.com or example.com/addon, but when i try to go to a page as addon.com/page.html I cannot pull up example.com/addon/page.html. It cannot find the page.


corz - 29.02.08 3:17 pm

And I cannot find my glasses!
Thing is, I don't even WEAR glasses!

;o)


Neal - 01.03.08 12:55 am

So there is no way to have that kind of functionality?


corz - 01.03.08 11:44 am

On the contrary, it's very doable, and quite common, too.

;o)

ps. I am deleting the comments below this as I type this addendum. Readers of this page are not interested in your tech diary, or rather, diahorrea. After you have read the article, the important, and rather BIG notice above the comments, and searched through all the comments themselves, and still haven't got your solution; feel free to come back with an ACTUAL question, providing all the relevant data, and having already done all the relevant leg-work, yourself.


berny - 01.03.08 5:50 pm

hi cor!

after hours of googling, i didn't figure out a very strange mod_rewrite problem. - then i ended up with your well-written article, but still no solution. - maybe you could help me...

here's an excerpt of my .htaccess file. - everythings' redirects as desired (tested on several browsers on os). - nevertheless, google doesn't index anything. - same problem, if you check my page with the w3c validator. - it gives an 404 error (an example link: http://www.neotrinity.at/photos/album/wok-tum/)

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# BEGIN FAlbum
RewriteRule ^photos/([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?$ /wp-content/themes/connections/photos.php?$1=$2&$3=$4&$5=$6&$7=$8 [L]
# END FAlbum

# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

</IfModule>


thanks much in advance.
berny


corz - 02.03.08 8:36 am

I suspect the trouble you are having is that (older) HTTP/1.0 requests are failing, whereas normal HTTP/1.1 requests succeed (usually caused by HOST_NAME based redirects - possibly your vhost setup). You can test this with a tool like Sam Spade, get it to send one of each kind of request. However, as far as I know, most all browsers send HTTP/1.1 requests, so it's not really an issue.

I have heard folk complaining about certain WP plug-ins sending the wrong headers, both HTTP/1.0/1.1 mix-ups as well as incorrect 404 headers; but my knowledge of WP is almost non-existent, so I wouldn't hazard a guess at any tweaks, even if they were necessary.

Not validating? Perhaps, when accessed directly, the W3C validator sends an HTTP 1.0 request. I do know that if I use the link from Firefox's HTML Validator (Tidy) icon, W3C validation works just fine for your /photos/album/wok-tum/ page - W3C says it is 100% XHTML 1.0 Transitional (the plug-in actually uploads the browser-rendered source). Try that.

Not indexed? Google uses both HTTP/1.0 and HTTP/1.1, I think; but for sure, your pages ARE being indexed, see here Cor do a search like this). Whether they are ranking for terms you'd like them to, is another matter, entirely in the realms of SEO.

At the end of the day, your WP install/gallery relies on certain .htaccess code being as it is, and it's probably best not to mess about with it too much. Your site works fine, and any anxiety is over something most of us are blissfully unaware of, and even if we were aware, would probably take for granted.

In other words, you have nothing to worry about.

;o)


berny - 02.03.08 11:22 am

hi cor!

thanks for your detailled reply. - you are right, that my photo-root page gets indexed, but all subpages like albums and photos are left out. that's what happens with other pages, which i redirect this way as well.

anyway, you second my thoughts and i will check wordpress (and plugins) as well as contact my webhost. thanks once more.

berny


berny - 02.03.08 1:16 pm

me again. - i solved the problem. - for anybody, who uses wordpress and calls plugins external via including the wp_blog_header (which you need to execute database calls, embed the theme, ...): just manually send a HTTP/1.1 200 OK Header to overwrite the 404 header which has been set by wordpress itself. so, cor, you were right, it had nothing to do with the mod_rewrite.

maybe their is a wordpress function out there, which one can use to tell wordpress, that the 'external plugin' has been executed correctly without changing the core. - i have a look on that later.

berny


corz - 03.03.08 4:06 pm

Ahh yes.. WP plugins! Pesky buggers!

I dunno about being right, as such; but I'm glad my clues were useful to you. Two heads, and all..

At least you have everything working now; that's what matters. And some handy info here for anyone else that might wander by with a similar issue. Cheers!

;o)


Franz - 05.03.08 11:36 pm

Thanks a lot, this tutorial is great. I was looking for something that makes:

url.com/param1/attribute1/param2/attribute2/param3/attribute3/param4/attribute4 and so on

i know how i would do that, but only for known amount of parameters and attributes. i want that to be flexible. for none, one or fifty!

keep up your good work, man!

greets from Germany
Franz


kai - 06.03.08 7:28 am

hey man, it's kai from 15.02.06. just dropping by to look for the solution you posted from 2 years ago (time flies). nice to see that you are still helping many!
cheers smiley for :cool:


corz - 06.03.08 11:19 am

Well, kai, I help some. Some are beyond help!

Hey Franz, look up the comments a bit, you'll see a rule that does pretty much exactly what you want. Something like (the mydir/ is optional - if you need this rule to cover *all* requests, simply omit that part)..
RewriteRule ^mydir/([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?$ /foo.php?$1=$2&$3=$4&$5=$6&$7=$8 [l]
Technically, if you requested, say, http://example.com/mydir/foo/bar/, you would get a query string something like foo=bar&=&=&=, but as the extra (empty) variables are ignored, your script (in this case, foo.php) would receive a single $_GET variable..
[foo] => bar
You could conceivably code in hundreds of "spaces". You can also do it with or without the "=", and create your own variable names in the target URI, ie..
... /foo.php?var1=$1&var2=$2&var3=$3 etc.
Pretty handy.

;o)


Derrick - 06.03.08 6:23 pm

Hi,

I am trying to take advantage of a bunch of old links that were populated on the search engines from our OLD MIVA ecommerce site using .htaccess rewrite rules. All are working well, except for the following:

http://www.foobar.com/mm5/merchant.mvc?Screen=PROD&Product_Code=ABC123&Category_Code=R1

I am trying to redirect to:
http://www.foobar.com/ABC123

My redirect rule keeps getting lost at the ? following the merchant.mvc as I "think" it is interpreting the ? as a rewrite separator...

Is there a way to escape the ?

Here is what I have tried unsuccessfully:
RewriteRule ^mm5/merchant.mvc?Screen=PROD&Product_Code=?ABC123?$ http://www.foobar.com/$2

Thanks for any insight you can provide!



Derrick - 07.03.08 1:46 am

I realized my understanding of the ? was incorrect. It was the regex ? that confirms 0 or 1 instance of the previous character. No chance of working!

I ended up going down a slightly different path, but figured out where most of the issue is. I had to use %{QUERY_STRING} to get the entire line including the parameters that were being passed after the ?.

I converted to using a couple of RewriteCond's and a RewriteRule instead:
RewriteCond %{REQUEST_URI} ^/mm5/merchant.mvc$ [NC]
RewriteCond %{QUERY_STRING} ^Screen=PROD&Product_Code=ABC123$
RewriteRule ^.*$ http://foobar.com/ABC123?

I don't have the Product_Code generalized yet, so I have coded the couple of entries I am concerned about for now and will pass as variables when I figure it out (hopefully tomorrow).

-Derrick


corz - 07.03.08 7:46 am

Hi Derrick.

Your first mistake; and it's a most common one; is that you are conceptualizing the rules back-to-front. You want to redirect FROM  http://www.foobar.com/ABC123, in other words; from the short URL (which the user types into their browser) to the long URL (in the background, so the user doesn't see it).

This might seem like a trivial distinction, but is the root cause of a great many rewrite problems. If what I think you are trying to do is correct (i.e. create "short" aka. "flat", aka. "friendly" URLs), you need only do something like..
RewriteCond %{REQUEST_URI} !mm5/merchant.mvc [nc]
RewriteCond %{REQUEST_URI} ^/(.*)$ [nc]
RewriteRule ^(.*)$ mm5/merchant.mvc?Screen=PROD&Product_Code=%1&Category_Code=R 
Which will work great, so long as ALL your links are for Screen=PROD, and Catagory_Code=R; i.e. these two variables are fixed. You can probably see what's going on; all requests NOT for mm5/merchant.mvc (i.e. all short URLs) are redirected to mm5/merchant.mvc using the product code %1, which we capture in the RewriteCond. In our example URL, that would be ABC123.

Remember, there is no QUERY_STRING in the original (flat) URL, so you can't use it in RewriteCond statements; the whole point of flat URLs is to NOT use a query string! QUERY_STRING only exists after we create it, with our RewriteRule.

For greater flexibility, you will probably want to incorporate these other variables into your short URL, which could instead look something like.. http://www.foobar.com/PROD/R/ABC123 (though maybe small case, huh?)

The rules for this aren't much more complex..
RewriteCond %{REQUEST_URI} !mm5/merchant.mvc [nc]
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^/]*)/([^/]*)$ [nc]
RewriteRule ^(.*)$ mm5/merchant.mvc?Screen=%1&Product_Code=%3&Category_Code=%2
Here we capture all three variables (which must be supplied in the correct order in the short URL), and use the captured variables, %1, %2, and %3 to build the query string in our target URL (in no particular order - so long as the target script (merchant.mvc) receives the three variables, it won't care about the order in which they arrive).

I don't know how Miva works (or even what it is!), so perhaps Screen and Catagory_Code are permanently fixed, and the first set of rules is all you need, but I figured it was a good opportunity to explain how to take it a step further, if required.

for now..

;o)


Richard - 07.03.08 10:48 pm

Hi there, this is a great tutorial and has helped me a lot. I have been looking for information like this to help me with a problem on my site.

I already have a gaming section on my site at www.example.com/gamer but I am now creating a library of game reviews. The game review URLs currently look like this because of the plugin I am using to create them:

http://example.com/index.php?now_reading_author=psp&now_reading_title=exit

In this example, it is a PSP game called Exit.

I want my URLs to look something like this instead:

example.com/gamer/game/psp/exit

I have added the /game/ extra level to make sure I don't bump into any of the existing /gamer URLs.

No matter what I do I get an Internal Server Error, can you give me any advice?


corz - 07.03.08 11:54 pm

A 500 error means there's an error in your .htaccess code.
My advice would be to fix that error.

If you can't spot it, post the actual code you are using; perhaps I can help you pin it down.

;o)


imachine - 17.03.08 12:48 am

How can I use mod_rewrite to achieve the urls similar to stumbleupon

http://joe.stumbleupon.com

I pretty much would like to rewrite http://www.mysite.com/mypage?user=sarah to http://sarah.mysite.com

Any help would be greatly appreciated. Thanks.


corz - 17.03.08 3:14 pm

imachine, firstly, you will need to have wildcard subdomains set up for your domain. That's the most important thing, or else requests for whatever.tld.com will never reach tld.com in the first place.

After that, it's fairly straightforward rewriting, and I'm fairly sure everything you need is already here. Go for it!

If you have any problems with the .htaccess code itself, drop it here, and I'll have a look at it with you.

;o)


bill - 17.03.08 5:36 pm

Great resource!

I have a question about something that seems simple, but cant find any info at all.

What if you want to have your page read /blog/ instead of /blog.php or /blog


What would you need to put into .htaccess to get that ending /


Thanks for the great article!


matt - 19.03.08 5:18 pm

Hello.

I'm having a problem that I can't figure out. I started using mod rewrite several months ago with this ...

Options +FollowSymLinks
RewriteEngine On
RewriteBase /encoder-repair/
RewriteRule ^([^/]*)\.html$ mfr.php?aid=$1 [l] 


This rule worked fine for about a month. Then I added a second rule producing this ...

Options +FollowSymLinks
RewriteEngine On
RewriteBase /encoder-repair/
RewriteRule ^([^/]*)\.html$ mfr.php?aid=$1 [l]
RewriteRule ^([^/]*)/([^/]*)\.html$ model.php?mfr=$1&model=$2 [l]


Everything went fine, no errors when I access the site, no complaints from customers.

Two months later I check my Google Sitemaps account and see many "500 Internal Server Errors". I go back through my logs and see the errors started the day after I added the second rule, and, its only happening on web crawlers. There are successful Googlebot crawls, and ones that get a 500-error. Same with Yahoo Slurp.

Please help. I can't reproduce the error. I make a change then wait for the host activity logs to see any improvements. So far I'm striking out!!



corz - 19.03.08 7:13 pm

bill, simply put your blog into a folder called blog, and have its main page as index.php, index.htm, or index.html. Then Apache should take care of the rest. If that's not possible for some reason, and you still need help, provide more details about what your blog page is.

matt, is that RewriteBase really necessary? Generally speaking, mere mortals don't need it, and if they do; it's usually a sign of trouble elsewhere.

Without seeing your error logs, it's difficult to hazard any more guesses. Have you looked at your error log? Knowing exactly what requests throw up the 500 error will be of great help to you. Not the access log, the error log.

;o)


Rob - 01.04.08 5:44 am

Cor

Thanks I have read this and all the comments its been very helpful so far. But I have a problem my url is

www.example.com/artist-listing.php?artist=Maurice%20de%20Vlaminck

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /artist-listing.php?artist=$1

But this gives me a URL of

http://www.example.com/Maurice de Vlaminck.html

When what I really need is
http://www.example.com/Maurice-de-Vlaminck.html

I promise I have read your article and I am sure the answer is there I'm just to new at this to work it out

Thanks
Rob


corz - 01.04.08 10:19 pm

What I think you are trying to do (given the general gist of your post1, and the current linking system of your site2) is have URLs with dashes, e.g. Maurice-de-Vlaminck.html transparently converted to URLs with query string with spaces in them, e.g. artist-listing.php?artist=Maurice de Vlaminck.

Two things concern me about this. Firstly, that artist-listing.php would prefer spaces to dashes; the former creates headaches; and secondly, that your back-end can create these friendly, flat .html links in the first place.

However, I'll assume that you have these things covered, and suggest you do something like (which would convert links with two dashes)..

RewriteRule ([^/]*)-([^/]*)-([^/]*)\.html$  /artist-listing.php?artist=$1\ $2\ $3 [L]

If you need to capture more or less dashes, put rules above (4, 3, etc.), and below (1) to catch the other possibilities. There may be a more clever technique, but my brain is in recovery today, sluggish.

If I've misunderstood entirely, ask again, being specific about the precise link the user will see and click on the page, and in their browser address bar; and exactly what URI you want your handler to accept; and I'll have another look.

;o)

references:
1 note: I genericized the last URL in your post
2 Which is actually mixed - some of your links have dashes, some spaces, so I'm not 100% about which variant is your preferred target. Most had spaces, though, so I've assumed this for my response. I have also assumed you required the [^/] conditional for some future purpose, and used that, too.



Rob - 02.04.08 12:46 pm

Cor

My current file and database works using this url

www.example.com/artist-listing.php?artist=Maurice%20de%20Vlaminck as the artist field is populated with spaces between the names in the database.

What I want to be able to do is have a link thai looks like this

http://www.example.com/Maurice-de-Vlaminck.html

convert to the original link so that it searches the database correctly.

So users will see http://www.example.com/Maurice-de-Vlaminck.html but it will convert to www.example.com/artist-listing.php?artist=Maurice%20de%20Vlaminck server side

I am hoping this will make my links more search engine friendly.

http://www.example.com/Maurice de Vlaminck.html will not work well as a link for the search engines as I understand it.

Thank you again for any help

Rob




fahim - 02.04.08 9:31 pm

Great article! Thanks!


corz - 03.04.08 11:32 am

Rob, that's what I assumed for my solution! The code I gave you will do exactly that!

All you need to do now, is have your back-end create links with dashes. Can it do that?

As to the SEO aspect, Google doesn't care either way. Dashes have pretty much always been interpreted as spaces, spaces obviously always are, and now even underscores are seen the same way.

It's six and half a dozen - do whatever feels comfortable for you.

;o)


Rob - 04.04.08 5:08 am

Hi Cor

Will use this in the PHP to change dynamic links

<?php  $artist=ereg_replace" ""%20"$artist );

?>

Thanks again

Rob


corz - 04.04.08 5:12 pm

Rob, that seems a tad wonky, but I'm sure you know you're back-end better than I do!

All the best!

;o)


Ros - 10.04.08 9:28 pm

Hi,

I created a simple code to urls from domain(mydomain.net)
to a subdomain.(blog.mydomain.net)- which is a wordpress blog.

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www.mydomain.net [nc]
rewriterule ^(.*)$ http://mydomain.net/$1 [r=301,nc]
rewritecond %{http_host} ^mydomain.net [nc]
rewriterule ^(.*)$ http://blog.mydomain.net/$1 [r=301,nc]

I'm having trouble getting stats data.
The 'stats' folder and url(domain.net/stats) that is on the main domain. is unable to get accessed

Is there anyway not to redirect the stats?
I'm a newbie to all this coding. your advise and help is most appreciated!
Thanks in advance!


corz - 11.04.08 5:37 am

Hi Ros.

The problem is that all requests are being rewritten to http://blog.mydomain.net. You second line should probably read..

RewriteRule ^(.*)$ http://mydomain.net/$1 [R=301,NC,L]

Note the extra "L" in the flags. I say "probably" because I'm confused as to why you would first remove the "www" part, and then rewrite all non "www" URLs to http://blog.mydomain.net Is this really what you want?

Anyway, if you add the "L" to the flags, a request for http://www.mydomain.net/stats will be rewritten to http://mydomain.net/stats and stop there (L meaning "Last Rule", or "do no more rewriting"), and you will see your stats.

I suggest you have another look at the whole thing. As it stands, in order to access the main domain, you have to add "www", which is then removed. If a user then bookmarks this new URL (with no "www"), they won't get back into the main site, but will instead be redirected to your blog!

Again I say, are you sure this is what you want?

;o)


Lucie - 14.04.08 6:33 pm

Nice tutorial, Thanks dude smiley for :)


Emil - 14.04.08 8:20 pm

Hi everybody.
I have a website in php(smarty, FCKeditor) and everything works fine on localhost(using XAMPP, folder htdocs). I create a folder newsite under htdocs and move all content to this folder and now is not working anymore(I'm doing this because I want to have more websites on the same host...so I need a folder for each website). I'm spending too many days looking for an answer and I hope you guys can help me. Please help me with suggestions.
Thanks in advance and this site is great.
htaccess:

php_value display_startup_errors On
php_value register_globals 0
php_value magic_quotes_gpc 0
RewriteEngine On
#flash xml:
RewriteRule header.xml index.php?vars=flash_xml
#general rewrite rule:
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_URI} !^/include
RewriteRule ^(.+)\.html$ index.php?vars=$1 [L]



corz - 15.04.08 12:57 am

Emil, firstly, posting the same question on multiple pages isn't going to increase the likelihood of getting an answer; quite the opposite, in fact. It took sheer willpower not to delete this one, too.

And as far as I know, "not working anymore" is not an Apache error message. Is that from your error logs? Have you even looked at them? That's where you start when things break down, you know. You knew about the error logs, right? Actually, start here. THEN read your logs, and get the information you need to ask a decent question.

Multiple sites on one host. You mean Virtual Hosts? If you don't, you'd better head along here, and make some.

And this "htaccess:" you posted, is that in the root? Or the sub-folder? Have you tried things without it? In fact, have you done anything at all to troubleshoot this? You didn't say.

Lastly, perhaps you noticed the huge notes above these comments, entitled "Before you ask a question..". The idea is that you read that before you post. Skipping over text is probably how you've managed to waste days tackling a single tech problem. This isn't brain surgery, but you gotta RTFM.

A few suggestions for you!

;o)


Ros - 15.04.08 10:14 pm

Hi Cor,
Thanks for the advise.

I see what you mean.
I want to just rewrite www.domainname.com to blog.domainname.com and not have any requests for wwww.domainname.com/stats.

Will add L to flags so essentially I just need to have:
rewritecond %{http_host} ^www.mydomain.net [nc]
rewriterule ^(.*)$ http://blog.mydomain.net/$1 [r=301,nc,L]

correct?
Appreciate your advise!
Rosa


AbdulGafoor - 16.04.08 6:24 am

Very useful information for web developers.


corz - 16.04.08 6:53 am

Yup, Rosa, that looks fine.

Requests for http://mydomain.net and http://mydomain.net/stats/ will pass through unmolested, but requests for http://www.mydomain.net/anything  will be redirected to your blog.

;o)


SoupyC - 17.04.08 6:49 pm

Excellent explanations of htaccess, especially to someone like me who hasn't a clue! My problem is...

I have a domain, say abc.com

I also have 2 sub-domains, say sub.abc.com and sub2.abc.com

I want www.abc.com to re-direct to sub.abc.com but I want sub2.abc.com to be handled as normal.

When I try to write the htaccess file, I can get www.abc.com to re-direct to sub.abc.com, but when I try sub2.abc.com it seems to re-direct to sub.abc.com/sub2.abc.com

Please help!

I'm currently using this...

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www.abc.com [nc]
rewriterule ^(.*)$ http://sub.abc.com/$1 [r=301,nc]
rewritecond %{http_host} ^abc.com [nc]
rewriterule ^(.*)$ http://abc.com/$1 [r=301,nc]


Thanks,

SoupyC


corz - 18.04.08 1:01 am

SoupyC, a single ruleset should be enough..

RewriteCond %{http_host} ^www.abc.com [nc]
RewriteRule ^(.*)$ http://sub.abc.com/$1 [R=301,L]


No? Also note the "L" flag, so no further rules are processed. Your second ruleset is surely a big typo, anyway.

;o)


SoupyC - 18.04.08 11:17 am

Thanks for the reply Cor. Logic would tell me that what you said should work, and I had previously tried that, based on what your actual article had said, however, maybe I didn't define my problem well enough.

On my hosting, I have my root folder, where the www.abc.com points to.

Within that, for my subdomains, I have 2 folders /sub.abc.com and /sub2.abc.com

I think it's because these sub-folders are under my root folder, that it's causing the problem...but I can't be sure.

When I apply what you have said in the htaccess file, going to www.abc.com results in a 404 page not found (as there is no index.htm) and sub.abc.com goes to the right place, as does sub2. It appears the .htaccess file makes no difference!

One question though, does my DNS affect this? If I change the .htaccess file but my DNS server has a record in the cache, would it make a difference or does .htaccess come after the DNS?

Many thanks,

SoupyC


corz - 18.04.08 11:37 am

Yes, .htaccess comes way after the DNS. But so long as the name servers are pointing all those domains at your site, you can do what you like after that.

As to your issue, it looks like you actually want to redirect users transparently; so you'll need to remove the R=301 business, and external targets (http://something). As I said in the article, NEVER use R=301 until AFTER you have everything working 100%. Your biggest problem now is going to be that you may fix the issue, and as the 301 is still in effect; you will still be redirected there. You might want to use a new browser for your next set of tests, after you remove the 301, that is.

My examples in the article are what you need..
RewriteCond %{HTTP_HOST} domain-one.com
RewriteCond %{REQUEST_URI} !^/one
RewriteRule ^(.*)$ one/$1 [l]
But actually, I'm not entirely sure, because, like you said, maybe you didn't define your problem well enough.

Think in terms of the browser request, the links on the pages, the real file system paths, and what the user sees in their address bar, e.g..

I want to put links on my site like "www.foo.com" and have them translated in the background to the "bar/" subfolder in the root of my site, while "www.foo.com" stays in the user's address bar.

or whatever. I'm confused by why you want www.abc.com to be redirected to sub.abc.com at all, and yet still access sub.abc.com directly. Are you using the www, or not? If not, why not remove it from the equation altogether. And if you do want it, why are you redirecting those requests to a different host name?

But like I say, I don't entirely understand what you are trying to do.

;o)


SoupyC - 18.04.08 3:55 pm

Cor, thanks once again for your time. I'll try and explain what I want in those terms and hopefully all will make sense.

I do not use www.abc.com at all. I only want those users that happen to type in www.abc.com to be re-directed to sub.abc.com and I'd like sub.abc.com to be in their browser. That is because I do not use www at all and want anyone that happens to type that in, to be redirected to sub.abc.com. For that matter, I would also like that if they typed abc.com in their browser, it too would point to sub.abc.com

In summary:

- User types www.abc.com it redirects to the /sub.abc.com folder and sub.abc.com displays in the address bar
- User types sub.abc.com and hosting provider already handles it being redirected to /sub.abc.com folder so nothing is needed in htacess (as far as I'm aware!)
- User types sub2.abc.com and like above, the hosting provider already handles it being redirected to /sub2.abc.com folder so nothing is needed in htacess for it either.

Does that specify it any better?

Thanks again,

SoupyC


corz - 18.04.08 4:29 pm

Hmm. This takes us right back to my first reply! It's basically a variation of the lose the "WWW" ruleset. And you can easily (theoretically!) redirect www.abc.com and abc.com to sub.abc.com with only this..

RewriteCond %{http_host} ^(www\.)?abc\.com [nc]
RewriteRule ^(.*)$ http://sub.abc.com/$1 [r=301,nc,l]

If that doesn't work, it probably means that the "hosting provider already handles it" is somehow messing with your rewriting. They are probably using mod_rewrite to create these subdomains, but unless we can see exactly what they are doing, it's difficult to know for sure. This may well explain why you are having so much difficulty getting things working.

It might be best to take it up with your system admins. They might even insert your own rewrite rules for you, directly into the vhost setup.

Many hosting panels enable you to do this yourself. I currently use Plesk for corz.org, and it allows you to choose to have domains with or without the www (personally, I have it with with www, to catch incorrectly typed URL's, and then redirect it myself to plain old corz.org with the rules in the article).

By the way, I've just noticed that the [pre] tags convert the flags to lower case. I'll need to look into that - something in the bbcode parser. I meant to use [R=301,NC,L], I prefer uppercase flags these days; more readable. Remember though, no "=301" until after it's working.

Anyways, I'd be interested in hearing how this turns out, along with more details about your hosting setup, type of control panel, etc.

Good luck!

;o)


SoupyC - 18.04.08 5:46 pm

Had you not already guessed I'm a total n00b at this stuff, lol! With that said, I tried putting in the htaccess and it didn't work. I'm now thinking that you saying how my hosting provider is messing with it makes sense!

I'm using GoDaddy as my domain management, and hosting provider...not sure if that answers what hosting panel I'm using. From within there, I set up my subdomains, but can't see any options about www and non-www issues.

At the minute, I can live with how it is, as it's a small blog I'm running but it would just be nice if I could have that flexibility...plus I like to learn new things and this whole process has taught me a lot I didn't know!

It now seems that where the .htaccess file WAS making a difference...it no longer is. When you say not to put in the R=301 until it's working, is that because it leaves a trace in the browser. I'm using Firefox now but I tried changing to IE (last used a couple months ago before I started fiddling with the htaccess) and it produces the same result (i.e. none!)

I'll keep digging and experimenting and let you know how I get on.

Thanks for all your help! smiley for :D

SoupyC


corz - 18.04.08 6:07 pm

Ahhh.. gogaddy! (insert muttering and swearing noises)

Being a n00b isn't a real obstacle, however godaddy hosting might well be. It's not real hosting, at least; I've heard enough horror stories about their .htaccess (and in particular, mod_rewrite) support to know it's not real enough. Basically, it's a "value add-on" for their domain packages. They (wrongly) assume that if you are noobish enough to pick them as your host, you won't have the savvy to even realize things like mod_rewrite exist.

You might want to consider a different host. I don't think mine is taking on new clients right now (it's like a wee family), but I do know that there are lots of really good deals out there, I mean like $10/year. Being small now, doesn't mean you always will be - it's never too early to get a decent web host.

Happy hunting!

;o)

ps. if you need to use IE occasionally, Google: IETab.


Pete - 19.04.08 4:31 am

I goombled across this article and it is excellent - thanks..

I have hit an issue uploading 'generated' HTML files from a Visio chart and finding that it creates "\" not "/" in any links in the diagram. this works in IE, but breaks in firefox, firefox translates it to a "%5C" which breaks the link.

So I need to convert a "\" or a "%5C" into a "/" in the .htaccess, to assist firefox.. any ideas?

Thanks for any help
Pete..


corz - 19.04.08 5:18 am

Weird. I'm guessing Visio is some Microsoft product. smiley for :roll:

Convert backslashes to forward slashes. Hmm. Something like this should work..
RewriteRule ^([^\\]*)\\(.*)$ $1/$2 [NC]
Though without more precise details about the nature of the links, full paths and such, I wouldn't bet money on it! This is a transparent rewrite, so you would still see the %5C in the browser. You could do it externally, too, though I'll leave that as an potentially interesting exercise for you!

;o)


Hikari - 19.04.08 9:56 pm

Hello, very nice tutorial you have here

I'm trying to make a redirection here but I'm not having success at all. I have a wordpress blog and 2 domains, myexample.com and mydomain.net. Currently, mydomain.net is being 301 redirected to myexample.com.

I'd like that both domains could be used to show my blog content. I suppose that could be done with parked domains, but on my host service both domains are set as addon domains. And anyway, wordpress insists to redirect users to its main domain if it is accessed from any other domain.

So, what I wanted to implement with htaccess, is a trick to spoof wordpress, so that users can access from mydomain.net, keep mydomain.net shown on browser address bar, and wordpress thinks it is being accessed from myexample.com.

I tried this
RewriteRule ^(.*)mydomain\.net(.*)$ $1mydomain\.com$2

but it didn't work, it is like wordpress knew it was being accessed from mydomain.net and redirects to myexample.com.

Do you know if this can be done?




corz - 20.04.08 7:47 am

Almost anything can be done, but having two domains display the same content is SES (Search Engine Suicide).

Reconsider.

;o)


SoupyC - 21.04.08 4:03 pm

Cor,

Thanks for the advice. GoDaddy is where I registered my first .com domain, so naturally it's where I registered my first hosting account. That is the only reason, though I will keep your advice in mind when renewal time comes (or before then if I'm getting particularly annoyed!). I already have IETab but wasn't sure how much of IE it uses (or maybe how much of Firefox it uses I suppose!)

I used IETab to view it, and then standalone IE to see if there was any difference. Sadly there was not!

Your site has been added to my bookmarks and I have no doubt I'll be visiting often!

Thanks once again,

SoupyC


corz - 23.04.08 3:21 pm

SoupyC, I registered my first domain with godaddy, too. A mistake*. But I've never had hosting with them. You don't need to host with your domain registrar. Most folk don't.

And IETab uses ALL of IE. Being the system browser, you can embed it inside things, but it's still IE. Lots of Windows web browsers are just IE in a new skin.

See ye around!

;o)

references:
*I eventually switched to namecheap.com - who are superior in every way.



Geeta - 24.04.08 8:17 am

Hello,
I want a redirection such as
I have domain where all the files are kept suppose http://www.abc.com
Now I have another domain say
http://xyz.abc.com/
Now whatI want is when I type another domain name i.e. xyz.abc.com
the files execution should be from abc.com. That means the url should be xyz.abc.com but the internally the abc.com site is shown.
Can we write mod re-write for it?
I tried in manier ways but I could not....
Kindly help me
-Geeta


corz - 24.04.08 12:13 pm

Geeta, what ways did you try, exactly?
(that means show me the code!)

Also, if you want help, you will need to supply all the information needed to help. I would probably have lots of questions, like.. Are the domains hosted on the same host? Are they on the same server? Or are they in the same folder? Or what? Does the DNS point the domains to the same location. Do you own both domains? Do you also want to be able to access abc.com directly?

And so on.

;o)


Minty - 26.04.08 6:29 pm

Okay so here is the problem, in my site the following pages work with clean URLS
http://www.mysite.com/anypage/
http://www.mysite.com/x/0 (used to be a screen like x.php?screen=0)
http://mysite.com/x/series/

But I cannot get the link:
http://mysite.com/x/series/0 (1,2,3 etc)

I have an "avatar" section on my site were people can browse and choose the series they want to look at. However they can't change the pages because I keep on getting errors!
here is my code:


:Options +FollowSymLinks -MultiViews 

RewriteEngine On 


RewriteCond %{SCRIPT_FILENAME} -f [OR] 
RewriteCond %{SCRIPT_FILENAME} -d 
RewriteRule .* - [L] 
RewriteRule x/series/(.*[^/])/$ /x.php?series=$1  [L] 
RewriteRule ^(.+)/([0-9]+)/?$ /$1.php?screen=$2 [L] 
RewriteRule (.*)/(.*)/(.*)/$ /x.php?series=$1&screen=$2 [L] 
RewriteRule ^(.*[^/])/?$ /$1.php [L]  





dMan - 28.04.08 10:51 pm

Hi - Great site!

heres my current setup:
I have a database with sku ids which have corresponding links.
example:
sku id in my database is: google
and its corresponding link is: http://www.google.com

so when a vistor clicks: http://www.mysite.com/go.php?sku=google
they go to: http://www.google.com
-----------------------------

It would really be nice to accomplish the following:

(below is how my links are currently setup)
http://www.mysite.com/go.php?sku=google (goes to) http://www.google.com

(below is what I wish could be done)
http://www.mysite.com/google.html (goes to) http://www.google.com

so basically take the sku id, then put .html on the end and wellah!
-----------------------------

I really hope you can help. But if you can or can't, I'm still grateful for you takin' a look. =)

Thanks again,
D


corz - 29.04.08 11:38 pm

..back from my own tech issues..

Minty! What is THAT!?
The first error probably comes from the colon ":" in front of the Options statement. That's gotta be wrong.

Next are these crazy (.*[^/]) sections. Surely you mean.. ([^/]*) The bracketed conditions replace the "." character in the more usual (.*), you see.

Thanks dMan. Your desire is fairly trivial to achieve, something like this should do the trick..

RewriteRule ^(.+)\.html$ /go.php?sku=$1 [L]

;o)


Flavio - 02.05.08 7:41 am

First of all, congratulations for this great article.

Back to the main theme... I've been wondering if this would be the best solution to deal with validation (W3C) when using PHP based url queries. The use of the ampersand is the main problem because it's a reserved character.
So I was planning something like redirecting the urls through .htaccess manipulation.

Do you believe this is a good solution for this problem?

Thank you,

Flavio


corz - 02.05.08 8:46 am

The best solution would be to use..
&amp;
..in your page links.

;o)


Flavio - 02.05.08 5:47 pm

The thing is I've been a little confused when I read this article: http://www.w3.org/QA/2005/04/php-session.
I used to don't care about web standarts and validation stuff and tableless, but now I'm seening how important is.


corz - 02.05.08 8:03 pm

You're right; all these things are important. And it's good that you care. However, this particular issue isn't of great importance because almost no one uses trans_id anymore, mainly because it's so insecure.

The article in question also gives solutions that you can use in your .htaccess, aka. "Apache directives". i.e..
php_value arg_separator.output "&amp;"
But by far the best solution is to disable trans_id altogether..

php_value session.use_trans_sid 0

or..

php_flag session.use_trans_sid off

which is also shown in the article.

;o)

ps. this now has nothing to do with rewriting, and so should probably be on page one! smiley for ;)


Faisal - 05.05.08 8:35 am

Thanks for sharing your knowledge with us. Here is my question after having done several tries.

This is the URL I want to get translated
www.example.com/index.php?pg=detail&catId=1&catTitle=2

After rewrite, it should look like this
www.example.com/about_us.html
(about_us is the cat title so it will change with every argument)

However, I have achieved the following.

www.example.com/detail/8/about_us.html

I have used the following line of code to get this result.
RewriteRule ^detail/(.*)/(.*)$  /sitewonders/index.php?pg=detail&catId=$1&catTitle=$2 [nc]
 

What my requirement is that I dont want to show detail and 8 whereas detail is the page where my codes are stored and 8 is the product category.I have no clue how to get hold of this. the <a href> code used to call is as follows.

<a href="<?=$webPath?>detail/<?=$viewcat['pk_id']?>/<?=str_replace(' ', '_', $viewcat['cat_Title'])?>.html" class="b">
 


Please give me any clue or help to achieve this target.


Derick - 05.05.08 12:56 pm

Your article was very helpful.

I have a problem of creating url as following

http://www.example.com/sub-category.php?r=37&sr=38 as

http://www.example.com/jouets-1er-age/puericulture.php

jouets-1er-age - means category name that is "r=37" in the url. So I have to get the category name from database

puericulture - means sub category name that is "sr=38" in the url. So I have to get the sub category name from database

can you please explain how should I use mod_rewrite to do that work.

Thanks.


corz - 05.05.08 1:59 pm

Faisal, if the parameters aren't in the original (flat) URL, they won't be available to your script, so you need to have them somewhere. Your rewrite is fine, and the flat link is probably as small as it can be.

Derick, everything you need is in the article. Have a read. If you still can't achieve your goal, get back here and post the actual code that isn't working, and I'll have a look at it.

;o)


Derick - 06.05.08 5:23 am

Thanks cor. I read this article and I have developed the code. But I didn't get the correct file. It redirect to index.php always but the name is correct.

http://www.example.com/sub-category.php?r=37&sr=38 as

http://www.example.com/jouets-1er-age/puericulture.php


-----------------------------------------------------------------------------------------------
My code of .htaccess is;

php_value register_globals off

RewriteEngine on
Options +FollowSymLinks

# RewriteBase /
# Rule for duplicate content removal : www.example.com vs example.com
RewriteCond %{HTTP_HOST} ^ludo [NC]
RewriteRule (.*) http://localhost/ludo/$1 [R=301,L,NC]

#RewriteCond %{REQUEST_URI} ^(/r/sr) [NC,OR] ##optional
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php

Please look at my code help me. I appriciate your kind help.


corz - 06.05.08 8:15 am

Sheesh! Derick, I didn't mean your entire site! <snipped> !

That .htaccess code is a mess. It's like you just threw characters into notepad and hoped for the best. I'm not gonna teach you the basics, dude! I already wrote a big article to do exactly that. Really, have a read, except this time; slowly.

Here's my advice: START AGAIN!

;o)

ps. hints: you only put brackets around characters you want to capture. Look again at my examples in the article, and the many many good examples (my replies) in these comments, and you'll get the idea - there are many examples that are probably exactly what you need. Good luck!


Dman - 06.05.08 9:50 am

Hi Corz,
thank you for helping me in my last post.
It works GREAT! I've read through the comments page and it seems
like mod-rewrite only works with variables and not static filenames.

Here is what I'm trying to do now.
My page has about 6 different category links which go to their individual category page.

These category file names begin with go + (3-5 random numbers) + .html
Below are a few examples to give you a better idea:

go245.html
go1256.html
go15460.html

These are all direct static links which represent specific categories.
And here is what I'm trying to do:

go245.html --> (rewritten to) --> cars.html
go1256.html --> (rewritten to) --> boats.html
go15460.html --> (rewritten to) --> trucks.html

Each link is static with none of that variable or wildcard stuff.
Below was my feeble attempt to make it work and both failed..

RewriteRule cars.html /go245.html [L] (didn't work)
RewriteRule ^cars.html$ go245.html [L] (didn't work)

------------------------------------
you see - because these are static links I dont think using
variables like $1 or ^([^.]*). is going to work in this situation.
but what the heck do I know - I need an expert and
I sure would be grateful for your expertise.

Thanks again,
Dman


corz - 06.05.08 1:09 pm

Dman, mod_rewrite works with URL requests. Any part of it; dynamic parts, static parts, user agents, protocol version, you name it.

Anyway, are you sure this..

go245.html --> (rewritten to) --> cars.html

Isn't meant to be this..

cars.html --> (rewritten to) --> go245.html

That is, the link (or what the user types into their address bar) would be "cars.html", and the real, server-side page would be "go245.html". Right? Back-to-frontness is very common in mod_rewrite questions.

If so, there's no reason why your first example wouldn't work..

RewriteRule cars.html /go245.html [L]

Requests for "cars.html" would be redirected to a file called "go245.html" in the root of your site. Looks fine.

If it doesn't work, then perhaps another rule is catching it before it gets to that one.

;o)


Dman - 07.05.08 9:44 am

thanks Corz,
wow, I think your "right on target" once again. I bet my first example (attempt) wasn't working because of what you said. Because I do have other lines in my .htaccess file. Heres what it looks like:

-----------------------
RewriteEngine On
RewriteRule ^go([^.]*).html /index.php?link_id=$1 [L]
RewriteRule ^([^.]*).html /index.php?search=2&query=$1 [L]
RewriteRule ^([^.]*).shtml /go.php?sku=$1 [L]

RewriteRule cars.html /go245.html [L]
ErrorDocument 400 /400.htm
ErrorDocument 401 /401.htm
ErrorDocument 403 /403.htm
ErrorDocument 404 /404.htm
ErrorDocument 500 /500.htm

-----------------------

The above example is what my htaccess file looked like which failed to work. Notice the 4th rule down. So could this be not working due to the other rules above it? Or what catches your eye? Thx again, D.


Praetor - 08.05.08 1:51 am

Great article....... learned a few things. Used the .htaccess in my includes folder. Works like a charm. Used the same in my css folder. No joy! Will have to leave that one alone. Now, if I can figure out how to do rel="nofollow" in the .htaccess, I will be in business.

Thanks for all the information.


Derick - 08.05.08 8:37 am

Dear Corz,

Thank you for your advice for the last post.

Still I am learning mod_rewrite. Now i got the things littlebit and now working my redirections.
But I have a two problem

1) Once I have create link as folders images are not loading and all paths taken from the current virtual folder.

RewriteRule ^jouets-.*-([0-9]+)/.*-([0-9]+)\.html$ /jouet-test.php?r=$1&sr=$2 [L]

http://www.example.com/jouets-1er-age-37/puericulture-38.html

2) I want to remove "r" value 37 and "sr" value 38 from the URL (http://www.example.com/jouets-1er-age-37/puericulture-38.html). If I remove those the jouet-test.php loaded without data. That means "r" and "sr" not set. How can I do this?

Thanks.

I appriciate your comments and suggestions.




corz - 08.05.08 9:18 am

Dman,
Notice the 2nd rule down.
smiley for ;) Wooosh!

Praetor, rel=nofollow is evil.

Well, okay, I guess it has its uses, but .htaccess-automatic? That's just Wrong! I only see a use for it in temporary situations, as in, before the webmaster reviews a public comment, personally. All other uses seem anti-web, to me.

I remember the buzz I got when I first spotted someone had dropped a Wikipedia link my way, and how my heart sunk when I read the source. rel=nofollow. Some kind of bandwidth-eating joke, and I fell for it! smiley for :erm: Still do…

Derick, if more than one page use either the phrase jouets-1er-age or puericulture in their flat-link, then you need those numbers in the flat-link.

In other words, unless you have a database of word->number mappings that equates jouets-1er-age -> 37, and jouets-something other phrase to 38, something else again to 39, and so on; which you don't appear to; then you cannot remove the numbers. You need them for the regex back-references.

If you want to re-use jouets-1er-age or puericulture in your flat links, then you need to keep the variable numbers inside the flat link, so mod_rewrite has something with which to populate the r and sr variables in th e target URI.

That's me said the same thing three different ways! And again, just in case..

If my flat link, the one the user sees, is foo.html, and I want the user to actually get bar.php?var=38, then I need to get the 38 from somewhere. It can either a) come cleanly from the flat-link, in the style of /foo/38.html, or foo-38.html, or foo38.html, or something like that. Or b) a file/database table/etc. exists somewhere, where bar.php could look to know that, for example..

foo=38
w00t=39
roo=40


and so on, and know that "foo" = "38", and the only time that I would use foo in a flat-link, is when I want to get the user to bar.php with var set to 38. Mod_rewrite is very like magic, but you can't just magic variables out of nowhere!

;o)


corz - 08.05.08 9:37 am

I must add, and quite literally, to this page; a link to my php debug script, I might even do a proper section about it, in lieu of its own page one day, as all these sorts of things should have.

Inside the archive (McAfee reports all downloads at corz.org are 0 on the nuisance meter, Green for go!) are two files, a regular back-end version (which spits variables to a real file, and is mostly used to capture and display debug output from inside php scripts), and a "report" version, which is most handy for .htaccess.

What's it for? You ask. Simply, it spits out a big page of stuff; the entire $_SERVER[] array, variables available to the script (say that when you're drunk!), that is; your captured variables, I sometimes drop a link to it into tech-support emails, and invariably folk get back with some positive comment, often a plain old "Wow!". It is handy.

Usage is simple; you make it your target page, so in a rule like this..

RewriteRule ^(.*)\.html$ catch-all.php?var=$1

You would have a copy of debug-report.php renamed to catch-all.php, and type foobar.html into your address bar, and with yer mojo working, debug-report.php leaps into your browser with a shit-load of exactly the sort of information you need to figure out all this stuff. When I'm messing with mod_rewrite, it saves me lots of time. Read-out. And it's free.

I'll drop a link right here, too, by way of motivating myself to get the URL into the clipboard, and in the Style of Brian Tracey, handle every piece of paper only once..

https://corz.org/engine?section=php/corz%20function%20library&source=debug.php

for now..

;o)


Derick - 08.05.08 10:10 am

Thanks Corz.

I got trick now.

I have another problem. When I create links as

RewriteRule ^jouets-.*-([0-9]+)/.*-([0-9]+)\.html$ /jouet-test.php?r=$1&sr=$2 [L]

http://www.example.com/jouets-1er-age-37/puericulture-38.html.

But "jouets-1er-age-37" directory is not there. So the all images are not loading.

Can you please advice for me.




kmm - 09.05.08 2:05 pm

Great info been really helpful trying to get a handle on rewrite/redirects.

Ok before I pull what little hair I still have out please have a look at this for me.

I want to redirect http://www.domain/?p=about_us to http://www.domain/aboutUs2.php

My .htaccess:
RewriteEngine On

rewritecond %{http_host} ^domain
rewriteRule ^(.*) http://www.domain/$1 [R=301]
rewriteRule ^linkmachine/(.*)$ http://www.domain/?p=linkRedirect [R=301,L]
rewriteRule ^blog/(.*)$ http://tvblog.another-example.com/temple-view/ [R=301,L]

rewriteRule ^about(.*)$ http://www.domain/aboutUs2.php [R,L]


IndexIgnore *

ErrorDocument 400 /a1Whoops.html
ErrorDocument 401 /a1Whoops.html
ErrorDocument 403 /a1Whoops.html
ErrorDocument 404 /a1Whoops.html
ErrorDocument 500 /a1Whoops.html

All the other rules work fine except this one and I cannot see why. I think it has something to do with the criteria being in the query string but heh I'm really out of my depth here
Thanks in anticipation of help!


Jamie - 13.05.08 1:23 pm

Thank you very much.. Finaly an understandable tutorial on .htaccess and Rewrite!


corz - 13.05.08 2:39 pm

Derick, that isn't an .htaccess problem, that's your back-end. If jouets-1er-age-37 isn't a real directory, you can't create "relative" links to images and such on page with a virtual URI. The user's browser has no idea you are using mod_rewrite.

You'll need to either use full links to the images, from the root of the server (i.e. begin <img src="/...), or else use more rewrites to redirect image requests to the correct folder. There are other solutions.

kmm, correct; you are incorrectly attempting to capture parts of the query string. Two things are wrong with your code..

Firstly, you do not understand the usage of anchors; that is, the "^" and "$" characters in the pattern part of the rules. Using code you don't understand is a recipe for disaster!

Along with some other improvement to that section, I have enlarged the regex special character descriptions, among these you'll find..

An anchor explicitly states that the character right next to it MUST be either the very first character ("^"), or the very last character ("$") of the URI string to match against the pattern …

In light of this information, you will see that the anchor in your first RewriteRule is completely superfluous, and the one in your last RewriteRule is what tells me you don't understand your code. A request for about.php?foo=bar would match your rule, as "about" is at the very start of the URI. See?

I'm assuming you are catching legacy inward links, or something - you can use Rewritecond, to check for the existence and value of the p variable..
RewriteCond %{QUERY_STRING} p=about_us
RewriteRule !aboutUs2\.php /aboutUs2.php [R,L]
Which matches all requests that have the p query variable set to about_us, that haven't already been redirected to aboutUs2.php

If you really need it to be an external redirect, simply tag the http://domain part back on.

By the way, you may be interested to know that I also recently added a new section about capturing variables. A good read for all! Feedback welcome.

Hey! Jamie, you finally got here!

;o)


stein - 17.05.08 11:44 am

Hi Corz

i'm read your htaccess tutorial, and it so nice

i want to make my url so short and nice looking like this

http://mydomain.us/lyrics/Dark+Moor/Dark+Moor-236.html

http://mydomain.us/lyrics/[this from DB artist name]/[this from song title]-[this id].html


and i'm using this rewrite:

RewriteRule ^lyrics/.*/.*-([0-9]+)\.html$ lyrics.php?liriklagu=$1 [NC,L]

but it doesn't work, can you help me please?

how about like this too:
http://mydomain.us/lyrics/Artis_name/123/song_title.html

where is the best look ? thanks

®


steve - 17.05.08 6:59 pm

Yellow text on a yellow background. Sweet.


corz - 17.05.08 9:01 pm

stein, you didn't state what form your back-end script expects the URLs to be in, so I can't be specific. However, I need to reply, because, looking at your code, it seems you have stumbled onto a nice Gotcha! One I haven't covered before.

Firstly, you need to decide on a format for the "flat" links. Either will work fine, but you gotta decide, or else you have confusion. I'm assuming all your script needs is the "id", and from that, it can produce the correct page. I'll assume also, that you have decided on the first type of flat link..

http://mydomain.us/lyrics/Dark+Moor/Dark+Moor-236.html

And that you are using this RewriteRule..

RewriteRule ^lyrics/.*/.*-([0-9]+)\.html$ lyrics.php?liriklagu=$1 [NC,L]

So why isn't it working? The code looks fine, right? You did good! BUT, what you didn't realize is that your requests are suffering from "Content Negotiation". Basically, because you have used the same word, "lyrics" at the start of your virtual path AND your real php script, Apache is trying to help you out by guiding the browser to the real file.

You have at least two choices. Firstly, use a different word, e.g..

http://mydomain.us/song-lyrics/Dark+Moo...

Or else disable Apache's MultiViews capability..

Options -MultiViews

And your current .htaccess code will work just fine.

;o)

ps. steve, if you are vision-impaired, I recommend you disable the styles, or use your own - you will find my pages are fully XHTML compliant, and look just fine even with all styles removed. If you use Firefox, and have trouble with your vision, I recommend "Read Easily", which I use a fair bit myself, particularly for white-on-black sites. A simple click toggles all the styles on a page..

https://addons.mozilla.org/en-US/firefox/addon/1224


mohsin - 20.05.08 11:35 am

SIR
ur given data about URL is for professionls
not for new user.•


Miz - 23.05.08 4:16 pm

I am suffering from being an .htaccess noob. I am trying to do a very simple mod rewrite of (mysite.com/foldername/*) to
(mysite.com/1)

* is any folder or file within that folder
1 is any folder name, doesn't matter

Everyone online has all these complicated php variable passing url's, and i just need something i think might be fairly simple.

Can you help?

I am pretty new to php and i need to change the folder names that people see, because i have thousands of files in the folder, but i need people to not see the real name of the folder, or the real name of the files eg. mysite.com/apple/applejacks/index.php and mysite.com/apple/dumplings/index.php needs to change to mysite.com/1 but show the real data that is on their respective index page.
mysite.com/cocowheats/cocowheats/index.php needs to change to mysite.com/2 and so on
i only have about 12 main folders



Miz - 23.05.08 8:16 pm

I thought of an even better way to explain what i am trying to do. Is this even possible?
When someone types in or clicks the URL http://www.mysite.com/foldername/address/index.htm
can mod_rewrite (or whatever) forward you (or disquise you) to
http://www.mysite.com/1
and have the same data show that is on http://www.mysite.com/foldername/address/index.htm

in this example, i have 12 folders that are like foldername, but 2500 folders that are like address


corz - 24.05.08 1:37 am

Miz, that isn't logical. Nor is it possible.

Think about it: "i need people to not see the real name of the folder". Fair enough, but how does the user get this link in the first place if you do not allow them to see it? They cannot click or type-in something that does not exist!

Yes, you can disguise the long URLs and present short /1 type URLs instead, but mod_rewrite doesn't know which page to send the user to, unless that information is somewhere in the URL in the first place.

mod_rewrite re-writes requests; it can't rewrite your back-end. You firstly need to create the short links in the first place (on your pages), and those links must, in one way or another, supply ALL the information required to recreate the target URLs.

In short, you could easily create twelve rules, one for each folder, and have users click links like mysite.com/1/dumplings/, and have that rewritten transparently to mysite.com/apple/dumplings/index.php; replacing apples with 1; but unless dumplings is somewhere in the original URL that the user sees and clicks, mod_rewrite can't use it in the target. Where would it get it from?

If it were possible to do %{ENV:MY_LINK} in a RewriteRule, then you could achieve what you want, but it's not (afaik), and anyway, the big question is; why would you want this?

In my opinion, what you are trying to do is Wrong for so many reasons. Your original URLs are clear and user-friendly, not to mention, very SEO-friendly; why would you want to use daft numeric URLs that only your back-end can understand? That's nuts!

Also, remember, you only need to do http://mysite.com/apple/dumplings/; assuming your DirectoryIndex directive is correctly setup, the index.php part would be would be handled automatically by Apache.

;o)

ps. mohsin, the overwhelming consensus seems to be the exact opposite of what you suggest. I guess there is a language barrier at work here; one of the downsides of living in a multi-cultural world.


Miz - 24.05.08 2:34 am

You are so right, however, in this application, it is necessary or i have a lot of work on my hands. Let me explain further.

I am helping a friend with this project, and that is where this all started.

He is a photographer for a bunch of real estate companies and has setup folders http://www.hisdomain.net/realtor/address/index.htm
they are all flash movies(virtual tours) of his photography for the realtors. There are about 12 different realtors with about 2500ish addresses. The MLS in our area has just changed the rules that you cannot advertise the realtors name, nor an address in the url for a virtual tour. The problem is, to change the url for 2500ish mls entries is impossible (not impossible, but stupid)

So, this is not a search engine friendly issue, nor is it as crazy as you suggest. Maybe i am thinking about this backwards, but i am just trying to save ourselves a bunch of headaches. I appreciate your responses, and your help in any way.

Does the real application make more sense as to where this crazy idea came from?


corz - 24.05.08 3:07 am

Well, firstly, I have a limited understanding of what an MLS even is..

MLS®, Multiple Listing Service®: These are trademarks owned by The Canadian Real Estate Association. They are used in conjunction with a real estate database service, operated by local real estate boards, under which properties may be listed, purchased or sold. An MLS® listing means REALTORS® have agreed to work together for the marketing of a listing.


But from what I can tell, it seems unlikely that these rules would even apply to your friend's virtual tours - he isn't an actual realtor, is he?

And secondly, unless the address is somewhere, how can the user pick which virtual tour to view? I can understand not wanting to have the actual realtor's name displayed (we call them "estate agents" in the UK), as that is advertising of a sort, but without the address, there's no way for the potential viewer to differentiate between properties.

Obviously I'm missing something here, otherwise (once you comply to these rules) you have something like a list of thousands of links; 1,2,3,etc. and the clicker has no idea what he's getting into. Who buys property at random?

Is the user expected to just sit there and run through thousands of virtual tours until they find one that suits their needs? It all sounds completely insane! Surely there's some kind of menu or search, or something that enables users to select tours by type/cost/location/etc., and once they have made this selection, there needs to be a real link to the virtual tour.

Unless you are using some kind of php handler script, with a list of numeric=>realtor/address mappings, there is NO WAY you can present links that do not have this information in them.

Like I say, I'm obviously missing something. Either that, or the MSI rule is totally nuts.

;o)


bilal - 25.05.08 12:21 pm

How can i do this

www.myexample.com/?p=13

to

www.myexample.com/MyParameter



corz - 25.05.08 12:50 pm

Too easy.

NEXT!

;o)


Saulie - 27.05.08 1:03 am

Heya

I have read through the tutorial and tried many ways but was wondering if you could show me how I make it possible for links like http://www.example.com/forums/index.php?showuser=2 be accessed through http://www.example.com/user/2. It's quite different from the frist url and I'm having much difficulty getting it, was wondering if you could help me, or if it's even possible, if not maybee at least getting http://www.example.com/forums/user/2 to work.

Thank you very much in advance, Saulie.


corz - 27.05.08 5:44 pm

RewriteRule ^user/(.) /forums/index.php?showuser=$1

;o)


cuc - 27.05.08 8:34 pm

a question
how change http://name.com by http://www.name.com
?


zum - 27.05.08 10:02 pm

please a question who nobody knows:
you know if is possible escape ? at the pattern
example

redirect 301 /?page=1 http://www.name.com/a.php

not works

redirect 301 /\?page=1 http://www.name.com/a.php

not works

redirect 301 /\\?page=1 http://www.name.com/a.php

not works

? inside brackets, not works neither

any ideas?


corz - 27.05.08 11:03 pm

zum, I don't do mod_alias, only mod_rewrite. Have you considered mod_rewrite, instead? You will find RewriteRule directives much more flexible. See the article (above), and try it.

cuc, you didn't post your code, so I can't tell what's wrong with it.

Also note, you will need to post in English.

;o)


Saulie - 28.05.08 12:33 am

Thanks for the help cor, would I have to put anything above that?


zum - 28.05.08 2:06 am

hi cor, thanks for answer
I try with RewriteRule, but I have the same problem

no brackets here because bbcode don't leave me post:

RewriteBase /
RewriteEngine On
RewriteRule ?page=1 http://www.name.com/a.php NC,R=301
RewriteRule ^\?page=1$ http://www.name.com/a.php NC,R=301
RewriteRule ^\\?page=1$ http://www.name.com/a.php NC,R=301


dont works, I hate this '?'



corz - 28.05.08 7:50 am

I knew when I simply dropped an answer (for Saulie) without any effort on his part, that I was setting a dangerous president (Hey, I was pushed for time!), and I won't do it again. Lesson learned! This page is for people who want to learn about mod_rewrite. If you just want "solutions", then pay someone to do it!

zum, please do not post again until you a) read the article (above), b) read the HUGE notice right above this comment facility, and c) have tried some rules based on the code found here. For starters; where in my article does it describe the use of RewriteBase?

Also, in no less than THREE places, is the information you need to know how to use square brackets in your post; 1) the notice above these comments, 2) the help text below the comment form, and 3) the error message you got when your post wasn't being accepted. If you could be bothered to read any one of those three things, you could have posted without difficulty. If you can't understand how to operate a simple bbcode comments facility, you have no chance with mod_rewrite.

I realize that foreign language can be a barrier, but if you want to do take control of your web site, you will need to get to grips with English. That's just the way it is.

mod_rewrite isn't so hard, you simply have to read. After you read the article, slowly, you will understand the basics, and will be in a position to try some real code that may work. Particularly, pay particular attention to the section about capturing variables.

Saulie; only the usual stuff, the same as every example in the main article.

;o)


zum - 28.05.08 11:44 am

Ok, solution is this:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=1$
RewriteRule ^(/)?$ http://mywebsite.com/url_to_go_to [R=301,L]


Thank you very much for your excellent article


corz - 28.05.08 11:57 am

Yes! You learn fast!

My only recommendation would be to use [R,L] while testing, then add the =301 only when you are certain it works as expected. It looks like it will work fine, though, good work!

By the way, unless you expect requests for multiple domain names that you need to redirect to that domain, you probably don't need the http://mywebsite.com part of the target URL.

;o)


Jay - 02.06.08 5:57 pm

I'm having trouble with my blog. It's installed under /blog. My htaccess file is in the main directory. I'm getting 403 forbidden errors when I try to access my blog at www.myexample.com/blog. You can access my htaccess file here. Is there a way to make the /blog directory exempt from the htaccess file in the main directory? BTW, if I delete the htaccess file from the main directory then the blog works fine so I know that's the problem.


T - 04.06.08 2:39 pm

hi!

just wondering if you could answer my question. Basically i'm just doing some testing to get the hang of mod_rewrite.

I have assumed that my website has categories and products and i want to have urls like:

http://www.mysite.co.uk/categoryname.html i.e. http://www.mysite.co.uk/furniture.html
http://www.mysite.co.uk/categoryname/productname.html i.e. http://www.mysite.co.uk/furniture/chair.html

I have got to the point where my urls are coming out as http://www.mysite.co.uk/cat/1-furniture however as i show above, i don't want to show "cat" or "1" for that matter i just want it to look like i have a folder called furniture, if you know what i mean. I am unsure of how to do this because from what i understand i need to have the id in the url for it to be passed to the actual page for me to then use in my scripts, i don't think its good practice to just use the category name in my script because its possible its not unique. am i making sense or just rambling?!

this is what i have:

RewriteRule ^cat/( <^/>+)-( <^/>+)$ /cat/$1-$2/ [R]
RewriteRule ^cat/( <^/>+)-( <^/>+)/$ /categoryinfo.php?catID=$1


i thought that this part: /cat/$1-$2/ [R] was showing what i wanted the URL to end up like but if i change it, it doesnt make a difference.

would really appreciate any help.

thanks


Joe B - 06.06.08 9:06 am

My problem is having apache authentication work if a rewrite occurs - it immediately goes to the 401 error. I have been all over the net, all over bunches of examples and there have been a couple of "solutions" which don't work. What you see is the result of various tests.

The last thing I tried was to get the request_uri to work - no go. I did a printenv to see that I was looking at the right stuff. I put $ ^ (.*) \ in just about every possible place <sigh>. Here is the .htaccess in my main directory. My last attempt was to have the request_uri filter out my password protected directory - so, I guess that could be a 2nd question because I just plain couldn't get it to hit on anything (trying ^/members24/ in various configurations to just see that I could get a match). However, my main concern is that I cannot consoldate website names (www and not-www) w/o breaking authentication for the .htpasswd protected member area.

If you could point to me something that I missed or give me a hint, I would greatly appreciate it. I'm just out of things to try except to build my own linux/apache so I can possibly see what a log would say. This has me stymied - but I guess I'm learning a lot just trying to fix this one little thing?? HA!

<Files ".ht*">
order allow,deny
deny from all
</Files>

#bypass rewrite so it doesn't tangle with a password protected area
ErrorDocument 401 /errordocs/badpasswd.html

#instead of a no-no, eventually send them back to main page
ErrorDocument 403 /errordocs/badpasswd.html

ErrorDocument 404 /errordocs/filenotfound.html

# well, maybe someday I will get the rewrite to work -- geesh
Options +FollowSymlinks

#RewriteEngine On
#RewriteCond %{HTTP_HOST} ^mathdork\.com$ [NC]
#RewriteCond %{REQUEST_URI} !^/members24/
#RewriteRule ^(.*)$ http://www.mathdork.com/$1 [R=301,NC,L]



This is in my protected area (mathdork.com/members24):


<Files ".ht*">
order allow,deny
deny from all
</Files>

#according to http://wordpress.org/support/topic/89515 this fixes the
#rewrite and password tangle - we will see

#force password prompt instead of crazy rewrite mess
# needs to be done instead of rewrite above
#ErrorDocument 401 /errordocs/badpasswd.html

#after too many attempts, force the right response
#ErrorDocument 403 /onerror-null.html

AuthType Basic
AuthName "Member's Area"
AuthUserFile /home/royannel/pass/.htpasswd
AuthGroupFile /dev/null

<Limit GET POST>
  require valid-user
</Limit>



Any help would be very much appreciated. I just don't get it.. You can see parts of things I tried after getting "fixes" from elsewhere. (Oh yes, been over your stuff several times -- I just must be missing something fundamental, I guess..)

Thanks for any and all help.. -- Joe B.


marko - 07.06.08 10:17 am

Small question.

Is it possible the modrewrite won't cause a new requst for the sever?

How would you guys recommend to do something like this.
I have a sub domain www.marko.myexample.com and I want that if you type www.marko.com it will actually be the sub domain but the user will see the reguler domain www.marko.com

The problem is that it is a virtual subdomain and the domain itself www.marko.com has nothing in it ( it's a pointer ). Is that solveable as all?


Ben - 09.06.08 6:24 am

I used your htaccess tutorial to translate pages a few months ago and worked great, thanks. This no longer works so I checked your French translate link which no longer works either. I guess Google have done something (only loads top frame) and thought I'd point it out to you.

regards
Ben




Mike Schinkel - 11.06.08 8:23 am

Hey cor,

GREAT post on mod_rewrite from someone who really appreciates well-designed URLs.

Anyway, I think I've read your post in its entirety and looked all over the net and still can't find (recognize?) and answer for this.

I've launched a new Drupal site that replaces an old Drupal site with much of the same content but I've changed the URL structure and Google still has lots of those old URLs in the following form:
http://myexample.com/?q=node/12345

The new site has changed the URLs to be this format:
http://myexample.com/node/12345

I want to 301 redirect from the old format to the new format so Google will eventually clean up its index and not see duplicate content.

In addition, I have both a staging server on one domain and a live server on another and want the code to be identical between each. I want to be able to test the old URLs on the staging server and have them correctly redirect to the new style before I upload to the live server and have it all just work, all w/o changing anything in the .htaccess file.

The problem is I can't seem to figure out how to capture back references from each of two RewriteCond statements. Here's what I have that works but only for the live domain (not for the staging server):
RewriteCond %{QUERY_STRING} ^q=node/(.*)$
RewriteRule ^/?$ http://myliveexample.com/node/%1? [L,R=301]

But here's what I want (but it doesn't work):
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteCond %{QUERY_STRING} ^q=node/(.*)$
RewriteRule ^/?$ %1/node/%2? [L,R=301]

Can you figure out how to capture both the host and part of the query string into back reference variables?

Many thanks in advance for your help.


Mike Schinkel - 11.06.08 8:58 am

Well, I figured it out so I thought it only fair to give the solution:
# Implement Old Node Search URLs with a "q=node/nnnn" parameter
RewriteCond %{QUERY_STRING} ^q=node/(.*)$
RewriteRule ^/?$ http://%{HTTP_HOST}/node/%1?	 [L,R=301]
I swear I tried that earlier and it didn't work (instead giving me the path to the local file) but anyway, it is working now.

OTOH, I still didn't figure out how to access back references from multiple RewriteCond statements in the RewriteRule statement. Any thoughts for future reference? TIA.


Ziggy Stardust - 13.06.08 12:50 pm

Hello, i will start saying that i read the whole article and its comments, and i still haven't found a solution to my problem. Or maybe i don't understand very well the whole thing.

I have an already created web site with lots of crazy urls like
http://www.mysite.com/sub_index.php?contenido=aplicaciones/resale/ficha&idioma=_eng&IdRes=52

I read this week about the existence of this thing called mod_rewrite, and decided to make the urls smaller.

I made a lot of tests, and what i really dont understand is that it works if i write the url directly in the address bar, but when I click on any link on any page, it takes me to the real link (the big one). I thought this thing called mod_rewrite would automatically change the link into, for example http://www.mysite.com/aplicaciones/resale/ficha/eng/52

Do i need to do something more? Do I have to rewrite the whole code in my 200 pages to change the links ... i think this is madness.

Regards and thanks.


corz - 13.06.08 6:54 pm

Yikes! smiley for :eek:

Jay, yes, as stated in the article, use RewriteEngine off.

T, correct, the ID, etc., MUST be in the original URL in some form. If you don't want to use numbers and such, use php (or similar) to map numbers to real words, and use real words in your URLs.

marko, simply use a rewrite to redirect all requests transparently into the subfolder.

Ben, rats! Something has definitely changed. When I try my own cute auto-translation link (on this page) I get a frame within a frame within a frame within a frame... And I have to kill the page. Hmm. I'll need to look into this. Thanks for the heads-up.

Mike Schinkel, I love it when folk get back with their own solutions before I get a chance to post. Of course, leaving the replies for a week or so always helps!

As for your back-references, you simply increment the numbers, %2, %3, etc. You might also want to have a look at RewriteCond %{THE_REQUEST}.

Ziggy Stardust, mod_rewrite is only half of the story; the links on your pages are your own responsibility, mod_rewrite won't rewrite your pages, only requests from browsers (etc). Normally, people will code this dynamic->flat translation into their back-end (php, or whatever), so the new, flat links are generated automatically (or not, if it's optional).

Altering hundreds of links manually, is madness, yes. But as you are using variables (it's generally variables that people want to make flat) I have to assume that at least in part, your back-end is generated.

Whichever part generates the links, make your code changes there.

;o)


corz - 13.06.08 8:48 pm

Oops! Apologies Joe B, I seem to have skipped right over your question! Perhaps that was because I subconsciously knew I had no good answer for you.

Firstly, thanks for bringing this to me, it's something I've not seen before. And although I can confirm it is a real issue, it's more like a real non-issue. Generally, www-> no-www translation is something that's used to catch dumb/uninformed users and legacy links; by the time they get to any member area, they should already be using the correct domain format, so it's not an issue.

If I add www. to my own "private" areas, I get the exact same result. But that's me doing it manually, to test your issue. In the real world, no one has ever done this, at least, no one has complained to me about not being able to access, for example, one of my beta test areas.

I wouldn't worry about it. Just create no-www links and be done with it. If any user is daft enough to add www manually, they deserve everything they get!

;o)


skydiamond - 15.06.08 1:45 pm

I have to redirect this address
/index.php?mod=read&id=1211181563
to
/blog/linux/debian/fare-il-downgrade-da-unstable-a-testing-in-debian/.

Here it's the code I made:
htaccess code
RewriteCond %{QUERY_STRING} id=1211181563
RewriteRule ^(.*) /blog/linux/debian/fare-il-downgrade-da-unstable-a-testing-in-debian/ [R,NC,L]

The problem is in the rewriterule. I want to erase completely the old URL for the new one, but it rewrites on this: /blog/linux/debian/fare-il-downgrade-da-unstable-a-testing-in-debian/?mod=read&id=1211181563.

I really don't understand why it stops on ?mod... How the regexp would be formatted to match this rule in perfect way? smiley for :ehh:


corz - 16.06.08 2:53 pm

Firstly, your question is poorly worded. I will assume English is not your first language, and also that what you are trying to to is simply remove the query string part from your target URL.

The answer is simple; add a "?" to the end of your target, e.g..

...ngrade-da-unstable-a-testing-in-debian/? [R,NC,L]

Of course, if you meant something else, then by all means, ask again.

;o)


goosey - 16.06.08 3:27 pm

Hey,

Firstly nice tut

secondly;

i've been trying to do this for a while...

make it so that you can have custom file extensions for files. For example you could have all you files end in .goosey

e.g. index.goosey

but in reality you want these files to be executed by php

I have seen it somewhere else but i can't find it anymore, but i am sure it can be done through .htaccess

If you can help that would be awesome!

thanks goosey


corz - 16.06.08 3:43 pm

Yes goosey, both can be done, no problem. See part one of the article.
(link above comments)

;o)


goosey - 16.06.08 4:01 pm

Awesome thanks so much,

I've got it working but i was just wondering, does this make it slower than having the file named .php in the first place or is it negligible.

goosey


corz - 16.06.08 5:36 pm

Nope, it's no slower than using a regular .php extension.
There may be other disadvantages, however.

;o)


skydiamond - 19.06.08 11:03 am

Firstly, your question is poorly worded. I will assume English is not your first language, and also that what you are trying to to is simply remove the query string part from your target URL.
You understood perfectly what I mean. smiley for :cool:

I will try your solution. Thanks for all!

P.S.
I'm italian.




Northie - 22.06.08 11:46 pm

I know there's not much chance of getting work done for me, but I'm really struggling with this. I am fairly tech-y but this htaccess and rewrite stuff is really not going in.

Anyway, I recently changed hosts and the set up I did have no longer works (every request failed and was picked up and processed by php in a cistom 404 page, no htaccess required).

I have two url formats that I want to re-write, these are

example.com/category/

and

example.com/category/page/

My .htaccess file looks like this

RewriteEngine On
RewriteRule (.*)/(.*)/ /index2.php5?category=$1&article=$2
ErrorDocument 404 /404.php
AddHandler php5-script .php

No, the later fo the two formats does work using this and I can see how and why.

However, the former does not (obviously at the moment) but whenever I add a line for the former format, eg

RewriteRule (.*)/ /index2.php5?category=$1

My php script doesn't get the right information. In fact, I don't know what it's getting.

I also tried a single rewrite rule for both situations, passing the whole request string as a query string, eg

RewriteRule (.*)/ /index2.php5?$1

But this just gave me a 500 error.

Any suggestions on how I can proceed will be greatly appreciated - many thanks in advance!

Firstly, remember, you can use conditions inside the pattern, eg. "([^/]+)" to first check for the shorter variant, and then allow the longer variant to pass to a second rule. And secondly, download my debug-report script and know *exactly* what's happening to your variables. Guessing + .htaccess = trouble! ;o)



iamnow - 24.06.08 5:40 pm

What a wonderful tutorial...The only way to go!

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/index\.php
RewriteRule ^([^/]+)/? index.php?cid=$1 [L]

This seems simple enough but I'm totally befuddled...

The above is blows the graphics away and basically cripples the site.  It's supposed to turn this http://localhost/members/658  into this http://localhost/members/?cid=658.  

Your help would be greatly appreciated.  Many thanks in advance...



iamnow - 24.06.08 8:06 pm

After taking a break I came up with this and it worked:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^members/([0-9]+) http://localhost/members/?cid=$1 [NC]

I put it on my host server as:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^members/([0-9]+) http://www.iam4gbg.com/members/?cid=$1 [NC]

Could that have to do with the httpt_conf being different?



DK - 25.06.08 1:58 pm

Dear Cor,

I have following problem. Can you please explain why is that happend?


RewriteRule ^jouets-.*-([0-9]+)-.*-([0-9]+)-.*-([0-9]+)\.php$ /jouet-sousrubrique.php?r=$1&sr=$2&ar=$3 [L]
RewriteRule ^jouets-.*-([0-9]+)-.*-([0-9]+)-.*-([0-9]+)\.php$ /jouet-sousrubrique.php?r=$1&sr=$2&arr=$3 [L]
RewriteRule ^jouets-.*-([0-9]+)-.*-([0-9]+)-.*-([0-9]+)\.php$ /jouet-sousrubrique.php?r=$1&sr=$2&pr=$3 [L]
RewriteRule ^jouets-.*-([0-9]+)-.*-([0-9]+)-.*-([0-9]+)\.php$ /jouet-sousrubrique.php?r=$1&sr=$2&prr=$3 [L]

but it always get the first one. if I come from other query strings it always get the first one.

Please explain.

Yeah! All the rules have identical patterns! The first rule will catch everything. You need to use the "ar", "arr", etc., in the pattern, too (left-hand bit) ;o)



AMIT - 02.07.08 12:05 pm

I have done this and its working fine
http://abc.com/java/jobs
RewriteRule ^( <^/>*)/jobs$ job/search_result.php?keyword=$1

The problem is with this url
How will write the rule for this
http://abc.com/java/jobs?e=0-2&a=7

check out the qsa flag. ;o)



simone - 07.07.08 9:22 am

Hi, tnx for this interesting article.

I have a problem with url rewriting, i need to use it to do deeplinking with SWFAddress,
so I want to rewrite

/products/productname/


to something like

/#/products/productname


the rule i wrote is:

RewriteRule ^products/([A-Za-z]*)/$ /#/products/$1/ [R]


this seems to work nice except for a simple issue:
the #
is urlencoded by apache into
%23

so the final address shown in the address bar is

http://www.mysite.com/%23/products/bonarda/

instead of the desired
http://www.mysite.com/#/products/bonarda/


any hint on this issue?

bye
simone


Tom - 15.07.08 10:39 pm

WOW, thanks for the wicked examples!
Espicially happy with your languages translator!!
Wicked idea!




ashwani - 16.07.08 11:55 am

.htaccess provide comlete™


Dman - 18.07.08 9:17 pm

I have the hardest time getting mod rewrite to work with Tomcat.

Here is our situation. We run Apache2.2 and Tomcat5.5.2.3 together. Apache does nothing but forward requests to Tomcat instances via Mod JK workers.

Mod Rewrite is loaded and working on the Apache side. Example if I create 2 files: test1.html and test2.html and my simple rule are as followed.

I have a .htaccess at the root of Apache
RewriteEngine On
RewriteRule ^/?test1\.html$ test2.html

User type http://www.apachehost.com/test1.html he gets the content of test2.html. Great!

I incorporated this in httpd.conf file and hoping that it would do the same but I am getting test1.html content only:

Example: If I type http://www.tomcathost.com/test1.html I just get the test1.html page instead of the test2.html content.

<VirtualHost *:80>
ServerName www.tomcathost.com
Options +FollowSymlinks
JkOptions +ForwardURICompat
JkLogFile logs/mod_jk.log
JkMount /* fadworker
ErrorLog logs/www.tomcathost.com-error.log
#CustomLog logs/www.tomcathost.com-access.log common
LogLevel Error
JkUnMount /admin/* fadworker
JkUnMount /manager/* fadworker
JkUnMount /host-manager/* fadworker
JkUnMount /probe/* fadworker

RewriteEngine On

RewriteLog logs/mod_rewrite.log
RewriteLogLevel 4

RewriteRule ^/?test1\.html$ test2.html
</VirtualHost>

My question is am I missing something here?

Mod JK is loaded before Mod Rewrite.

Dman, remember, for httpd.conf use, you need to add a slash to the start, e.g. /test2.html, or else you'll get a 400, Bad Request. As for Tomcat, I have zero experience. ;o)



Evan - 25.07.08 1:47 pm

Wicked cool article. Just starting messing with apache and this stuff, after a good explanation like the one above, really makes me happy. smiley for :D


micmac - 27.07.08 11:57 am

Neat approach and performance, sheding light in an dark area, typical cause of lots of frustrations. By this help I could improve a lot the security of my server.

But also here are examples, where a given code won't work in reality. It will be, because too fast the practitioner gains the impression, he understood the language systematics and thus creates on this base new varieties of options. I understand, that not any sample, presented in such a broad collection is tested first. Who likes 500 server error on his life system?

A typical sample, not covered here anymore (?) is the hiding of the .htaccess. Long survey finally gave, the given good codes only apply for .httpd server config and don't apply for the .htaccess itselfs (fine 500 disturbances...). The server provider (US) does not even understand, what the customer is asking, and falsly claims, the file is hidden already. That is valid so far any .ht is a hidden file, but it can be seen by any simple remote access via web.

My latest sample the simple RewriteRule. The given code to rewrite URL does not work, because the new URL contains a "?" for PHP POST command and that can't be masked by ESC, when the own server is set up for a certain ISO character set, for what are good reasons to stick with.

A limit of information is, that the given samples are only explained in short. Namely the definition of expressions like (^/y) in the samples is not satisfactorily covered by the table of valid forms. The same goes for Apache genuine web-manuals, written by machines for machines. Thus the reader here only can import given samples, to the utmost customizing them, but finally does not understand the rules. That a few minutes ago also brought good 500 errors to my readers or banned them from pages, now unintentionally restricted by authentication.

But such, it seems, is the server config world ...




Rob - 01.08.08 6:23 pm

@simone
You need to tell mod_rewrite not to urlencode your rewritten URL, you do that with the NE (noescape) flag. So you should use something like this:

RewriteRule ^products/([A-Za-z]*)/$ /#/products/$1/ [NE,R]


Michael - 05.08.08 5:57 pm

Thank you for a great article on .htaccess. I am a novice in this area and I really needed some help. I am running a demo video sharing website (I am just experimenting on how the script will work). After installing the script, I uploaded some test video to see if the script is functioning as it was intended by the script authors. The script is function very well as promised. Today I was playing around typing the url of that website on my browser and I was shocked on what I have just found out. All of my converted video files (flv) are saved by the script inside the folder called "uploads". If I type MyDomainDotCom/uploads on my browser, it will show all the files in that folder and I don't want other people to be viewing and downloading this files using their browser.

I was searching about the topics on how I can protect this folder, and I found one from other website that one can just throw in an .htaccess file inside the folder and it will disallow the access. It was easy I said to myself. I have created the suggested .htaccess file with the following code below. This is from one of the forum I visited this morning.

Order allow,deny
allow from myDomainDotCom


Now my problem with the code above is that the embed code coming from my site cannot play the video. I only want to restrict browsers from viewing the uploads folder, but at the same time the files inside it will be still accessible by my embed code, and by myDomainDotCom php files.

Can somebody please help, or guide me where to start.

Thank you very much for your time.

Michael


corz - 05.08.08 6:56 pm

Michael, the .htaccess code you have is wonky. I recommend you start at the correct page, and follow *my* examples, instead!

allow from myDomainDotCom

..is a nonsense. Mainly because requests, even internal HTTP requests won't be coming from that domain (that's assuming Apache can even translate the domain name into an actual IP). If they are coming from anywhere, they are coming from 127.0.0.1, aka "localhost".

Having said all that, you need to keep in mind that if the user's browser needs to access the files directly, you MUST allow access to the folder. Only if your script accesses the media files via the file system (not HTTP request) and then serves them up to the users browser via some kind off "passthru" functionality, only then can you deny all direct access to the folder. If it does use a local HTTP request, then you can deny all except 127.0.0.1. The former system is better, though.

But like I say, if you need more, go to part one, where I deal with all this (this page is about rewriting), and check out the examples there and in the comments.

Have fun!

;o)

ps. thanks for all the flowers guys, you know that's just food for my funky new front-page random quote script! smiley for ;)


Predrag - 08.08.08 9:56 pm

Wow, you tutorial is amazing....

I have one problem.

I have a site on www.site1.com with all content.
And I have second site on www.site2.com who must when someone go on address www.site2.com to show all content from www.site1.com but in address line in browser must see www.site2.com.
Everything must be like is two different sites but it is only one with two domain name.

Can you help me about this? How can I do this with .htaccess?

Thanks,
Predrag

If they are on the same server, simply use regular internal redirects pointing to the file system. If they are on different servers, you can't use .htaccess to cloak the real URL, as an external redirect would be required. In that case, use a frame or similar construct. - OMG! I just recommended someone use a frame! smiley for :eek: I'll burn in hell for that! ;o)



Justin - 11.08.08 4:02 am

Okay, new problem.

My .txt files, .js files, and .flv files are all blocked now...and the .txt and are still usable in the way i want...but my .flv videos still aren't. And my .js file wont do what it's told on an .html page that accesses it...

And, on more of a side note, every time I upload a new .htaccess, close out of the ftp editor, and open it again, the .htaccess file is there, because i know it is, and because it's still in effect, but i can't find it in the ftp editor. I'm confused. It doesn't even ask me if I want to overwrite the file when I upload a new .htaccess . How can I fix this?

Thanks,
Justin

I'm sorry, I've forgotten what all that blocking stuff was about - don't be afraid to post code. As for the invisible .htaccess; you don't explain what you mean by "ftp editor", which is a term I'm not familiar with. "ftp client", yes, for moving files between your computer and an ftp server; and "text editor", for editing text, perhaps directly on another server using the ftp protocol; but "ftp editor" is new to me. Even that one question is actually more than one question.

It sounds like you need to have a good rake through the preferences of your ftp client (if that's what you mean). Or else switch to method B; use a text editor to edit .htaccess files directly, via ftp; works great. ;o)



BlackHatEmpire.com - 13.08.08 2:31 pm

Congrats on a very good comprehensive HTACCESS guide. Well done, was very helpful and structured well. I wish more websites would be like yours!

Ahh.. We are a dying breed; literally! ;o)



gulflee - 15.08.08 3:24 am

Hi I had re-direct the url

(category)
www.example.com/?p=12 --> www.example.com/category/another-abc.html
and
(sub-category)
www.example.com/?p=1 --> www.example.com/category/subcategory/abc.html

but I dun want the subcategory i m thinking is it possible to rewrite the URL from

www.example.com/category/subcategory/abc.html --> www.example.com/category/abc.html

thanks in advance.

Yes, it's possible. ;o)



Taek - 19.08.08 5:31 am

I am looking to use url re-write rule and I'm not sure what I can use to do what I am looking for i will setup my httpd.conf accordingly but i would like to doing something like

media.somedomain.net/data/file.jpg
to
files.media.somedomain.net/file.jpg

also would like to do something like
media.somedomain.net/data/someuser/file.jpg
to
someuser.media.somedomain.net/file.jpg

I know this seems stupid but I plan on having a small image hosting system and this would help alot.

Sounds like a plan. Was there a question? ;o)



Jaunty Mellifluous - 19.08.08 3:39 pm

Hi,

Thanks for the nice tutorial.

I'm using this code from your examples to run my site from a subfolder in my root hosting space.

..meaning I've placed my site in a folder inside my root folder. /root/roadsout/ << like that.

RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} roadsout.com
RewriteCond %{REQUEST_URI} !^/roadsout
RewriteRule ^(.*)$ roadsout/$1 [L]


so that's the code. It works fine. But the problem is, when I browse to the links within my site, the url becomes something like this, http://www.roadsout.com/roadsout/miley..

I want to url to look like http://www.roadsout.com/miley.. all the time. So that it doesn't show the extra subfolder inside my hosting space.

Please help. Thanks.

The downside of blatant self-promotion ("example.com" works well), is that I get to see what kind of site you have, and thereby ignore you intentionally. ;o)



AskApache - 20.08.08 11:41 am

@Corz

Hi I just published an advanced mod_rewrite tutorial that you might find interesting. It shows some sick methods for viewing the variables available to the mod_rewrite engine by adding them to the REQUEST headers. Once set up, you simply request a debugging file like your "du" php file and variables that were once hidden from the php interpreter are now populated.

Love the site and I've been waiting for htaccess3.php for quite some time now...

-AA


ckyeu - 25.08.08 9:11 am

This article is very good. however, I have run into a problem when I want to rewrite this url http://myexample.com/memberprofile.php?user=xxx yyy to maybe http://myexample.com/xxx-yyy or http://myexample.com/xxx+yyy. I was able to rewrite for http://myexample.com/memberprofile.php?user=xxx to http://myexample.com/xxx. It is a live site and I didnt foresee that people would register with names with spaces :(. (the name has a space i.e. xxx yyy)

Thank you guys in advance for your help..

Thanks for this tutorial too..smiley for :)

Just send them to memberprofile.php, and use php to grab the name (from REQUEST_URI, REDIRECT_URL, etc. ;o)



Peter - 26.08.08 3:54 am

Similar to this guy's question:

Wow, you tutorial is amazing....

I have one problem.

I have a site on www.site1.com with all content.
And I have second site on www.site2.com who must when someone go on address www.site2.com to show all content from www.site1.com but in address line in browser must see www.site2.com.
Everything must be like is two different sites but it is only one with two domain name.

Can you help me about this? How can I do this with .htaccess?

Thanks,
Predrag

My question is very similar except that I'm building a Joomla site that is absorbing two or three sites that have their own domain names.

Example, say I had a site called "chevy.com" and I was rebuilding it, bringing into it sites like detroitchevy.com and toledochevy.com, as they were going to be different "sections" within this chevy.com site running on Joomla. The puzzle for me is this: I want those users who are going to detroitchevy.com to go to the detroit chevy "section" of chevy.com and keep detroitchevy.com in the URL somehow. The same would hold true for toledochevy.com. Is this even possible?

Yes. ;o)



Andy - 28.08.08 3:46 pm

Hi

Excellent site; I have already used some of the ideas. I think you forgot to talk about switches [NC] etc - you said you would come onto those later.

Thanks
Andy

In truth, I find it hard to get motivated about .htaccess - maybe one day! I'm guessing I probably used the word "probably". ;o)



doru - 29.08.08 6:01 pm


sorry to badder you
my site is php site
I own an web site and hire someone to make my links shorter.
but they made it in 9 folder

example.com/a/b/c/d/e/f/g/h/j.html

what I want is example.com/state/city-category-produs/

and he said is not possible , is he right ?

No. ;o)



Rrryan - 02.09.08 7:48 am

Very Helpful page. Thank you!

I'd been looking for an elegant way to strip 'www' out of all requests and this does it simply and perfectly, though I had to hard code in the domain name and lose the '%1'. Thanks!

Like I said; not all servers allow the %1. But still a great trick! ;o)



unokpasbaxaki - 13.09.08 3:04 am

I'm Spanish and my English isn't perfect, so sorry if I make any mistakes.

I'm testing my website on a XAMPP on my computer (XAMPP is a tool which includes Apache, MySQL and some more things for making up a development mirror). I tried some rewriting code, but I got a 500 error. I tried several things to make it work, but the only thing that worked was commenting the all the rewrite lines.

Then I tried to copy those directives to the Apache configuration file, like this:
<Directory "C:/xampp/htdocs">
    [...]
    
    RewriteEngine on

    [rewrite code]

</Directory>

And at restarting Apache, I got this error:
Syntax error on line 231 of C:/xampp/apache/conf/httpd.conf:
Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration


What can I do?


unokpasabaxaki - 13.09.08 5:14 pm

I've already found out what was wrong. In httpd.conf, the line to load the rewrite module was commented out.

If it doesn't work on your server, search for this line:
#LoadModule rewrite_module modules/mod_rewrite.so
and remove the #.

Apache setup 101, m8! It's still a good catch, most Apache installs have it uncommented by default. By the way; I DO NOT recommend XAMP (or any one of the many similar packages), the basic installers from Apache.org et al, are excellent these days, and working with the servers in that form gives you solid, transferable skills. It the long run, it usually ends up being a lot less work, hassle, and not to mention, just as "user-friendly" as these so-called "easy installs". ;o)



Nizzy - 21.09.08 12:38 pm

A challenging question!

Is there any way to generate a random string with .htaccess?

I like to accomplish this:

http://www.sitename.com will redirect to http://www.sitename.com/.xyz123/

.xyz123 is a random string generated by .htaccess.

thank you.

It can't be done entirely in .htaccess, but it would be trivial to do in php, so simply redirect to a php handler (or use php's auto_prepend_file in .htaccess), and use php to send a (random) location header. ;o)



simon - 22.09.08 12:40 pm

just looked you up when i had a little htacess question and found the answer on your page. thanks. simon.


banabcsee - 23.09.08 9:35 pm

Hy,

my purpose is to pass the full request uri to index.php?q=..., and i tried this wonder on localhost.

the errordocument section was excellent, i could redirect it easy, but i found a lot of error messages in the log - because of the 404 errors although it was handled by my script (i sent a status 200 OK header, but no result)

now, i try to use RewriteCond, RewriteRule, etc, but it causes 500 server error.
in httpd.conf i set "LoadModule rewrite_module modules/mod_rewrite.so", but the error message doesent change.

so, my question is: how can i solve my problem with mod_rewrite, or how can i use a 'silent' mode to hide error messages?

thanks a lot,
banabcsee

---

i found meanwhile the solution: i had to restart the apache smiley for :eek:
sorry (:


Michael - 05.10.08 11:25 am

Hi,

Fantastic article, I'll be back to read the rest soon.

I think your bit on 'Inheritance' is something I'm needing to look into.

Do you know of a way using .htaccess to ignore a folder.
I have a sub-directory in my main root called 'zenphoto' and I don't want the .htaccess file in my main root to even look at this folder.
Any ideas?

Many thanks for this great resource!

.htaccess doesn't really look anywhere. Is it web browsers you are trying to deny access to? Or something else? Or did you want to disable rewriting in a sub-folder? For a better answer, I'll need more details; though I suspect when you come back to "read the rest soon", you'll probably find what you are looking for. ;o)



Paul - 15.10.08 8:11 pm

Hi! I'm trying to redirect using a similar style to what I have used in the past and use for other pages on our site. The first line shows what I am trying to do and the second the command I am trying to use to do it.

# http://www.site.com/product_category.php?category_id=75a6fcc1df7dc77d1cd7ab39b7e45549 => http://www.site.com/product_category.php?cat_id=sheet-protectors
RedirectMatch (.*)(?i)product_category\.php?category_id=75a6fcc1df7dc77d1cd7ab39b7e45549 http://www.site.com/product_category.php?cat_id=sheet-protectors

I tried it both with and without the \ in front of the .php and that did not seem to be the problem. Any suggestions?

That's mod_alias; try mod_rewrite (what this page is all about) and you might have more luck. ;o)



Torbjørn Torsvik - 20.10.08 2:26 pm

Hi. Is it possible to make a htaccessfile check for certain SESSIONS?

HTTP is a "stateless" protocol, a.k.a. "sessionless"; there is no such thing as an http session. If you are referring to sessions created by some server-side language (e.g. php), then you need to use that language to check for them.

Note: You can check for cookies with .htaccess. If you want to know more about that; drop a comment on the other page - this article is about mod_rewrite. ;o)



D. Rich - 22.10.08 3:56 am

I used the information you provided to solve my Joomla subdirectory problem.

I added a link back to this page. Great work & Thanks!


Jesse - 23.10.08 10:49 pm

Hi I'm trying to do a 'controlled' redirect..I've been searching everywhere for some answers I'm wondering if you might have an opinion.

I have (many) old links that are being displayed like they were before I implemented mod_rewrite.
I'd like for the user to be able to still click those links, and end up at the exact same place but see the rewritten url in the address bar...

I have:
RewriteRule songs/(.*)-(.*)\.html songs.php?page=$2&song=$1


ok, so:
http://localhost/songs/abc-22.html ends up at http://localhost/songs.php?page=22&song=abc

however I have thousands of links still showing songs.php?page=22&song=abc ...

I've tried many things, the best I can describe my guess for the solution is:
RewriteCond %{QUERY_STRING} . 
RewriteRule ^songs.php?song=(.*)-(.*)$ /songs/$1-$2\.html [R]

This bulletin looks like it's been recently used and answered by you so I'm leaving my question..
Thanks in advance,
Jesse


corz - 24.10.08 1:54 pm

This is a very good question.

Firstly, I'm surprised you got your initial rewrite to work, having used "songs" as your virtual "directory", rewriting to an actual script called "songs.php". Many people attempting to replicate this would be left scratching their head, wondering why it didn't work.

I have to assume you either realized that Apache's content negotiation would mess it all up, and disabled Multiviews..

Options -MultiViews

.. or else you got lucky, and it was already disabled. Either way, it's a gotcha I had to mention, in case someone else reads your post and tries something similar.

So, you have your new flat links, and want to rewrite these, as well as existing dynamic links, so the the user sees only the new flat links in their address bar, while every request is rewritten to dynamic links in the background, regardless of its original form. I like your goal, which on the surface seems "elegant in its simplicity", as villains often say; but in practice, is thorny.

Personally, I would run sed over the entire site, and physically switch out all the old dynamic links for flat links, be done with it. However, I'll assume that's not possible or feasible, and continue with a purely .htaccess approach..

In your experimentation, you've likely discovered that getting both parts to work together is tricky; each individual rewrite works fine on its own, but together, put the browser into an infinite loop.

You probably also discovered that "L" flags (which instruct Apache to stop processing rules at that point), are not enough to prevent reprocessing on their own because in the per-directory scope (.htaccess, as opposed to httpd.conf), a redirect will re-inject the request at the server level, causing processing to begin again from the first RewriteRule; and there's that loop again. There is a solution.

The first part; catching the flat links, and transparently rewriting them to dynamic links; is simple enough..

RewriteRule songs/(.+)-(.+)\.html songs.php?page=$2&song=$1 [NC,L]

The thorns appear when you try to catch those pesky dynamic links on your old pages, rewrite them visibly into flat links in the browser's address bar, and then have them rewritten transparently back to dynamic links with the first rule (above). To accomplish this, we'll need to employ two "tricks". Here's some code..

RewriteCond %{THE_REQUEST} page= [NC]
RewriteCond %{QUERY_STRING} page=(.+)&song=(.+) [NC]
RewriteRule ^songs\.php /songs/%2-%1.html? [NC,L,R]

The first trick is the use of %{THE_REQUEST} in a RewriteCond, which matches only the original request (in its entirety), i.e. the actual link that was clicked, or typed into the user's browser. This prevents Apache applying the rules to requests that have already been rewritten. In this example, the page=, ensures that only requests with a query string containing that variable are affected. song= would work just as well.

The second trick is the use of the "?" at the end of the target (.html?), which essentially strips off the query string, again, preventing infinite reprocessing.

There are other ways to achieve a similar effect, but that code or something very like it will do the trick.

Have fun!

;o)

references:
In fact, the second [L] flag is redundant in this example, but I added it because it's generally a good, efficient practice.



aconana - 26.10.08 8:41 pm

Many thanks for this extended enlightenment on good htaccess usage! I hope you can tolerate another three questions - a typo (or not?!?), a basic misunderstanding, and something in between.

•First code snippet (after the 6th paragraph) is the second line missing the end-of-string delimiter, i.e. should it be
RewriteRule ^(.*)\.htm$ $1.php

instead of
RewriteRule ^(.*)\.htm $1.php

That would be "not". Anchors are completely optional. It should, in fact, read.. RewriteRule (.*)\.htm $1.php, because it would better suit the poem. ;o)


•When does / doesn't (.*) pick up the query string? For example, in the "shortening URLs" section, the sample URL appears to give
$1 = "?file=my.zip"

but the "QSA Overkill!" sample appears to give only
$1 = "foobar.zip"

(i.e. excluding the ?foo=bar stuff). Does the [QSA] flag have anything to do with it?

(.*) greedily catches everything in its path. In the first exmaple it catches everything after "grab", and in the second, everything after "grab/". The [QSA] flag is what tags the original variables; "level=5" and "foo=bar"; back on to the target URL. ;o)


•Stripping a query string from a URL (in the "Just a demo!" code snippet) - why is even the question mark ? necessary?

The "?" is the only thing which IS necessary. As mentioned, this is what strips the URL of its query string. Technically, it creates a URL with a single empty query variable, which is automatically stripped off when the URL is re-injected. ;o)


Thanks for any further patience you have with this nit-picking!

No problem! ;o)



Jesse - 27.10.08 5:36 pm

Thanks so much for your interesting response. It's enlightening to say the least. mod_rewrite is a tough module to learn, there's so much deep stuff going on.. It moreless becomes a matter of learning the internals of apache. You're right about just editing all of the old stuff into the new links. The links are actually in a database I believe, I'm a freelancer so it's not my site.
After I thought about it for a while, it became obvious that mod_rewrite is mostly made to make 'fake' stuff rather than work with 'real' stuff. For example redirecting on a file that exists will never work... and that's the dead end that needs to be realized I think. This site is awesome. Thanks for your input!

While it's true that mod_rewrite is mostly used to "fake" stuff, there is nothing to stop you rewriting from a file that exists. Why you might want to do this, is another matter. mod-rewrite provides almost limitless possibilities; being processed before the browser gets the page.

As for your links; a database, that's even easier; one simple command (in phpmyadmin, for example) would change them all, something like this..

UPDATE tablename SET tablefield = replace(tablefield,"oldstring","newstring");

You could also add a "WHERE" clause, for more specificity. Have fun! ;o)



martín - 28.10.08 8:11 pm

I've changed my "oldsite.com" to a new site "newsite.com"
The oldsite was on php and the new on html.
Pages in google are listed as "www.oldsite.com/contact.php" which I would like to redirect to "www.newsite.com/kontakt.html" or www.newsite.com

installed this, but it only works if you enter the oldsite name, www.oldsite.com. not with the .php pages

DirectoryIndex index.html index.htm index.php
IndexIgnore *
RewriteEngine On
RewriteCond %°HTTP_HOST° ^www.oldsite.com$
RewriteRule ^°.*°$ http://www.newsite.com/$1 °R=301,L°
RewriteCond %°HTTP_HOST° ^oldsite.com$
RewriteRule ^°.*°$ http://www.newsite.com/$1 °R=301,L°

°=bracket

If this post is some kind of joke, worry not, I'll be deleting it soon enough. If it's not a joke, then please a) frame a question that makes sense, giving as much detail as possible, and b) read the above article (including the huge notice above these comments) first. ;o)



Paul - 29.10.08 5:06 pm

I am trying to rewrite from:

http://www.site.com/product_category.php?category_id=75a6fcc1df7dc77d1cd7ab39b7e45549

to

http://www.site.com/product_category.php?cat_id=sheet-protectors

Per your suggestion above I have stopped using mod_alias and started using mod_rewrite (because, after all, that is what this page is about). smiley for :evil:

But, it still is not quite right. Can you take a look at the commands below and help me?

RewriteCond %{HTTP_HOST} (.*)(?i)product_category\.php\?category_id=75a6fcc1df7dc77d1cd7ab39b7e45549
RewriteRule /product_category.php?cat_id=sheet-protectors 


Thanks again.


corz - 29.10.08 6:03 pm

Paul, you clearly don't understand how this works. Did you read the article at all? I mean, how on earth could the string (.*)(?i)product_category\.php\?category_id=75a6fcc1df7dc77d1cd7ab39b7e45549" match against your domain name? That's nuts! And the RewriteRule beginning with a forward slash; again, the article (above) explains why you would not do this inside an .htaccess file.

Och well, something like this will work..

RewriteCond %{QUERY_STRING} category_id=75a6fcc1df7dc77d1cd7ab39b7e45549 [NC]
RewriteRule ^product_category.php /product_category.php?cat_id=sheet-protectors [NC,R]

However, is that long category_id really necessary? If there are many such id's corresponding to many cat_id's, then fair enough; I guess you'll be writing lots of RewriteRules (in which case, add an "L" to the flags). If there is only one, then you can omit that long string from the RewriteCond, and simply check for the existence of category_id.

If there are lots of id's to map, it would be easier done in php (or similar), creating a mapping list somewhere, and rewriting all queries to the php handler.

;o)


Daniel - 30.10.08 11:34 am

Hi, I wish to acheive something similar as Paul, I have a page, dealing with animals, and the get variable determins the id to sql out of the database to get all the info but I want to rewrite the url from this:

www.example.com/species.php?id=18

to this:

www.example.com/common-toad-bufo-bufo.html

Is it possible to query a database to get the data I want to do the rewriting or must I make a map (?) with each id corresponding to a certain string of information I would use? I like the sound of Php handler, what does this consist in?


Paul - 30.10.08 4:49 pm

Thanks for your help. Of course what you suggested works. You were only half right on your points, though: You are right that I clearly don't understand how it works. I have read the article many times over the years and have spent many hours trying to understand it. In the end I can't tell my foo bar from my egg foo young.

Addressing your code (or my implementation):
RewriteCond %{QUERY_STRING} category_id=046216723ed2bab34a3dab9fd01fdb19 [NC]
RewriteRule ^product_category.php /product_category.php?cat_id=sheet-protectors [NC,R,L]


Is this what it is saying: If the string equals category_id=046216723ed2bab34a3dab9fd01fdb19 (no matter what the case) then rewrite it so that it uses everything before product_category.php and replaces it with everything before the string plus /product_category.php?cat_id=cleer-adheer-laminating-film (no matter what the case, redirects and ends the rule)?

Also, what is the difference between mod alias and mod rewrite?

By the way, the links in your conclusion don't work for me as they just go to http://localhost/manual/misc/rewriteguide.html

Again, thanks for your help.

Paul


corz - 30.10.08 9:26 pm

No problem, Paul. Of course what I suggested works! Half right? That means I must also have been half wrong. Tell me exactly what I was wrong about, and I'll give you the dang-diddley abc of exactly how the code works.

For the difference between the two Apache modules; see here. Also check out the useful links section below the article.

By the way, I might have been tempted to dive into a full explanation right away; but I know you aren't paying attention. For example; did it not occur to you that not one but three links in an article read by thousands of people every day, might be correct; and instead, you are in error. You're skipping! The paragraph begins, "If you have apache installed on your system..". localhost is your machine! If you're not Apache'd-up, of course the links won't work.

And where did the cleer-adheer-laminating-film come from? smiley for :eek:

Daniel, what goes on in your back-end is your own business! As for rewriting, at a minimum, simply rewrite all requests to species.php, creating a variable with the custom string, and let that script do all the work. species.php then, is your "php handler"; it handles the variables.

Somewhere; in a database, or a plain text ini-type file, even inside the handler itself, the string "common-toad-bufo-bufo" is translated to the integer, "18", which I assume means something to the script; an sql table id, or whatever. At any rate, "18", and its associated data are now available for processing by your script.

How much of all this is handled by .htaccess, and how much by your php script, depends on the number of transformations (mappings) you need. One or two would be simpler in .htaccess; one ruleset per mapping; but a longer list would be better handled by your server-side code; i.e. php.

Decide what you need, and try a few things.

;o)


Jesse - 31.10.08 1:37 am

Just a quick note to some people posting on here. mod_rewrite is used best in making simple links that only involve 2 or 3 variables at the most. Take for instance

www.site.com/food/fruit/banana.html

looks much better than

www.site.com/index.php?category=food&sub_category=fruit&name=banana

..ideally if everything is planned right from the beginning, then you can put all the info you would ever need to in the link without any &,=, or ?.

Thanks for the input! Also see here. ;o)


one more thing completely different but same topic is you can use php header("Location: somefile.php?stuff") to make up for anything that doesn't otherwise with mod_rewrite.. it's a dirty hack and i don't recommend it but it works..

Though DO read the manual for that. ;o)



Amin - 31.10.08 6:45 pm

If I want my url to look from this

http://www.mysite.com/index.php?t=browse_artist&letter=a

to

http://www.mysite.com/browse_artist&letter=a.php

How can I write the mod rewrite, I am new to php and linux.

Thanks

Simple create a new plain text file called .htaccess in the root of your site, and put the rules in there. ;o)



Rohan - 02.11.08 12:04 pm

If you are familiar with regular expressionssmiley for :eek:, you can master URL rewriting under 2 minutes! smiley for :D

And if you put all the worms in the world end-to-end, they would reach from here to the Moon. Oh, no, wait a minute; the one in the middle would probably wriggle and spoil it. ;o)



Paul - 07.11.08 8:31 pm

The part where you were half wrong is where you imply I did not read the article. And I did not say the links did not work, I said they did not work for me...smiley for :evil:

Thanks for the links to the difference between alias and rewrite.

Paul

Exacitly my point! If you'd read the beginning of the paragraph, you would have known that the links only work when you have Apache installed; which is something I encourage all serious webmasters to do. ;o)



Andrew - 09.11.08 5:23 pm

Hi there,

Thanks for the guide. I'm in the situation of having two domains on the same host; I've been trying to use the htaccess file as you've outlined above, but with limited success. The links are pointing to the right files/folders, but the server seems to be 301 redirecting the URLs rather than rewriting them. The new URLs appear in the browser address bar, and the server seems to be sending a 301 redirect, according to web-sniffer.net.

Anyway, if you've any ideas why my code isn't working, I'd be glad to hear them. Apologies in advance for any obvious errors.

RewriteCond %{HTTP_HOST} myfirstexample.com 
RewriteCond %{REQUEST_URI} !^/ga 
RewriteRule ^(.*)$ ga/$1 [L]  

RewriteCond %{HTTP_HOST} myseconddomain.co.uk 
RewriteCond %{REQUEST_URI} !^sp 
RewriteRule ^(.*)$ sp/$1 [L]

Thanks!

Andrew

A slash in front of the "sp" in the second ruleset's RewriteCond would be a good idea. Aside from that, it all looks fine; I can't see anything that would force a 301 external rewrite. Feel free to email me with your actual domain name and full .htaccess file - I'm curious. ;o)



Catherine - 13.11.08 7:26 am

Fanastic - after searching high and low your page fixed my problem. I could kiss you :-)

Hey, if you're local to Aberdeen, feel free! ;o)



evipbg - 18.11.08 5:48 am

Brilliant!

Thank you, a lot.


Pratik Thakkar - 18.11.08 9:31 am

Hi,

I am facing problems with multiple .htaccess files - one in parent and one in subfolder.

My parent .htaccess file
RewriteEngine on

RewriteRule ^(images|swf|includes|admin)(.*) - [L]
RewriteRule ^((.*).html|(.*).rss|^(\d+)$|(.*)/$) /index.php [L]


Now .htaccess in the folder "/admin"
RewriteEngine on

RewriteRule ^admin/(.*) /admin/index1.php [L]


I want all pages in the directory admin and its sub-folders to go to /admin/index1.php.


But I always get an error saying "Access Forbidden!" - You don't have permission to access the requested directory. There is either no index document or the directory is read-protected.

The only php file under /admin is index1.php.

How do I acheive this? Am I doing something wrong?

Any help will be highly appreciated.

Kind Regards,
Pratik Thakkar

Try.. RewriteRule .* index1.php [L] ;o)



reub - 21.11.08 5:28 am

oh course im a bloody human!


tech - 21.11.08 4:14 pm

An excellent tutorial, thank you! I still have one tricky problem, though:

I have one host with three domains pointing to it (xxx.eu, xxx.de and xx.com). I have Joomla running from the root on my server, and in Joomla the Joomfish multilanguage componenthandles the translations and a SEF-component handles the nice outlook of the url:s. A normal url with no translation looks like 'http://xxx.eu/index.php?/myfile.htm' and gives the default language page. When I select another language it changes to 'http://xxx.eu/index.php?/nn/myfile.htm' (where nn is the language code). All three domains work identically, from the root.


My problem: I would like to redirect all people using xxx.com to the english-language version of my content, and all people using xxx.de to the german-language version. Would that be possible using rewrite, and if so - how? I have tried several solutions but usually get stuck in a loop... (Rewrite to sub-folders work fine, but I do not want to have three different installations of Joomla just for the different languages...)


smiley for :eek:




Matt H - 22.11.08 2:01 pm

Excellent write-up.
Just wanted to say thank you.


adri - 24.11.08 10:51 am

Hi, I'm trying to redirect
mypage.php?var1=var1&var2=var2
to
var1=1&var2=2&var3=3

Another requests could be:
mypage.php?var1=var1&var4=var4
to be redirected to
var1=1&var4=4&var3=3

mypage.php?var2=var2&var5=var5
to be redirected to
var2=2&var5=5&var3=3
(and more requests...)
I was using something like this:
RewriteEngine On
RewriteRule ^mypage.php(.*) /$1&var3=3[QSA,R,L]

But it doesn't work, this redirect to:
&var3=3?var1=1&var2=2

Do you know what I could do to put that
&var3=3
behind?


Sure, adri. I'll assume you are rewriting to "index.php" (you have to be rewriting to something). You could do it like this..

RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^mypage.php /?%1&var3=3 [R,L]


;o)



adri - 24.11.08 3:05 pm

Oh, it works!

If I don't want to rewrite mypage.php when request has not vars I suppose that it should be:

RewriteCond %{QUERY_STRING} (.+) 
Correct! ;o)



Thank you very much, you opened my eyes! (sorry my english )



Anthony - 24.11.08 5:44 pm

Hi,

This is really a nice tutorial for .htaccess beginners like me. Any ways, i want to convert http://localhost/anto/about.php file to http://localhost/anto/about just removing the php extension. Any suggestions for me???

Thanks for time and help,

Happy coding smiley for :)

Suggestions? Yes. I suggest reading the article carefully, and then trying a few things. After this, you will be empowered, and won't need to ask anyone else to do this stuff for you. If you still have issues, come back and post the actual code that is troubling you. Also, you might want to load this page with all comments, and do a type-to-find for "extension". ;o)




Anthony - 24.11.08 7:13 pm

HI,

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(about)$ about.php

I TWEAKED IT,THIS WORKS FOR ME smiley for :)


Philip Arshe - 28.11.08 6:18 am

Hi! Here's the difficult one ever !
what about trying to do:

www.whatever.com/whatever.whatever
into only
www.whatever.com/

Is that possible ?

Possible. Yes! Difficult? No. In fact it is so trivially easy, that even my dog could do it. ;o)



HAS - 30.11.08 6:18 am

hello,
nice tutorial.

do you have a way around missing the relative links for images and css files?


request url:
http://www.example.com/articles/show/1

original url:
http://www.example.com/index.php?act=articles&cmd=show&id=1

the problem faced here
that all images and CSS are missing.

will be great help if u have any guide. thanks

Load the page with all comments and do a type-to-find. I'm fairly sure the info you are after, is there. ;o)



Siteman - 01.12.08 6:27 am

Hi Cor,

After 6 mths of searching I finally "found" your site today - best practical info on doing useful stuff with .htaccess I have ever seen!

So now I think I have finally been able to resolve the issue of canonisation of my site, eg have all combinations of example.com/index.htm, www.example.com/index.html, www.example.com/INDEX.HTML, etc. point to the same URL (www.example.com) - which search engines really love... So here goes:


# Canonise all page URLs to start with "www."
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]

# Convert all index.htm* to base URL (case independent)
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^index.htm http://www.example.com/ [NC,R=301,L]


I hope I got it right? Have I missed anything?

S.

Missed anything? Nope. In fact, you've done too much! The second RewriteCond is superfluous. Also, I suggest you use "NC" in the first RewriteCond (along with an "L" in the RewriteRule), so you also catch EXAMPLE.COM/etc.

And while you are still at the "unimplemented" stage of your canonization, consider not using the "www", it's deprecated. See here for more details.

Six months to find me? Crazy! Should have tried Google! I'm surely in the first page for any .htaccess-related search! ;o)



Siteman - 01.12.08 12:21 pm

Cor,

Well, you are indeed in the Google top-10 for "htaccess", but I had always included "Apache" ... smiley for :erm: :blush:

When I tried your suggestions, I found:

1. If I delete the 2nd RewriteCond (= the last 3 lines), then www.example.com/index.htm is not canonised to www.example.com.

2. EXAMPLE.COM/index.htm is canonised to www.example.com even without adding NC to the first RewriteCond (= on the 3rd line)

Not sure why this is - or have I misunderstood your comments? I use shared hosting, so maybe there are server-wide htaccess settings that affect my htaccess file?

Many thanks.

S

PS: I did read no-www.org ... interesting points...


corz - 02.12.08 12:30 am

Hi.

1. Yes, you misunderstand my comment. I said nothing about removing the second RewriteRule!

2. It's likely that your browser lowercases domain names, but unless you do it in .htaccess, you can't guarantee that it's happening on other browsers. In other words, that's a browser thing. In fact, you had that in already, unless I corrected it without realizing - wouldn't have been the first time!

I like your idea of converting hits for example.com/index.htm to plain old example.com/. It's better SEO, and neater, too. But why stop at your root?

Here's some code similar to what I have in my site-wide init-file. It runs at the very beginning of request processing..

<?php
if (substr($_SERVER['REQUEST_URI'], -9) == 'index.php' and empty($_POST)) {
    
header('Location: http://'.$_SERVER['HTTP_HOST'].str_replace('index.php'''$_SERVER['REQUEST_URI']));
}  
// this code could be easily improved!
?>

I'm assuming you can run php scripts, and have somewhere to put this - if you don't have any init/header files, you can use php's auto_prepend_file to have a script run before all requests are handled, e.g. (in .htaccess)..

php_value auto_prepend_file "/var/sites/example.com/inc/index-handler.php"

Obviously, for your site you would need to replace index.php with index.htm. Note that we check for $_POST variables, so as not to mess with form input (bots won't be using the forms, so there's no SEO impact).

for now..

;o)

p.s. Top ten for just "htaccess" on its own! w00t! smiley for :D


chris - 02.12.08 8:22 pm

first off thank you very much for this tutorial, I've been designing webpages for a while but I'm pretty new to the admin side and this has been a great help

my company is going from coldfusion to plain html, and I tried writing a Rewrite rule as best I could per your examples, but it still seems to be ignoring everything after .cfm

so ex http://mysite.com/content.cfm?n=company needs to become http://mysite.com/company.html

my rule:
Options +FollowSymlinks
RewriteEngine on
RewriteRule /content\.cfm\?n\=(.*) /$1.html [R,NC]

still gives 404 "The requested URL /content.cfm was not found on this server"

I've tried variations with and without the escape char \, but everything still gives the exact same 404 error message... if you could point out where I'm going wrong, that would make life soo much easier.

Agreed. Once you see where you are going wrong, life becomes much easier. I can see where you are going wrong, because I've gone wrong in the exact same way myself, many times.

I would gobble up some information and dive into a "solution", and when that didn't work, try a variation, nope, curse, try another, and another, until I was so utterly frustrated that I either had to abandon the problem altogether, or try a completely different approach, sometimes following the exact same cycle with the alternative. What a drag.

The answer is simple; RTFM. While you were skipping over the above article, you missed the section entitled "capturing variables". It's my fault for only using an <h3> tag. Either read that, or for a sizeable donation, I will provide the exact code you need, simple as it is. Also, lose the forward slash. ;o)



chris - 03.12.08 9:40 am

lol I did actually did read the capturing variables part, and tried copying and substituting, but I had no luck so I thought I might be over complicating things... I'm a photo/video/design guy so the most scripting I've had is a semester of JavaScript. But after reading it about 6 more times and a few dozen attempts I appears to be working properly smiley for :D

RewriteCond %{QUERY_STRING} n=(.*)
RewriteRule ^content.cfm(.*)$ /%1.html? [r=301,NC]

The confusing part is that logically it seems the query string should be n== or escaped as n\== , 1st = as last character of the url, and a 2nd = for the equation itself.. and not realizing I didn't need ?n= in the RewriteRule

Thanks again!

No Problem! Note, in your RewriteRule, you don't need the anchors (^ & $) in the match string. Nor do you need the (.*), because you aren't capturing anything in the RewriteRule, only in the RewriteCond..

RewriteCond %{QUERY_STRING} ^n=(.+)
RewriteRule content\.cfm /%1.html? [R=301,NC]


And of course you used [R] until you had it working 100%, and then switched to [R=301]; as advised; or else you probably had a hellish time getting it to function. smiley for :lol: Nicely caught with the ? at the end of the target. All's well that ends well, eh! ;o)



Syam - 03.12.08 9:18 pm

I'm having headache with my .htaccess

here is my directory layout

/www
    /example
        /zf
          .htaccess
          
          /fckeditor
          /media
          /library
          /webroot
              /images
              /scripts
              /index.php


I use mapping file for my vhost (assume: example.zf /www/example/zf)

and here is my .htaccess. The problem is whenever i use http://example.zf/?key=value it seems to read /www/example/zf/index.php(which i have now) but I want it to redirect to index.php in /webroot folder. Furthermore, /library will give 403 but /library/Zend/Acl/Router.php still can be accessed. How can I *lock* entire /library from viewers?


.htaccess

Options All -Indexes

RewriteEngine On
RewriteBase /


 RewriteCond %{REQUEST_FILENAME} library(/.*)
 RewriteRule ^library(.*) - [F]


 RewriteCond %{REQUEST_FILENAME} !^(/scripts|/images|/lang)
 RewriteRule ^(scripts|images|lang)(.*)$ /webroot/$1/$2 [L]


 RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[.*])$  [NC]

 RewriteCond %{REQUEST_FILENAME} !-s
 RewriteCond %{REQUEST_FILENAME} !-l
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d

 RewriteRule (.*) webroot/index.php
 
 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]



corz - 04.12.08 4:01 pm

I'm getting a headache just looking at it!

First thought is, "Is this some kind of joke?". Really, who designed this layout? Don't answer that. And what's with all the multiple file system calls? And how's any request supposed to get to the php-cgi-auth-workaround-thingie line? And why the the redundant regexp. Also, I have near-zero experience of working with mapping files.

My next thought, was, "Why not map directly into webroot/ ?". Which probably proves the previous statement!

"locking" library/ from viewers is easy; from part 1 of these articles..

deny from all


Have another look at your layout.

;o)


jasmit - 07.12.08 2:45 pm

hi corz

hi read your article and comments and found a code that i needed for my site but not able to get it work.

i get an error 404 object not found.

i have mod_rewrite active.

i have url
http://localhost/jasmit/products.php?dealer_id=9&category_id=1

and i want to change it to
http://localhost/jasmit/products

i m using the below code
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{THE_REQUEST} dealer_id= [NC]
RewriteCond %{QUERY_STRING} dealer_id=(.+)&category_id=(.+) [NC]
RewriteRule ^products.php /products [NC,L,R]

jasmit

Are you sure you don't mean you want to convert FROM jasmit/products TO jasmit/products.php?dealer_id=9&category_id=1, in other words, you want users to use and see the short URL, but in the background have it send these variables to your script, right? You are working back-to-front, or more correctly, ass-backwards.

Realize, these variables have to be somewhere within the "short" url, either literally (e.g. /products/9/1/), or mapped somewhere to longer variables (inside your php script, e.g. /procucts_a/ => "a" = "1"), then go read the sections above about shortening URL's and making flat links, and then try again. ;o)



willowdan - 11.12.08 2:59 pm

I'm very thankful for this guide... Fort time for me to apply url rewrite, and this has been very helpful.

What I simply did was to preserve the id's within the new url, example:

example.com/directory/61-Business or
example.com/directory/ad-43-Sample-Ad-For-Sale-Dell-XPS-1530

... and simply made the category_name and ad subject be part of the new url.

Many thanks,
willowdan


aman - 20.12.08 3:37 pm

hi

i have a url like
description.php?id=$1

i used rewrite to make it seo friendly
RewriteRule ^product([^/\.]+)?.html$ description.php?id=$1

it gives me
product7.html

now i want to replace "product" word with dynamic word coming from database.
how can i do it.
if i manually replace "product" word with "nokia" in htacess
RewriteRule ^nokia([^/\.]+)?.html$ description.php?id=$1

then it works

what should i write in rewrite rule so that it accept dynamic word inplace of "product".
so that my url become
nokia7.html
samsung2.html
sony5.html

aman

Your database can spit out whatever links you like. All you need to do is catch the string, along with the id, and feed it to your target/script, something like..

RewriteCond %{REQUEST_URI} !description.php [NC]
RewriteRule (.+)([0-9])\.html$ description.php?id=$2&product=$1
That rule could be improved, by the way, but it will get you started. ;o)



Pedro - 23.12.08 3:53 pm

Hi there,

Great tutorial, but a bit way to confusing for me at this point, and my question is this...
I want to monitor how much bandwidth is used when files (images) are called by the users so i have set a php script to do it, and with a RewriteRule, i thought it would do the trick, but i am probably missing something as it does not work.

all files i want to monitor are in www/images/upload/username and htaccess file is in www/ (where www is the web root for the site), and so is bandwidth control file
And the rule is this...
RewriteRule images/upload/([a-zA-Z0-9_-]{1,})/([a-zA-Z0-9_-]{1,}).(jpg|jpeg|gif|png)$ bandwidth.php?u=$1&i=$2.$3 [L]

how would it needed to be set to properly trigger the bandwidth.php?


The rule itself looks valid enough, though probably over complex, and that's what's tripping you up. For example, the {1,} sections are redundant (pseudo-copying from the ILoveJackDaniels Cheatsheet, eh?), and [a-z0-9_-] is probably just fine (you can use [NC,L], remember). In fact (.+) would probably work just as well, maybe ([^/]+).

And while we're here, (jpe?g|gif|png) is neater. In other words, simplify things. Also remember, to specify an actual dot in the RewriteRule, you need to do \.

Also, actually reading the article might be helpful to you. Give it a shot!

;o)

p.s. If you post more code here, it's best to use [pre] tags (this is mentioned in the big notice above these comments, as well as in the help underneath, and error message you received when you first tried to post) - I switched your code into these, you'll notice. Much easier than messing with double-brackets. smiley for ;)



Ravin - 23.12.08 5:10 pm

Hi,

I have read this tutorial carefully n some posts have also answered my problem but still none of them solved it. Issue is very simple but i am not getting where i am going $%)(^%,. I want to convert http://localhost/Tasks/Login.php to http://localhost/Tasks (simply putting, i want to hide extension). Any suggestions for me???

Thanks for time and help.

Happy coding smiley for smiley for :)

code i am putting in my root folder.
±

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(tasks)$ *.php

smiley for :lol: That's got to be the wackiest RewriteRule I've seen all week! Your biggest trouble is that "Tasks" is an actual directory, and you are attempting to use it as a "virtual" path. Fortunately, while definitely not recommended, it is doable, try this..
RewriteCond %{REQUEST_URI} !Login.php
RewriteRule ^Tasks/? Tasks/Login.php [L]
;o)



Pedro - 23.12.08 7:45 pm

And you know... a set of fresh eyes make wonders. After all, a few minor tweaks were more than enough, and the missing \ was the mother of all troubles.

THANK YOU very much for it smiley for :)

RewriteRule images/upload/([a-z0-9_-]{1,})/([a-z0-9_-]{1,})\.(jpe?g|gif|png)$ bandwidth.php?u=$1&i=$2.$3 [NC,L]

Working as charm!

Now, all you need to do, is replace "{1,}" with "+", and you have a real RewriteRule! ;o)



Swapnilab - 26.12.08 2:12 pm

hi,

An excellent tutorial on .htaccess, thank you! smiley for :D
I am Practicing you code and some of it works for me. Now I am able understand proper use .htaccess file. earlier this i was thought .htaccess used for url rewrite and some ini setting. but its more than this.

Thank you again for this superb Tutorial.

bye
have a great time and day.


Dennis - 26.12.08 8:00 pm

I just want to express my appreciation for your excellent standards-compliant site. Your htaccess examples quickly led me to a solution for my particular application, and the Html Validator extension I'm running in Firefox reports no errors or warnings---a very rare and much welcome occurrence. Thank you!


thomas alexander - 27.12.08 11:03 am

i have a url like this

http://server/harold/searh?id=2&name=harold&job=web

i want to convert to

http://server/harold/searh/2/harold/web



i could display http://server/harold/index.php to http://server/harold/index


when i put http://server/harold/index/ the images are not listed i used the below code


Options +FollowSymlinks
RewriteEngine on
RewriteRule ^index$ index.php


can i write a single line code to rewrite all php pages in directory format or do i have specifu separately for each page



corz - 28.12.08 10:46 pm

Swapnilab. in truth, I keep looking at it and wondering how anyone can make heads or tails of it, and thinking of improvements, but there you have it; it seems somehow it gets the data into people's brain. Cheers!

Dennis; I absolutely adore it when folk notice+mention it; the usual reaction, in the 1% who do notice, is to scurry off, ashamed, to their text editor, in a frenzied gush of compliant code. At least, that's my fantasy. Thank you, I mean it. I could count, on one hand, the number of bona-fida comments I've had about the fact that every single single fucking page (expect the old OSX section, of course) is 100%, hell-yes Standards-compliant XHTML 1.0 Strict, with not even a whiff of a warning message; read, green tick all-the-way-baby! I'm guessing we both installed the very same extension (TIDY) - something I often recommend to web developers.

*deep breath*

*ahem* where was I. Oh, yeah, cheers, it actually took me 200+ hours to get the site compliant (3000+ files, you know!); another holiday I never got! smiley for :roll:

thomas alexander, yes, you can use a single line. (gets poked by conscience, continues..) But remember, if you are using relative links for the images, you will also need to rewrite those links.

;o)


Dexter - 29.12.08 4:57 am

First of all, great site dude.

I know this seems easy but i haven't found a simple solution.
We know that the "^" when not used as an anchor serve us to exclude chars from a match but i want to allow a range of numbers between 1 and 9 but excluding the "5" digit. all the examples i've found have examples like this: [^1-9] excluding any digit of that range or a specific char like this ^5 but they don't have an example which excludes a single char or a range of chars from a previous range. i tried with this [1-9]^5 to get what i want but didn't work. any suggestion?


Sure, what about.. [1-46-9] ;o)



Dexter - 29.12.08 8:11 pm

Thanks for the quick reply.

That's gonna do the dirty job for sure but as far as i have messed around with this cool stuff, which is new for me, makes me wonder: there's no way to exclude characters from a range with "^" ? If so, i'll just have to do some little hacks to get it work like you did spliting the allowed numbers in two ranges to leave "5" digit out. the thing is that for each excluded digit i'll have to split the range once again. Well, i guess they have their reasons to make posix work that way.

Anyway. Thanks again man. I already have bookmarked your site in my frecuent websites folder.

By the way, while surfing the net i found a nice regex tester made in flash. I hope it can help others to experiment with strings to mess around with regex: right here




corz - 29.12.08 9:14 pm

Hey! I didn't say it couldn't be done! A double grouping is just the simplest approach, one that would suit most people's needs. But I can think of at least two ways you could specifically exclude the "5" digit, if you really need that.

The first and most obvious, is with a RewriteCond statement, something like..
RewriteCond %{REQUEST_URI} !images/5\.png$
You would, of course, make it specific to whatever resource you are trying to not rewrite, matching your RewriteRule, except with the 5 instead of the grouping.

The second way, though less obvious, is perhaps superior, and that is a "negative lookahead". Basically, if the lookahead matches, the ReWrite is bypassed, something like this would do what you want..
RewriteRule images/(?!5)(\d+)\.png$ index.php?foo=$1 [NC,L]
Note, I used \d; a short form for :digit:, which is the same as doing 0-9. I've used the fictional images/5.png as my resource to exclude, but you can, of course, use whatever you need. Oh Joy of POSIX!

Thanks for the link. I have a few of these types of things in my bookmarks; one of which may yet end up in the useful links section.

Have fun!

;o)


kylemech - 29.12.08 10:05 pm

In the process of moving between content management systems, I'm trying to redirect users looking for old member profiles from their old location to the new format. I'm using the following rule:
.htaccess
RewriteRule ^/faculty_staff/profiles/(.*)\.asp$ /users/$1 [R,NC]
The request can come in different cases, but as I understand it [NC] takes care of that. I'm not having any luck with this rule, so I must be misunderstanding something. I suspected that my search string is incorrect, but I couldn't find any obvious reason.

Any ideas?

Yes. Read the article, and you'll know exactly why. ;o)



Dexter - 29.12.08 11:52 pm

Thanks again, i hope you're not getting bothered because of me coming back and asking again but negative lookahead stuff is kinda new for me, i thought i understood it but it seems i have not got it at all. what i'm trying to do is making friendly url's. the accepted strings go like this:

/mod/ver/any number (except 5) five times

so what i currently have is:

RewriteRule ^mod/ver/(\d{5})(?!5)$ /mod/index.php?id=$1

i thought that if i had mod/ver/12345 it would only match the string before "5" so the string wouldn't be valid because the number part would match until "1234", four numbers, not five, so it doesn't match;but no luck, it still accepts "5" as part of the string. i'm sure it's because i didn't know how to implement negative lookaheadsmiley for :roll: Been looking for a workaround but no luck finding similar examples.

Any suggestion is accepted. thank you very much

You could use a RewriteCond to do the actual capturing, and use the RewriteRule lookahead to check for the 5-not-5, e.g. ((?!5)\d){5}. Though perhaps less readable, you could also put the whole lot in the RewriteRule, like so.. (((?!5)\d){5}). There are other approaches, too. ;o)



rick - 30.12.08 5:34 pm

-Fixing paths that start with a forward slash-
A site I'm working on currently has no domain name. The only way to view it is in a sub-directory of an ip address.
Many images and links pathed are root relative I.E. "/images" since the preview of the site is in a sub-directory this breaks all of the images and links that begin with the forward slash. Is there a way to write a htaccess file to rewrite the root relative paths?(To many files to search and replace)
Something like "/images" to "preview/www/images/"

Thanks in advance.

Yes, there is a way. ;o)


This is what I have now.

RewriteBase /images/(.*)
RewriteRule ^/preview/mysite/files/images/(.*)$ /$1

Where did you get that?  No, don't answer that. The solution is quite simple. Read the article (above) and try a few things. You'll figure it out. ;o)



Dexter - 30.12.08 9:58 pm

You're the man!! Works perfectly. i also have some e-books of regex so i will check them out to get into the deep well of regex, and luckily i will come back to the surface knowing one or two tricks. Will be around. Thank you very much. i will contribute through paypal as soon as i can.

Heh, thanks. Truth is, although I've known about regexp lookahead for some time, I've not had an opportunity to actually use it for rewriting, so this was a fun head-scratcher. Good luck with the regexp learning (e-books are good, but many excellent tutorials and guides are available online for free); once you get your head around it, it sure is powerful stuff. ;o)



Sean McGilvray - 02.01.09 10:26 pm

I have the following problem.
I have my old domain that users were using to get to my site with a user name.

domain/?username

I am moving to RoR app and want to rewrite all incoming request like a above to the new address.

domain/profile/username.

How do I make this happen with .htaccess. I have tried many different ways and none are working.

Thank you,

Sean McGilvray


Jason - 12.01.09 10:12 pm

Hi,

I have read your article, and they have helped all but my final issue. Hopefully this example will help you to see what I mean without a ton of words.

I would like the URL

http://domain.edu/kgcoe/chemical/department/mission

(really we're just focusing on anything after chemical as the base) to convert serverside to

http://domain.edu/kgcoe/chemical/index.php?section=department&subsection=mission

The rules I am working on are as follows:
RewriteRule ^([^/]+)/([^/]+)/$ /kgcoe/chemical/index.php?section=$1&subsection=$2 [l]
RewriteRule ^([^/]+)/$ /kgcoe/chemical/index.php?section=$1 [pt]

On the server, I have a folder for each "section", but not for subsections, such that inside of the department folder there would be both department.php (the section) and mission.php (a subsection).

If i enter something a file that doesn't exist (/department/squiggly), it works fine. It is only when the subsection .php file exists that I have issues.

Thanks in advance!

Jason


Björn - 14.01.09 3:49 pm

Hello!
Great Article and very helpful! smiley for :) Unfortunately it didn't help me with what I'm trying to do...

Basically I want my MAIN index-page to NEVER show "/index.htm" in the address-field but all OTHER index-pages to ALWAYS show it... To achieve the first I can use:
RewriteRule ^index\.(.*)$ http://www.myexample.com/ [R,L] 

And to achieve the other:
RewriteRule ^(([^/]+/)*)$ http://www.myexample.com/$1index.htm [R,L] 

Naturally I can't use those two at the same time as that would create a never-ending loop for "http://www.myexample.com/". Another part of this problem is changing folders from format "http://www.myexample.com/folder" to "http://www.myexample.com/folder/" that is making sure there's always a "/" at the end...

Would be very thankful for any help!


Björn - 15.01.09 8:14 am

Hello again!
I managed to solve the problem on my own... Not sure if this will slow down my site in which case I will have to think of another way to do it. Anyway... Figured I'd put the code and what it does here in case someone else has similar problems... Here goes:

# Turning the magic on:
Options +FollowSymLinks
RewriteEngine On
RewriteBase / 

# Making sure the "www." is always shown:
RewriteCond %{HTTP_HOST} !^www\.(.*)$ 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R,L]

# Removing /index.* from address field for startpage:
RewriteRule ^index\.(.*)$ http://%{HTTP_HOST}/ [R,L]

# Making sure foldernames end with "/":
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R,L]

# Making sure "/index.htm" is always shown in "/folders/" (but not in the root or /tags/ folder):
RewriteCond %{REQUEST_URI} !^[/]$ 
RewriteCond %{REQUEST_URI} !/tags/(.*)/$ 
RewriteRule ^(([^/]+/)*)$ http://%{HTTP_HOST}/$1index.htm [R,L]

# Sending every visitor in "/folders/" to "page.php" and making exceptions for files my
# PHP-script will be calling. I can then write page.php to control
# what content to show depending on what is in the address-field:
RewriteCond %{REQUEST_FILENAME} !^(.+)\.css$
RewriteCond %{REQUEST_FILENAME} !^(.+)\.js$
RewriteCond %{REQUEST_URI} !img/(.*)\. 
RewriteCond %{REQUEST_URI} !swf/(.*)\. 
RewriteCond %{REQUEST_URI} !php/(.*)\.
RewriteCond %{REQUEST_URI} !pics/(.*)\.
RewriteCond %{REQUEST_URI} !page.php$
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+)$ page.php [NC] 


I hope this can be helpful to someone and I would be thankful for any improvements!

P.S. This should be really good to use for SERP-optimization but remember to change the [R,L] to [R=301,L] once you know everything works... D.S.


Bes - 17.01.09 2:34 pm

Hi

Fantastic article -read it all the way thru, but having an issue as follows:

RewriteRule ^test-(.*)-(.*)\.jpg render/imgEdit_jpg.php?an=$1&col=$2

I want to use this to get values for both an and col.However, the php script I am submitting to always reports col as blank.

The problem seems to be occurring with the ampersand, as this:

RewriteRule ^test-(.*)-(.*)\.jpg render/imgEdit_jpg.php?an=$1$2&col=

Allows me to see everything I am expecting (i.e. the value I inputted for col in my form appears in the PHP script

Any ideas?

Thanks


corz - 17.01.09 9:05 pm

Hi.

Jason, your question doesn't entirely make sense to me. I don't understand why there's a folder called "department", with anything in it; your rewriterules never point to there, only to /kgcoe/chemical/index.php. That index.php looks to be the handler, and /directory is a virtual path. So why does it really exist?

Sean McGilvray, I've no idea what RoR is, anyways, check out the section (above) about capturing variables. It has what you need.

Björn, I handle this with php, which I find superior. Here's a copy of some of my site's init.php script..

<?php
// redirect initial requests for folder/index.php to folder/ (neater, and better for SEO, too)
// better than using .htaccess (if you have $_POST forms, for example)..
if (substr($_SERVER['REQUEST_URI'], -9) == 'index.php' and empty($_POST)) {
    
header('Location: http://'.$_SERVER['HTTP_HOST'].str_replace('index.php'''$_SERVER['REQUEST_URI']));
}
?>

It would be trivial to adapt to your exact requirements (root, not root, etc.)

Bes, Your code is fine (both statements). I would use a less hungry quantifiers (.+), but unless the supplied links are in the wrong format, it should work as expected.

Perhaps you might want to check your setting for php_value arg_separator.input.

;o)


Jay - 21.01.09 8:11 am

How can I redirect my main domain 1 level down to a sub-folder but the sub-folder name should not show in the URL address bar?

e.g.
http://www.ABC.com which root @ /public_html
http://www.ABC.com/gallery pointed to /public_html/ABC/gallery but not showing http://www.ABC.com/ABC/gallery in the URL bar?

Million thanks.
Jay

Jay, I recommend mod_rewrite. It can do exactly what you need. I have an article all about it, here. ;o)



eccentricegg - 31.01.09 8:32 am

Excellent site, although much of the subject matter is currently well beyond my comprehension. I stumbled my way here through myriad websites looking for an easy way to test hotlink redirection and after reading several pages I found not only the hotlink tester,but also a dynamic hotlink image creation script, and a wealth of .htaccess information. Thanks.

My next quest will be to find information on serving compressed content to browsers that support it and testing to ensure the changes work as expected. I'm unfamiliar with PHP or any other programming/scripting languages but have tried to use mod_deflate for gzip compression without any success.I used the following line:

Added to .htaccess
AddOutputFilterByType DEFLATE text/css text/html text/plain text/xml

After reading article from http://httpd.apache.org/docs/2.0/mod/mod_deflate.html several times, I get a server generated error page (as opposed to my custom error page).

One note about the hotlink tester, when I ran the test my redirected image never finished loading (I waited 5 minutes over cable connection, it's under 2k in size), however I was able to right-click, copy the image address of the alt text, and it did, in fact, point to the desired redirect image. I'm not sure why the image never loaded but the test was otherwise successful.


corz - 31.01.09 9:52 am

Working back..

It sounds much more like the hot-link generator test was unsuccessful, and that, for some reason, the script failed to complete. This could be any number of reasons, though most likely, the test server did not meet the requirements of the script, which would be roughly php4.2 with GD library (or whatever it says at the top of the script). Check your error logs for the exact cause.

As for your compression tests and the server error (I'm assuming you meant a 500 error - didn't it specify a code?), it sounds like mod_deflate isn't enabled on your server. If you have access to the main configuration (this is your test server at home, right?), uncomment that line in the LoadModule commands, and restart Apache. Otherwise, you'll need to either pester the server admins, or better yet, try another approach. Or was it another error code?

Oh! And it's unlikely that you reading the Apache documentation caused the server error, much more likely something you did. smiley for :lol:

If you aren't using php to zip the output (read: the easy approach, as outlined in part one of these articles), then you may find this site useful. Another useful thing is the "View Response Headers" feature of Firefox's Web Developer Add-on; you can instantly see if the page arrived compressed, though note, it seems to cache the results, and I vaguely remember playing with this a while back, and having to make fresh copies of my test document for each test, to guarantee accurate results. Otherwise, it's excellent. Check it out.

As for all the web stumbling, why not simply Google: hot-link test   smiley for :ken:

Lastly, it's unlikely that any of the subject matter here is beyond your comprehension. Maybe your current comprehension, but that's easily fixed!

;o)


eccentricegg - 02.02.09 7:57 am

Thanks for the tips.

I actually didn't test the hotlink generator. I merely tested the redirection using a static image. This was the image that was not displayed but was, in fact, the source of the image that never loaded.

When I added the line to .htaccess, it was as you suspected a server generated 500 error code(as opposed to my custom error page). Unfortunately, this is a hosted site. So that seems like I will need to "pester the server admins."

Does the PHP approach work with other filename extensions or is it dependent on filenames ending in .php? The site uses server side includes extensively and the host requires pages using such includes use .shtml extension. Are there any other options to compress output to browsers that support compressed content, that will work without mod_deflate (assuming my passionate pleas do not dissuade the host server adminssmiley for :erm: ) and without changing filename extensions?

It seems I may have to try Firefox again. I've been using Opera extensively for years and it will be a tough transition. I may have to use both for some time.


corz - 02.02.09 6:30 pm

This was the image that was not displayed but was, in fact, the source of the image that never loaded.

What? smiley for :eek:

As to your question.. Yes. The technique described here will work for any file extension you choose, so long as it's something php can handle, you can have the php engine serve it up, even if there's no actual php code in it.

Some would argue that it's not an efficient use of php, but unless you are hosting slashdot, it's probably not an issue, and anyway, getting small download sizes seems to be your main priority, so I'll expound..

First, setup zlib.output_compression, as described, here. Then output your .shtml files through php, as described here. There is a second code example in the text under the code box, which would probably fit your needs as-is.

I don't know of any other ways to compress files, that are guaranteed to work on any hosting setup. It's usually a case of trying one method, and then another, and if it's still not working, pipe it through php and use gzip. Whether or not this might interfere with any SSI you might have, is something you'll need to test.

As for Opera, I like it. It's a fun browser, with some nice features. But when you regularly develop for the web, Firefox is really the only choice. The ability to plug-in what you need, build whatever custom browser suits you, nothing comes close. A vast array of free tools are available as Firefox Add-ons.

Of course, Add-on development isn't always up-to-date with Firefox's development, and that is an issue when Firefox does a major upgrade - who knows when I'll get to use Firefox 3! But with FF3 not long out, this is definitely a good time to "get in".

for now..

;o)


eccentricegg - 03.02.09 5:54 am

"
This was the image that was not displayed but was, in fact, the source of the image that never loaded.

What? "

I right-clicked the image place holder and copied the image address that it was pointing to and it was the address of the redirected leech image not the image I pointed the script to. So the redirection itself was relatively successful(of course it would have been nice if the image actually loaded)smiley for :ken:.


I added the following to my .htaccess and tested using "http://www.gidnetwork.com/tools/gzip-test.php" and if said that the content was NOT compressed. I am beginning to wonder if the compression is worth the effort at this point. (I don't know if you've looked at the site in question but there's not much to look at so far.)
I think this is a point where my comprehension lacks behind my wishes. I feel totally cluelesssmiley for :ehh:

BTW my includes don't seem to be working fully either.

.htaccess

# begin zlib compression #
<ifModule mod_php4.c>
php_value zlib.output_compression 16386
</ifModule>
# end zlib compression #

# begin handler for phpsuexec..#
<FilesMatch "\.(css|style)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.s?html$">
SetHandler application/x-httpd-php
</FilesMatch>
# end handler #

I'm getting a bit frustrated with my lack of knowledge in this arena.

Thanks for all your help so far. I just wish I knew enough to apply your know-how with greater success.


corz - 03.02.09 6:08 am

Gotcha. Yeah, it looks like your mod_rewite code was doing rewriting, but for some reason, the auto-hotlink script isn't working as expected. Load the script on its own if you want to debug it. Remember to set your preferences (inside), first.

That's a handy gzip test tool, thanks for the link. I tested my front page; it's working fine (the tool, that is).

Looking at your code(box), I see you kept the old "php4.c".. Are you certain that your server uses php4? Try "php5.c", instead. Or simply omit the ifModule line (the worst that can happen is you get a 500 error if php isn't loaded, in which case, all this is futile, anyway)..
php_value zlib.output_compression 16386

<FilesMatch "\.s?html$">
  SetHandler application/x-httpd-php
</FilesMatch>
Frustration comes with the turf. Once you get it working, that evaporates.

;o)


MaRmAR - 13.02.09 10:51 am

Thanks! You just solved my problem smiley for ;)

No, I provided information in a form that was accessible. YOU solved the problem. ;o)



saiful haziq - 18.02.09 12:58 pm

you really made a nice job. thx smiley for :cool:


Isindim - 18.02.09 8:19 pm

Thanks for a very helpful site -- one problem I've been trying to solve (and hoped to find the solution here, maybe it is there but I can't get it working), is how to redirect a PHP site to a regular HTML one. Your examples show the other way round.

In other words, how would you use .htaccess to redirect
www.mysite.com/index.php?page=anypage
to
www.mysite.com/anypage.htm

That would be really helpful if I can do that!

Thanks again.

..However, my flat-links examples show it just the way you want. In other words the solution is in the text of the above article. Only post here again if you have code. ;o)



jonesy - 19.02.09 7:19 pm

Hi all,

Just like to say thanks for the useful info you given us! A lot of this is way beyond me at the moment. Anyway, basically I need to remove .html extentions from all my pages, I'm guessing this is quite simple for you guys considering the complexity of some of the code i read. This is as far as I have got at the moment:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.domain\.co.uk [NC]
RewriteRule ^(.*)$ http://domain.co.uk/$1 [R=301,NC]
RewriteRule ^index\.(.*)$ http://%{HTTP_HOST}/ [R,L]


I am completely clueless as what to do?? Any help would be much appreciated,

thanks

Seeing as how you are completely clueless anyway, allow me to recommend that you NOT remove .html extensions. The web is confusing enough as it is. ;o)



Beautiful Ways - 20.02.09 1:19 pm

Hey,
First of all, thanks for the detailed guide! I've been looking for this kind of thing for ages and never seemed to find any good documentation until now smiley for :D

Anyway, I've been trying to set up some RewriteRule's, and have had success... with most of them. For some reason, two of them aren't working - even though they're based on the same code with only two parts changed =/

Here's some of the code:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^register /user.php?mode=register [NC]
RewriteRule ^user/([^/]+)/profile /user.php?mode=view&usern=$1 [NC]
RewriteRule ^fests /festivals.php [NC]
RewriteRule ^forum /board.php [NC]
RewriteRule ^links /links.php [NC]
RewriteRule ^contact /contact.php [NC]

...so by typing this:
/links
If should redirect to links.php but for some reason it doesn't, instead resulting in an "Internal Server Error"... I read about removing the +FollowSymLinks part in case the host disabled user-level settings, but all the files are in the same directory and the only rules that don't work are "links" and "contact"!

If anyone has any ideas, please let me know... I'm stumped!

Cheers,
Dave


EDIT!
I took out the '/' from in front of links.php and contact.php, and now it works... I have absolutely no idea why those two fail with the slash when the other rules work fine but meh, at least it works.

It looks like you threw mod_rewite into a crazy loop which it recognized as a configuration error, which it was. Your solution basically forces mod_rewrite to ignore subsequent rewrites and continue. A better solution would be to use an anchor, like so..

RewriteRule ^links$ /links.php [NC]

It is also less confusing. ;o)



Anton - 25.02.09 5:48 pm

Hi there, I got a problem here

I got a few pages that where links are directing to the page it self but with more and more GET variables each time

like domain.co.uk/index.php?v1=whatever next step is domain.co.uk/index.php?v1=whatever&v2=some-other-stuff and so on
about 4 times or so

So how can i make a revrite rule to have flat links here?

say domain.co.uk/whatever next domain.co.uk/whatever/some-other-stuff and so on

any ideas?

The error is clearly on the first line of your code. Oh, wait a minute, you don't have any code! That will be what's causing the lack of functionality. See the flat links section, above, and try some stuff. ;o)



reutopya - 26.02.09 2:16 pm

Hi
i search to rewrite this url:
http://example.com/profile.php?user=test
to
http://test.example.com/
is this possible? if yes, what i have to put in .htaccess?
Thanks smiley for :)

Yes, it's possible. What you put in your .htaccess is RewriteRules. There's a big article all about them, right above these comments. Check it out! ;o)



reutopya - 27.02.09 4:57 pm

I searched but I found nothing that help me :(
can you tell me what i need to put into .htaccess? thanks

It will take you a lot less time to read the article, than it took me to write it in the first place. ;o)



Raghavendra - 01.03.09 3:33 pm

hi,

i have created .htaccess file with

Options +FollowSymLinks
RewriteEngine on
RewriteRule family-id-(.*).htm$ family.php?id=$1

but it is not opening any page in localhost and in server

the error is

*************************************************
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

*********************

i didn’t come to know hoe i need to do.

Regards,
Raghavendra.

A.K.A "500 Error". This means there is an error in your configuration, most likely in your .htaccess file. Double-check every line. Also ensure there is a full blank line at the end. ;o)



sing - 04.03.09 8:13 pm

Hi,

Have you enabled
"LoadModule rewrite_module modules/mod_rewrite.so" in httpd.conf file?. I assume you use Apache Web server

Heh, actually that might be one httpd.conf gotcha worth adding to the article! smiley for :D  ;o)



Smartness - 07.03.09 10:02 pm

Hey, a little question here!

this is the url:
http://localhost/zeri/article.php?id=12

the page title of that page is example:
Hello - How Are you Doing!

How to rewrite to:
http://localhost/zeri/article/Hello-How-Are-you-Doing.html

Is it possible?

Yes, it's possible. Examples above. ;o)



Ryan - 13.03.09 3:43 pm

I have been reading your htaccess guide and its brilliant.

Just a question, is there anyway to htaccess rewrite index.php#anythinghere to /#/anythinghere

?

www.we7.com do it, but I don't know if thats htaccess or javascript.

Thanks

smiley for :)
Ryan

Firstly, mod_rewrite can't work with the part after the "#", because it's "a browser thing" which the server doesn't see; a page anchor; so if any site is redirecting with it, it's probably Javascript/Ajax of some sort. I've not seen any examples of this.

And secondly, I don't think that's what's going on at we7.com - none of their links, that I saw, have a hash in them, it's simply a part of the final URI path, in other words, /magazine >> /#/magazine/. ;o)



Ed - 15.03.09 4:25 pm

Hello,
I am rather new to .htaccess and I don't fully grasp all the rules.
I have been racking my brain to come up with a solution.
I have this in my .htaccess and I am getting too many redirects in the browser.
RewriteEngine on
RewriteRule ^search-results/([^/]+)/?$ /results?Search=$1 [L]
RewriteCond %{QUERY_STRING} ^Search=([^/]+)
RewriteRule ^results /search-results/%1? [R=301,L]

But when I remove the $1 from the first line as below, the redirect works, but the search results are no longer being filtered returning all the results in the database.
RewriteEngine on
RewriteRule ^search-results/([^/]+)/?$ /results?Search= [L]
RewriteCond %{QUERY_STRING} ^Search=([^/]+)
RewriteRule ^results /search-results/%1? [R=301,L]

I am completely stumped. Any help, advice, direction would be "GREATLY" appreciated.

First, you create a loop. Then you remove one of the conditions of the loop so that it does not loop. Okay. It would be useful to know what you are trying to do. Let's see if I can decipher that...

Nope, nothing is coming to me. But my advice would at any rate be to keep the $1, and instead remove lines 2 and 3. ;o)



Czakalli - 16.03.09 9:06 am

Hi there,

congrats on the nice tutorial you have here.

i'm making a script for multiple domains (a few hundred).
I would like to make a redirect for domains without the www. (like http://example.com) into http://www.example.com

if i write the domain it works, like this:

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com [R=301,L]

but i would like to have all domains redirected without writing one by one.
smth like this won't work:
RewriteCond %{HTTP_HOST} ^.*\..*$ [NC]
RewriteRule ^http://(.*)$ http://www.$1 [R=301,L]

any help would be apreciated.

As much as it pains me to help someone to ADD "www" into their domains, I'll give you a clue..

You need to capture the host, all of it. ;o)



John Wilson - 19.03.09 3:55 pm

I spent an hour or more looking for only a portion of what you provide here.

Others just talk a little and give no explanation or instruction so that a reader can go and create a rewrite.

Your presentation is excellent.

I was trying to understand redirection and use it to make search engine friendly links. Links that were cleaner looking.

While I haven't digested the entire article, I am confident that the answers to my questions are here.

Thanks so much for such an informative piece.

John
8BitPublishing.com


koorosh - 20.03.09 8:21 pm

hello could you please help me ?

i want to redirect this address for example :


http://p30city.net/archive/index.php?t-481.html

to :


http://p30city.net/archive/index.php/t-481.html


(/ insted of ? )

i have read your learning but really i cant use them for this issue...

Take a loop at the "PATH_INFO" server variable. ;o)



ma69 - 23.03.09 8:48 am

excellent information.

thanks


paulo - 26.03.09 7:57 pm

First, great info you are providing. Next, I might be stepping into areas not related, but I'm using Joomla 1.5.x for which there are modules with the Google translator.(and there is one which claims to solve the following problem, but it does not work for me.) A problem arises when trying to get Google to translate pages that are password protected (e.g. a member's profile). Depending on the module, it either kicks the user out of those password required pages, fails, or shows a blank page. I have tried a couple of variations for the rewrite rules but no success. Google apparently treats it as a bot and disallows. Not sure. Could this be handled in .htaccess? Anyway, thanks! (one aside and nobody ever mentions it: Is there a heirachy that lines must be entered in .htaccess?)

Unless Google has a membership (and knew to login!) this isn't gonna work. Remember, Google is grabbing the page for you.

As for "heirachy", no. .htaccess lines are simply read in order. ;o)



Peter Nichol - 29.03.09 6:21 pm

Love the mod rewrite's you've got here.

I used it to make my dynamic sitemap


Graham - 30.03.09 2:30 am

Hi,

I'm having real trouble with this, I have an .htaccess file that I am using to send all requests to a control file "content.php", the current htaccess looks something like this:

RewriteEngine On
RewriteCond %{REQUEST_URI} "/google*" [OR]
RewriteCond %{REQUEST_URI} "/y_key*" [OR]
RewriteCond %{REQUEST_URI} "/ajax/*" [OR]
RewriteCond %{REQUEST_URI} "/resources/*" [OR]
RewriteCond %{REQUEST_URI} "/nav/*" [OR]
RewriteCond %{REQUEST_URI} "/sitemap*" [OR]
RewriteCond %{REQUEST_URI} "/style.css" [OR]
RewriteCond %{REQUEST_URI} "/travel-guide/*" [OR]
RewriteCond %{REQUEST_URI} "/js/*" [OR]
RewriteCond %{REQUEST_URI} "/content.php" [NC]
RewriteRule ^(.*)$ $1 [L]
RewriteRule ^(.*)$ /content.php?url=$1 [L,QSA]

All this seems to work fine but for some reason when I try to access a file e.g. /travel-guide/file.php it throws a 500 error.

It has been working properly for over a year but doesn't seem to like it??

Any help would be sweet.

Oh man! Where to start? Firstly, I can tell by the fact that you didn't use the [pre] tag to post your code, that you don't read the instructions. Maybe you think you do, but probably your eyes are moving too quickly over the words, and their meaning isn't getting to your brain.

Instead, you laboriously double-bracketed your entire code example.
I put that in a paragraph on its own in the hope that you will read it a dozen times, print it out, pin it to your fridge, make a T-shirt, and so on. Lessons for life. So, getting errors are we? ..

RewriteRule ^(.*)$ $1 .. is just plain hilarious. Thanks for that.

And why all the asterisks? Did you think this was a windows file search? I mean, what is the actual point of them? By which I mean, there is no point. "foo" matches "foobar".

And why all the double-quotes? Exactly who told you to use those? By "exactly", I mean please send me their email address so that I may abuse them, textually.

What else? Oh yeah! The whole shebang only works (assuming everything else was fixed!) if the REQUEST_URI is for content.php! Nice one! smiley for :lol: Of course you meant to specify that it not be for content.php..

RewriteCond %{REQUEST_URI} !content.php [NC]

Working properly for over a year? smiley for :lol: Right! Maybe the "RewriteEngine On" part! ;o)



R - 03.04.09 7:54 pm

Great article! I find it mildly amusing that despite the section on hotlinking and stealing content, your entire page has already been "borrowed" and placed on someone's blog:
http://th-e dot blogspot dot com/2007/09/htaccess-tips-and-tricks.html

Anyway, my question was that I can't figure out how you were able to have the google translator header disappear on the translated page? I can't seem to do this on my page, and I reviewed your source and didn't see anything that would point to a solution. Could you give me a hint? smiley for :) Thanks!

I remember when I found it mildly amusing. Now there are dozens of copies. There are even whole domains that are nothing but this page! smiley for :eek: What's annoying is that I do update even my oldest pages, and the "other" copies never reflect these changes. Mind you, since the wayback machine stopped indexing me a couple of years back for some reason, this might not be such a bad thing!

As to the "secret" of how I make the Google strip disappear. Well, that's not .htaccess. Check my page source for JavaScript includes, there's one called "breakout.js". What you are after is in there.

Be mindful if ripping it off for your own site. Lots of community bookmarking sites use frames to embed other sites, or add their own site frames, and breakout will render them completely ineffective, quite probably along with any ability the user may have had to vote on your site. I don't care for all that silly shit, but many do.

breakout.js provides an option to allow certain containers, but this, being my JavaScript, isn't 100% reliable. Google images, for example, can't contain one of my pages, even though I added Google to the allowed containers. As implied, it might be my JavaScript code, in which case, feel free to update that and return it to me working! smiley for :D

I guess it wouldn't be too difficult to write some JavaScript to only breakout of certain, specified containers. I've just not bothered. ;o)



Dr P - 07.04.09 7:18 am

Dynamic Fail


Hey, I've been playing with .htaccess for a little while and it's frustratingly not working the way I would want.

Really simple flat link setup.

I want to rewrite
http://www.example.com/home

to
http://www.example.com/index.php?p=home


Example1 works very well. I want it to be dynamic but Example2 fails with a 500 code.



Example 1 - works



Options +FollowSymlinks
RewriteEngine on

RewriteRule ^home$ /index.php?p=home [NC]




Example 2 - fails



Options +FollowSymlinks
RewriteEngine on

RewriteRule ^([^/]+)$ /index.php?p=$1 [NC]


I've tried all sorts of combinations within the parenthesis... without luck.

I'm running around in circles I think... smiley for :eek:

Thanks.

You could get by by simply removing the forward-flash before index.php, but it still wouldn't do what you want, because it would loop around and "p" would always be "index.php". You need to think outside the parenthesis..

Specifically, you need to specify a condition; that you only want to rewrite requests that aren't already pointing to your target. Something like this would work well enough..

RewriteEngine On
RewriteCond %{REQUEST_URI} !index.php 
RewriteRule ^([^/]*)$ index.php?p=$1 [NC]
There are other things you could check for to achieve the same goal, like whether or not a request has already been rewritten, or whether it already contains variables, and so on. Those three lines are the basics, though. ;o)



Bhagwat - 08.04.09 6:43 am

i have one site
http://bhagwatsingh.ueuo.com/index.html

when i try to type following in url

http://bhagwatsingh.ueuo.com/index.html/kkk



it is showing me 404page.

can u help me if user type aanything like (/kkk or /23222) it will not consider this and index page successfully show.

The easiest way forward is to use index.php, instead. You should find what you are trying to do works fine.

Alternatively, have .html files parsed as php..

<FilesMatch "\.html$">
  SetHandler application/x-httpd-php
</FilesMatch>


;o)



cwd - 11.04.09 10:11 am

this is an excellent guide. i would love to see more info on the different switches (NC, R, R=301, L) and so on. Thanks!


Jason - 13.04.09 2:52 am

Thanks for writing this article. Mod-Rewrite is an intimidating beast, and you've managed to make it much easier to understand. That's talent, right there. Thanks!


nasal - 15.04.09 1:55 pm

Great blog post but..

*Splutter* WHAT? You cheeky bastard! If it weren't for the fact you go on to state you read the comment disclaimer; which almost no one does; that would have been instant deletion "right there!", as the previous poster puts it! smiley for :lol:


I'm sorry for asking, but I didn't come up with a solution on my own, so I was hoping to get a little help (I read your comment disclamer).
<- !!!


I have this code:

htaccess
RewriteRule ^(.*)/$ ?$1 [NC,L]

RewriteCond %{REQUEST_URI} portfolio/(.*)/?
RewriteRule ^portfolio/(.*)/? ?portfolio=%1 [NC,L]


Aha! No [pre] tags! So what you meant was "I noticed that you had a comment disclaimer, and skipped over it." That's definitely not the same thing! But I'm committed now. Well, up to this point. smiley for :lol:


So everything after example.com and between slashes (/(.*)/) will become ?$1,
and if there is something after portfolio it will become portfolio=%1.

What I don't understand is:
- how can I make the second / optional (example.com/something/ <- this one) so that if I ommit it it will still point to ?$1 and not give me a 404 (or maybe redirect to the url with / at the end)

Put a "?" after any character to make it optional.


edit
i added this now

RewriteCond %{REQUEST_URI} !images/*
RewriteCond %{REQUEST_URI} !inc/*
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]

so now everything exept files in those folders go redirected to the domain with the trailing slash.. is this a good way to do it?

No. Requests for /foo/ will, all by themselves, end up in /foo/. There is no need to include mod_rewrite in the process.

And... Again with the asterisks! wtf! Learn what they are for, and use them for that, nothing else! I'm bored with this post now. ;o)


(- how can I do the same for portfolio? why wasn't /? working? shouldn't it make the slash optional?)

- new question: why isn't portfolio/something/ pointing to portfolio=something but to portfolio/something ?

Thanks for all the help you are willing to offer :p and for writing this article, there rules became clearer now.

Kind regards,
Rudi


Boris Vujicic - 16.04.09 1:17 pm

Really good text.
It is fluid and detailed, covering all the options.
Good Job man.
Really good job.

PS: i hope you dont mind but i am saving this, just in case this gets off the internet.

I don't mind, per se, but I would very much prefer if you didn't, because; 1) This page WILL still be here in 20-oatcake, and 2) I like it when you come back to visit! Actually, a better reason is that I do sometimes update this page, and your copy would then be out of date. See? ;o)



Adam - 24.04.09 8:47 pm

Great article and I've printed it for a closer reading later, but for now I haven't been able to find a solution from the sections I have read. Yes, I know, and I promise to read it all, but my problem has been one for two weeks now without any success.

I tried a couple of variations of the code you have shown here, with zero success so far. I have three needs that need to be met for my current site design:

http://example.com/(.*)$ =>
http://www.example.com/$1 [NC,L]

^www\.domain\.com/(.*)$ =>
www.example.com/$1 [NC,L]

^([A-Za-z0-9-]+)\.domain\.com/$ =>
$1.example.com/PAGE1?var1=$1 [NC,L]

^([A-Za-z0-9-]+)\.domain\.com/^([A-Za-z0-9-]+)/$ =>
$1.example.com/PAGE2?VAR1=$1&VAR2=$2 [NC,L]

Those are the only four cases that I current have to worry about, but the fact that a sub-domain is a variable has been a major problem for me.

Also, is there a method to make any sub-domain, other than WWW and the NON-WWW, be automatically handled without the need to add another domain record? I have seen ^([A-Za-z0-9-]+)\.domain\.com/$ => www.example.com/$1/ solutions, but none that keep the "fake" sub-domain. If I have to, I'll gladly add the individual sub-domain records, but the first four rules a must for the current design to work. smiley for :erm:

Thanks in advance for any help you can provide, even if it is already above, and I just missed it, or you know where I can read about a similar solution. Thank you. smiley for :D

That's not mod_rewrite code. As for your infinite sub-domains, Google: Wildcard Subdomains It's a DNS thing. ;o)



hankwatson - 25.04.09 7:05 pm

handy tutorial! Thank you!


seattle drupal user - 30.04.09 12:06 am

I was working on my htaccess redirect for an hour trying to get the regex right. And the answer was here all along!

Thanks so much for putting this page together. Now all my coworkers think I'm really S-M-R-T

cheers!
dd


Zach - 03.05.09 8:49 pm

Okay, so, I checked many different pages for a simple rewrite rule to lose the www and after trying like four that didn't work I finally found yours. Thanks!


Dha12oks - 05.05.09 7:56 am

Omg, i just have to say you saved my life! because i was looking around for a $_GET .htaccess that would obviously read w/e i would put in the link and now it works! smiley for :)

Thanks mate really, really appreciated


Panchu - 07.05.09 10:28 am

I have to convert all uppercase letters in the URL to lowercase automatically with .htaccess. For eg:- if a user types http://mysite.com/PanChu to http://mysite.com/panchu automatically wit a 301 redirect, is it possible with .htaccess? If yes please help me how to do that...
Thanks in advance

I can't think of any elegant way to alter the case of a request with .htaccess. A better approach might be to use .htaccess to send ALL requests to a server-side handler (e.g. php) where they could have their case interrogated and altered, as well as 301 redirected, or whatever else you need. ;o)



Yatko - 12.05.09 11:24 pm

URL based language selection

Hi, and thanks for the above!

I have a site, and two domains pointing to it (both domains pointing to /home/public_html/example.com folder). What I am trying to achieve is language selection using .htaccess

The language selection works like this:
if the URL ends with /index.php?lang=ro or ⟨=ro -the site will use RO translation
if the URL ends with /index.php?lang=en or ⟨=en -the site will use EN(defa) translation

I am trying to redirect all requests that come both from domain.ro and www.domain.ro to www.domain.ro/index.php?lang=ro

I managed to redirect www.domain.ro to domain.ro/index.php?lang=ro (without www), but I am stuck here. I am unable to redirect www.domain.ro to www.domain.ro/index.php?lang=ro and cannot add additional rule in case of a visit without www (domain.ro). I am sure, my rule is OK, just not the right rule I should use in this case :(
RewriteCond %{HTTP_HOST} ^www\.domain\.ro$ [NC]
RewriteRule ^(.*)$ http://domain.ro/index.php?lang=ro$1 [R=301,L]

I started to experiment with the above, ...server error smiley for :)
#two domains served from one root..
RewriteCond %{HTTP_HOST} domain-one.com
RewriteCond %{REQUEST_URI} !^/one
RewriteRule ^(.*)$ one/$1 [L]

RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URI} !^two
RewriteRule ^(.*)$ two/$1 [L]

Please write an email if you can help. Thank you!

As you already have two different versions of your site; one for each language; all you need is the domain redirection. I do not see the point of adding all this extra stuff onto the end of the URI. Once the user is in the correct folder for the Russian domain, they get they Russian language site. What need is there of _GET Parameters? Keep it simple. ;o)



PH-MJS - 15.05.09 3:03 pm

Very easy explanationsmiley for :eek:


datta - 15.05.09 7:50 pm

I Have rewrite condition
RewriteCond %{THE_REQUEST} ^(.*)cid=1239374326385(.*)$ [NC]
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{THE_REQUEST} !^(.*)flash=noflash(.*)$ [NC]
RewriteCond %{THE_REQUEST} !^(.*)&s=(.*)$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/mobile? [R=301,L]
RewriteRule ^mobile([/])?$ /servlet/ContentServer?pagename=XM/Page&c=FlexContent&cid=1239374326385

Also I have a mobile folder in docroot /local/apache/docs/mobile
When I try to access it goes into a loop and returns a Page load Error
"Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

How would I make apache mod_rewrite differentiate between a folder and
url and make them available based on condition if index.html does not exist in the mobile folder use the URL /servlet/ContentServer?pagename=XM/Page&c=FlexContent&cid=1239374326385 to redirect.

Thanks
Datta.

One step at a time, dude! Firstly, what is the point of capturing variables you don't intend to use? In other words, why "(.*)", when ".*" will do just fine. And why specify "^(.*)foobar", when "foobar" does exactly the same thing?

First you need to fix all that stuff - every single line suffers from these errors; ([/])? ?? Once you do that, the whole thing will be much simpler to understand, and fix.

Checking for a file's existence is trivial ({REQUEST_FILENAME}), but does produce some overhead. If the rewrite is in a place where it will be processed on every request, do consider an alternative approach. ;o)



NixGeek - 23.05.09 7:19 pm

Hi. It's a very good article, definitely worth bookmarking, but a couple things I would like to point out from experience.

First off, a high traffic website (and I mean HIGH traffic, as in 30K uniques/1M pageviews per day) is gonna seriously suffer with a large .htaccess (trust me on this, Ive seen it, experienced it, dealt with server constantly crashing under the heavy strain of processing the big file - someone was trying to block a geographic region using rewrite rules, 15,000 lines or so of rules... ouch!!! We removed the .htaccess rules and designed a shell script that created iptables firewall rules instead, to solve that issue. ) So, I would like to point out to please, be careful with the language where you state "Fortunately, mod_rewite can parse enormous lists of ban-lines in milliseconds, so feel free to be as specific and comprehensive as required." - It can, up to a point, but then it becomes a liability. - Most users may never see an issue, but it's something to keep in mind if you notice your website seems very slow and server seems to be heavily loaded... in which case renaming your .htaccess , if that is the cause, would show an almost instant improvement in performance..

Yes, you are right about that. But in my experience, anyone running a site with millions of visitors is going to be well aware of the limitations of .htaccess, and will be configuring on a per-server basis, anyway.

Interestingly, I only added that text a couple of weeks ago after running tests on some fairly massive .htaccess files here at the org, and being quite surprised by just how many rules you can get off with. Of course, I may seriously underestimate just how comprehensive some peple might get! As with all the text here, there will be edits, cheers!


Secondly- You mentioned Inheritance, and I thought "Oh! Great, someone who knows their sh*t, not just another blogger spouting off something they heard somewhere else.." But then I was disappointed. you never mentioned this:

RewriteOptions Inherit (*might* only work for Apache2/Apache2.2 though, I haven't run Apache 1.3 in years)

Saves the trouble of "but I'll need to reinsert the rules for it to work in this sub-folder "

So you could do :

RewriteEngine On
RewriteOptions Inherit
RewriteCond blahblah
RewriteRule blahblah

in a sub folder, and your rules from root folder's .htaccess will still work in the subfolder, unless they then match the new rewrite rules , obviously smiley for :)

You are absolutely correct to assume that THIS is exactly the sort of behaviour someone would want from mod_rewrite. However, fortunately, the behaviour you speak of is already the default, that is to say, it happens anyway, with no effort on our part, in Apache 1 and 2.

The option you are referring to is for something slightly different. Something which in the real world is rarely useful, and its results more often than not; very like RewriteBase; confuse and create more problems than they solve, hence its omission. In all the years this page has existed no one has ever asked me about it, and still hasn't. Good! ;o)


Other than that- Great article, well done rule sets (SO many other places give rewrite rules that might work for them, or just some stuff they read somewhere else and posted it as their own content without ever testing... but the rules cause issues for others, if not completely break outright.. yours are extremely well done, and obviously very well tested)


Henry - 28.05.09 5:15 am

This is a great turorial but I still can't find the solution to my problem.
I have a subdomain like this

http://great.example.com

And on the server-side, I'd like to point that to

http://www.example.com/try.php?url=great

Here's My code
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?([a-z]+)\.domain\.com$ [NC]
RewriteRule (.*) /try.php?url=%2


When I try www.great.example.com or great.example.com, it always says "Address Not Found" as if the url is wrong.

What is wrong here? Could you please help me? I really appreciate it.

Thanks,
Henry

This is a DNS issue. Check that you have enabled this subdomain in your hosting control panel (or else enabled wildcard ("*") subdomains) ;o)



nEeNa-JoSE - 01.06.09 10:25 am

if i want to rewrite permalink

profil/?fullname=NoName&user=112233 (in http://mysite.com)
to
http://profile.mysite.com/NoName

what codes should i put in my .htaccess??

thanks in advance for ur help
i'm little bit confuse

Just try something! It's not so hard. If you have any issues, post the code here. ;o)



m4niac - 07.06.09 2:18 pm

i'm sure there is a problem in the code: in controller.class.php at this instruction
some code..
$this->$model =& new $model;

now, i've checked everything and im 100% sure that it's just like you said, everything works except this...
if i put an echo just after this line it will not be shown!
if i put it before, it shows...

i've tried
some code..
$this->_model =& new $model;

but the same thing happened...

finally i've commented this line and it echos what is next BUT i get this error:

some code..
Notice: Undefined property: ItemsController::$Item in /var/www/mvc1/application/controllers/itemscontroller.php on line 14 Fatal error: Call to a member function selectAll() on a non-object in /var/www/mvc1/application/controllers/itemscontroller.php on line 14

which makes perfect sense to me... so.. didn't you miss something?

smiley for :eek: Yeah! You missed the fact that you are on the wrong page! Dude, the wrong SITE! ;o)



Manish - 20.06.09 11:45 am

My Problem is

1) the url in the browser does not redirect to the new url (though when i enter the new url myself , it works fine)
2) Do i need to change the links in the entire site for a particular url if i need to go for a url rewriting.

Help required .

1) Missing flag there is, big R somewhere (any way to the url , fine)

2) Perhaps, but in one line always such edits are, oh yes! And failing that, regexp. ;o)



Sanjay - 22.06.09 3:01 pm

This is a nice tutorial abt the htaccess.I got the information from scratch to the high level


Chris - 26.06.09 9:39 pm

I was running into trouble with the hot-link prevention code... without an additional rewrite condition specifying RewriteCond %{REQUEST_URI} !hotlink.png won't it send the requester into an endless loop and never actually display the hotlink.png? or did I make a mistake?

Good catch, sir! Yeah, I forgot to add that line to the updated code here (I now link to my php auto-hotlink generator, which is .php!). smiley for :eek: Thanks! ;o)


also in regards to Panchu's question about lowercase conversion, it is possible with 26 separate rules to cover each letter, ie A=>a, B=>b etc... (and another rule to start over again to look for additional capitalized letters as each rule only does the first capitalized instance of that letter) The 26 rule method is really slow and inefficient, its much better to use a rewrite map in httpd.conf, if you can... just Google "htaccess wheel of fortune" to find more details on 26 rule. The wheel of fortune makes it easier to find as the particular posting I'm thinking of showing the code has a reply showing the alphabet rearranged by most commonly used, helpful for wheel of fortune/hangman games..smiley for :geek:

Thanks again Corz for the great reference!

You're welcome! ;o)



makeitup - 28.06.09 9:17 pm

Hi,
On my site I put all my contents in a subfolder . So when I want to access my webpage I would type in www.aa.com/subfolder/html/index.html. There are other pages such as contactme.html, info.html, etc. The images and css are stored in /subfolder/css and /subfolder/images/

I made a rewriterule to hide the subfolder with the following:
DirectoryIndex index.html
RewriteRule ^(index|contactme|info)\.html$ /subfolder/html/$1.html [NC,L]


So instead of having to type in each html page name, I tried the following:
RewriteRule ^(.+)\.html$ /subfolder/html/$1.html [NC,L]

This gives me a internal 500 error.

Is there a reason the second rewriterule doesn't work?

Also since i'm using relative paths for images and css files, they are broken when i use the rewrite rule. In the html files they are accessed as "../images/image.jpg" or "../css/file.css". Ive tried a rewrite rule like:
RewriteRule /images/(.*)/(.*)\.(jpg|gif)$ /subfolder/images/$1/$2.$3 [NC] , but its still not finding the images.

Does rewriterule only work based on the url on the address bar?

Thanks!

mod_rewrite works with requests.

I see no reason why you would get a 500 error. Some other .htaccess content (that you didn't post) must be doing that (missing line break at the end of the file, perhaps).

Your big problem here, is basic design. All these extraneous subfolders are always gonna cause more headaches than anything else. If you put something in a subfolder, there really needs to be a reason for it. ;o)



pp - 30.06.09 11:43 am

Hello.. I`m trying to have two condition in my htaccess.. one for hotlink protection and another one is to bypass the hotlink.. here is my htaccess looks like

RewriteEngine on 
#
#Block Hotlink
RewriteCond %{HTTP_REFERER} !^http://mywebsite.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://mywebsite.com$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mywebsite.com.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://mywebsite.com$      [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|php|html|mp3|mid|rm|ram)$ - [F,NC]

#
#Bypass HOTLINK for Iphone 
RewriteCond ${HTTP_USER_AGENT} iPhone
RewriteRule ^.*$ -



For the iphone it's not bypassing the HOTlink.. would you please tell me where exactly i`m doing wrong..

Thanks


Re: PP - 02.07.09 6:38 pm

First off, your question does not make sense. Hotlinking is preventing another site from using your images as their own, so you don't need a bypass for your iphone unless you were somehow hosting a website from it!

as for your hotlink code, it's unnecessarily complicated - you could easily condense your 4 HTTP_REFERER conditions using the style in Corz's example

also ${HTTP_USER_AGENT} should be %{HTTP_USER_AGENT}, and you could have combined the 2 rules by adding !phone condition to the hotlink, like this:

RewriteEngine on
#
#Block Hotlink
RewriteCond %{HTTP_REFERER} !^http://(www.)?mywebsite.com/ [NC]
RewriteCond %{HTTP_USER_AGENT} !iPhone
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|php|html|mp3|mid|rm|ram)$ - [F,NC]

but all that would do is prevent a site from using your images when viewed from any browser except an iphone, and with iphone your images would suddenly appear on their site (if iphone were even the correct user agent name, more likely it shows up as Safari)

FYI its a wasted effort to block filetypes you don't actually use on your site, so unless you actually host .bmp, .mid or .rm files, no point in including them. Also html|php are unnecessary, and might actually interfere with anyone linking to your site, which could drive visitors away...


Ronny - 05.07.09 5:06 pm

What rule do I need to use if I want to access all kinds of rewrites in a PHP script to do all the work for me?

Simply send everything to your php handler with "." or "(.*)". And use the [QSA] flag, to ensure all variables are intact. ;o)



momosa - 09.07.09 9:21 pm

hi corz, meet again

i have a question about redirecting index.php to to root folder

see this:
people can access my site from:

http://example.com/index.php
http://www.example.com/
http://www.example.com/index.php

and i want from those url above should be redirect to http://example.com (without index.php)

i've been already done to remove www from domain with your htaccess trick (thanks very much)

and now i want to ask u what is the best htaccess expression to remove(redirect 301) index.php while user access my site from http://www.example.com/index.php and should be redirect permanently (301) to http://example.com

once again, mmm,, sometime i'm develop my site in localhost, so how should this htaccess method to remove index.php is working too when i'm access my project with http://localhost/mysite/index.php should redirect to http://localhost/mysite/

and there is a possibility to redirect all index.php in other subfolder likes http://example.com/journal/index.php => http://example.com/journal/

thank for the reply, i really appreciate for your answer smiley for :D

Removing index.php is best handled in php, perhaps in some included init script. I go into more detail earlier in the comments. Something like this..

<?php
// redirect initial requests for folder/index.php to folder/ (neater, and better for SEO, too)
// better than using .htaccess (if you have $_POST forms, for example)..
if (substr($_SERVER['REQUEST_URI'], -9) == 'index.php' and empty($_POST)) {
    header('Location: http://'.$_SERVER['HTTP_HOST'].str_replace('index.php', '', $_SERVER['REQUEST_URI']));
}
?>
;o)



steve circeo - 10.07.09 3:04 am

thanks for the help. dunno if i will have any questions, but i threw a little cash your way, anyway. 'preciate it.

Woah! After so many millions of hits... a donation! w00t! ;o)



zaphod - 14.07.09 7:55 am

there's a lot of really useful information here but i'm going blind trying to read it. more contrast between the text and background colors would make this page infinitely more readable

See here.


thank you for taking the time to post this information

i want to redirect from 'filename.js?var=1' to 'filename.php?var=1'. any thoughts? i tried a variation on one of your examples but it created a server error and took my site offline

any thoughts are appreciated

zaphod

It's those variations that get you every time! You didn't post code, so no further comment required. Aside from... You are playing with mod_rewrite on your LIVE site?? Are you mental? You must have skipped the part where I recommend strongly that you test test test on a local development mirror. ;o)



Joe - 23.07.09 11:20 am

Thanks for the great insight I to mod rewrite.
Like you say there are to many posts that give rewrite rules without explaining them very simply to people.

They make things complex when half the time they are not its just the description need to be put in to words people can understand that know nothing about mod rewrite and regex.

Regex = regular expression for the people that do not know.

Very intuitive and well done will be saving for another day to read when I have more time and for referencing. smiley for :D


Bentow - 29.07.09 4:13 pm

Hello,

I have a host in a domain and I've pointed the www and forum to my host.

I have the following folder structure:

- example.com
--- www
--- forum

I wanted that when the user types: www.example.com he gets redirected to the www folder, and when the URL is forum.example.com he goes to the forum folder.

I tryed the following way

<?php
Rewriteengine on
RewriteCond 
%{HTTP_HOST} domain.com
RewriteCond 
%{REQUEST_URI} !^/www
RewriteRule 
^(.*)$ www/$[L]

RewriteCond %{HTTP_HOST} domain.com
RewriteCond 
%{REQUEST_URI} !^forum
RewriteRule 
^(.*)$ forum/$[L]
?>


When I remove the second block of rewrites, it rewrites correctly to the www folder, but when I add the forum section it doesn't work anymore.

I followed the multiple domains tip.

Could you please help me?

Cheers.

For starters, you need to specify the domain name in the RewriteCond! Also, try and keep sets of rules like this consistent, makes it easier to track down errors. ;o)



Delirious - 02.08.09 10:06 pm

Evening,
I'm having a bit of difficulty with a rewrite rule:
The Aim: To have users/index.php?un=username to display as users/username

The Current Code:
.HTACCESS code
Options +FollowSymLinks
RewriteEngine On
RewriteBase "/users//"
RewriteRule ^.*$ index.php?un=$1

There are at least three reasons why I won't be responding to this. Answers on a postcard! ;o)



Javacode - 02.08.09 11:31 pm

Hi, I am having such a hard time with a themes on my website someone made me where the creator must have made a mistake in making it. Every time someone trys to navigate the site I get logs of 404 Errors for spacer.gif I was wondering if you could help me to point all request for spacer.gif to 1 file instead of having to put one in each and every folder on the website (200+)

This is webmastering 101, mate! And absolutely nothing to do with .htaccess or mod_rewrite, not even close! smiley for :lol:   I'm here anyway, so..

LOOK AT THE SOURCE! Better yet, run a regexp search and replace over the entire site, switching in the valid link. Better yet, get rid of that daft old-school spacer altogether! ;o)



arpit - 20.08.09 9:47 am

i really liked d clearness in your description
its a fine blog


naganata - 21.08.09 10:22 pm

Excellent blog. I now have ReWrites making the site look much more professional. Many thanks!!

Referring to this article, and the hundreds of hours of work involved in it, as a "blog", will not get you into my Christmas list! ;o)



Michael Griffin - 22.08.09 2:33 am

Ok, so I think that I have perused your article and the comments enough to find that this particular situation has not been brought up. Of course I could be mistaken, but I hope not. I want a relatively secure system for protecting a number of files in a directory. I had hoped to deny all access to the directory using an .htaccess file with the
deny from all
piece of code. Then I wanted to use php to open up the directory for access for an authenticated user. I say this for two reasons. First is that I want to prevent direct linking to the file with a .htaccess as it is the most simplistic method I have found so far. Second is that I want to use php so that I can provide appealing login logout sessions and the added benefit of storing user and password information in a database. Is this possible? If it is I would be forever grateful (and I would make it clear with my donation befitting my current status of college student). Thank you so much! smiley for :D

Michael Griffin

Oh and I have tried to use php to open the directory but that seems to not work:

<?php
$dir = 'downloads';
opendir($dir)
?>


See this. ;o)



steve - 28.08.09 4:13 pm

very clear site.
thanks!


Geeta - 10.09.09 7:46 am

Hello,
I want domain redirection.
I have written a rule as by reading the above contents
RewriteRule   ^(.*)something(.*)$ http://myexample.com/server/folder/display_test.php?code=$2 [NC]


This is not working since servers are different or I am not able to figure out what exactly is the problem.
the rule is like something is a word which I am searching in url which may be say http://abc.com/something/nfghui/ or http://xyz.com/something/dfff/ and if that word occurs then redirect to mydomain's display_test.php
Any help regarding this would be appriciated.
Thanks,
Geeta

Add an "R" to your switches (for an external rewrite, aka, "redirection", as explained in the article), so it's [NC,R] ;o)



Jesmo - 18.09.09 7:44 am

Hello from Denmark

I have this problem with my 301 redirect:
http://mydomain.dk/belysning-9/tiffany-lamper-32/?url=tiffany-lamper-32/

The last part of the url is where the redirect come from.

It show´s an extra url in the redirect - i am trying to avoid duplicant content but the extra url can be searched also. so when i remove 1 risk i just made another ??

Anyone who can help me please .

Best regards smiley for :eek:
Jesper

--

The string in my .htacces looks like this:
redirect 301 /tiffany-lamper-32 http://mydomaine.dk/belysning-9/tiffany-lamper-32

Try again with mod_rewrite! ;o)



netsnake - 18.09.09 7:56 pm

Hi!

Wonderful tutorial! I've been looking for something like that for quite a while.
Now I happen to be German, so I couldn't resist translating this page using your nice -de ending like that: https://corz.org/serv/tricks/htaccess2.php-de but what happened, is that it just got stuck in a loop, I don't know why, but it did. Just try it yourself. You'll see.

I guess you really don't have any non-English readers, or not any more smiley for :D

Great Blog, great site, keep it up!
Victor

It's a JavaScript Issue With Firefox 3 (my frames breakout code). I get thousands of hits in my logs every day with it, which is annoying. It's now disabled, until I can get around to a workaround.



make it up ! :D - 19.09.09 10:33 am

Hey, i just a have a quick question.

I have a folder, lets call it FolderAA

So i want to redirect all access of FolderAA to /
This is easy to do ....

the problem is that when i create a folder FolderBBB that will simply redirect to FolderAA.

But since i wrote my redirect there, how can i make it go to FolderAA if it is only access from FolderBBB ?


Any suggestion would be helpful, since i cant seem to get it to work !

This makes my head hurt! You didn't supply enough information to give anything but a silly answer. ;o)



chris - 24.09.09 11:52 am

Hi, i need to add to each called url the parameter
&ctitle2=singles

for example. i have:

/achats/newyork/allchats.php?cid=1&ctitle=newyork
/achats/dallas/allchats.php?cid=1&ctitle=dallas
/achats/london/allchats.php?cid=1&ctitle=london

and it should redirect (so also i see it in the browser url

/achats/newyork/allchats.php?cid=1&ctitle=newyork&ctitle2=singles
/achats/dallas/allchats.php?cid=1&ctitle=dallas&ctitle2=singles
/achats/london/allchats.php?cid=1&ctitle=london&ctitle2=singles

i tried something like:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ctitle=(.*)
RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} (.*)
RewriteRule allchats.php %1/?ctitle=%2&ctitle2=Singles [R=301,L]

but that comes in a loop :()

hope you have an idea

thx
chris

THIS is why I ask (quite clearly) that folk post code inside [pre] tags. I basically don't have the time to decipher that to even begin thinking about what issues you want to leech my time to fix. Life is to precious to waste chasing after people who can't even read the article first.

Unless you learn to pay attention to the details, you will not have any success with mod_rewtite. ;o)



jignesh - 26.09.09 10:17 am

I want to make URL like http://craiglists.org using .htaccess file.

When i have select any city or state then URL make like http://cityname.craiglist.org/

Please help me to make URL like this

You need mod_rewrite for this. See that big article above the comments! That will help you. ;o)



Shonn - 28.09.09 3:15 pm

I am taking over hosting and managing an existing site that uses .htm as suffix for SEF urls.

My system (joomla) is using .html

I am using mod_rewrite and was hoping it could convert all .htm suffix and point them .html instead.

Is this possible please with .htaccess file?

I've no idea what SEF is, but the very first example on this page (repeated later, with details) sound like what you are after, switching out the different extension, of course. ;o)



francesco - 09.10.09 2:17 pm

Hi there,
Great article!!!
thank YOU very very much.
you guide (after apache one) is the best over the net!

I just have some more problem now about more than one attribute managment..
ehehehe..

If you had the time it would be great to had an example about transform more than one attribute to a complete seo-friendly path

Example:
/list.php?art=nop&macro=bestmore&sub=lastchoice&search=42
to
/list/macro/bestmore/sub/lastchoice.html

thank you again! You really help me!

There is an example of this in the article! ;o)




Jim Jackson - 16.10.09 12:11 am

I've really been struggling with RewriteRules and all the other on-line documentation wasn't making it any easier.

Your info was by far the best tutorial, it really got me out of a hole.

Cheers




james - 20.10.09 11:53 pm

I've been searching for something that would explain what exactly can .htaccess do and I'm telling you ... this post is the most comprehensive resource I could find.


Rahul - 26.10.09 10:52 am

This tutorial about .htaccess is really gr8.


Mom Bunheng - 28.10.09 2:54 pm

Hi,

I have create fake url like this http://www.myexample.com/service/grandbiz.html by using .htaccess rewriterule as below

#mod_rewrite in use
Options +FollowSymlinks
RewriteEngine On

RewriteRule services/(.*)\.html mainpag.php?page=$1

Only thing giving me problem is all images, css and, javascript request could not find because looking into services/images/, services/css/, services/js/ actually all my images, css and javascript virtually store at (myexample.com/images/), (myexample.com/css/) and (mydomain/js/), I would like to know is there any way within .htaccess to solve this problem.

Once again thank you for your contribution in this blog.

Bunheng

Yup. Check previous comments. ;o)



Worried - 29.10.09 8:02 pm

I am wondering if I can use htaccess to block a 301 referral from another website. Here's the situation. Someone registers embarrassing.url and with htaccess redirects this to niceperson.url. Now whistle blower points this out to the world and embarrasses niceperson. As there doesn't seem to be an http_referer record I can't see how to stop this coming in. Any suggestions?

The referer header is a browser (web client) thing. Some don't send it, but most do. Surely there is ONE inward link from this supposedly-evil domain that has referrer information!? Check your logs, find out the domain name, and then contact the the registrar, get something done.

And if there isn't a single inward link with a referer header, then clearly it's no issue at all, because no one is visiting that domain, whatever it is, or else it does not exist. ;o)



CraftyGeek - 03.11.09 6:59 pm

Great easy to understand article - its been a while since I last looked at using htaccess....the last time it was a tad scary - this is a whole lot more approachable & actually makes sense!

Good job & nice writing - shame about the peeps asking basic web questions, but hey, I guess we were all there once.

Thanks, this has been a real time saver smiley for :cool:

Craftygeek


Robert - 04.11.09 12:52 am

Thank you for your time and energy that went into making this. You have saved me a tremendous amount of trouble. I just wish I found this post this morning! Great work.


just made up - 04.11.09 3:18 pm

there is an example somewhere... but even that i can't follow...

how to mod_rewrite from /site/index.php?p=picture 1.jpg to /site/picture 1.jpg? or for that matter any filename that ends with jpg or png or bmp or gif? tnx.


Mike - 07.11.09 5:40 pm

Hi,

Thanks for all the info you posted here.

I'm still struggling with one thing though and it has to do with .htaccess file.

I want my password rules to apply to every file except for my index.php file.

I can not get this to work and have scoured many pages.

Here's what I have...it still prompts for user/pass when you hit the site

AuthType Basic
AuthName "AWAT Restricted Access"
AuthUserFile D:\Web\secure\awat.us.apasswords
require valid-user

<FilesMatch "index.php">
Order deny,allow
Allow from all
</FilesMatch>


I have also tried something similar to this (thinking I would FilesMatch would return true for all files but index.php) but that don't work either:

<FilesMatch "!index.php">
AuthType Basic
AuthName "AWAT Restricted Access"
AuthUserFile D:\Web\secure\awat.us.apasswords
require valid-user
</FilesMatch>

This seems like it should be trivial but its turning out to be a major pain.

Any help is greatly appreciated.

Thanks,

Mike




josh - 09.11.09 1:11 am

Hi, I just came across your site when trying to learn basic URL rewriting. I just want to use a simple URL to point to a complicated RSS feed address. Here is the command I used

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^g2albumrss$ gallery2/main.php?g2_view=rss.Render&g2_name=New+Albums

The result I want is that

www.example.com/g2albumrss

gets mapped to

www.example.com/gallery2/main.php?g2_view=rss.Render&

Should this work as I have written it, or am I doing something wrong. My hosting company says any changes to .htaccess should be visible immediately. Thanks for any help you might be able to give.

Cheers!


Andrew B. - 17.11.09 2:23 am

Thanks for the good info on htaccess. Even though it's still arcane to me, you've put made it much less so!

Now, is it possible with just htaccess to do redirection, but retain the originally entered URL in the user's browser window?


thomas crown - 19.11.09 7:46 pm

hey the link http://www.ilovejackdaniels.com/apache/mod_rewrite-cheat-sheet/ has been updated to http://www.addedbytes.com/apache/mod_rewrite-cheat-sheet/ please change it. Thanks for providing this tutorial it has really helped me out


Greg - 20.11.09 2:59 am

Greetings,

This is a great site,and I have a couple questions that I was wondering if you or someone more knowledgeable that I could answer. Let me explain my setup. I have 2 domains called www.test1.com and www.test2.com. I have Joomla installed in different folders on www.test1.com and want all traffic to go to there.

This is what I want to do:
1. Anyone going to www.test1.com will be re-directed to www.test1.com/sites/test. I have accomplished this part, but I want the URL in the browser to only show "www.test1.com" with no folders listed. I have problems with this part.

2. Anyone going to www.test2.com will be re-directed to www.test1.com/sites/test2 while only showing "www.test2.com" in the browsers URL, again, no folders shown. I have a feeling that if I can get the answer to the first question, it would solve this problem as well.

Is this possible using .htaccess? And if so, I would appreciate anything that can help me accomplish this goal.

Thank you,
Greg


Boris Vujicic - 24.11.09 12:28 pm

Ok smiley for :)


The Hotest Girl in my Space :) - 24.11.09 3:56 pm

Thanks a lot! I have really modified my htaccess now!

A caveat though, you say that you have another version of debug_report.php in the archive. I didn't find it *humpf*

So I downloaded the one shown on your eminent page 2.

I guess that's the one I should use?

would appreciate if you e-mailed me

Thanks for a great site!smiley for :ken:


Alex - 30.11.09 11:17 am

Hi i have a problem with mod rewrite

(it work)
RewriteRule ^music/( <^/>+)$ music.php?artist=$1
http://www.xxx.com/music/queen

(don't work)
RewriteRule ^( <^/>+)$ music.php?artist=$1
http://www.xxx.com/queen

pls help me smiley for :ehh:


Chris - 30.11.09 5:21 pm

Try...

RewriteRule ^([^\/]+)\/?$ music.php?artist=$1

Should allow both:
http://www.xxx.com/queen
http://www.xxx.com/queen/

To go where you want...


Sam - 02.12.09 6:32 pm

Hi there,

I am trying to remove the www from the URL. I am developing my app in Codeigniter and the code above doesn’t help me :( Do you guys have any idea how to get the Codeigniter working with it?

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/index.php/$1 [R=301,NC,L]


The url should work as http://example.com/controller/method but it dosen't like it :(

My working code to achive the above url format in Codeigniter for both with www prefix and without it is

    RewriteCond $1 !^(index\.php|robots\.txt)
    RewriteRule ^(.*)$ index.php/$1 [L]


Any you guys know how to get raid of the www and keep the Codeigniter happy?

Best,
Sam


NiAlMa - 03.12.09 5:38 am

i have a .htaccess with the following rules

RewriteEngine on
RewriteRule ^(.+)\/$ index.php?show_page=$1
RewriteRule ^([^/.]+)/?$ index.php?show_page=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?show_page=$1&extra=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ index.php?show_page=$1&extra=$2&extra_2=$3 [L]
ErrorDocument 404 /404/

/404/404 is created but when i click an link that should be display my 404-page i get the normal 404-page from the server....

Could anyone tell me what the misstake is?

I can grant a demotestversion for testing.

Greetings from Germany


esjabe - 05.12.09 4:39 am

First off... outstanding.

Secondly, I have a question regarding HTTPS rewrites and I did not see anything in the article mentioning this.

I have SSL set up on the root domain , and a subdomain setup in a folder on the root. I would like to rewrite the HTTPS requests to not include the domain. In other words, I want the main domain transparent to the user so they are not confused when the domain shows up all of a sudden when the go to check out.

Currently:
http://subdomain/unsecure_foo.php (this is good)
https://domain/subdomain/secure_foo.php?blah=foo?blah=bar

Preferred:
https://subdomain/secure_foo.php

Is this possible? I ask because I would like to use a single SSL cert to secure several subdomains.

Thanks again for sharing your expertise...
Regards
Steve


stefan - 05.12.09 3:00 pm

This article is
fantastic
, BUT...

1. I still dont know what ! means
2. and how to get $_POST variables while the page is redirected


thanks a lot for this article






SINTAX - 05.12.09 3:14 pm

First off, great article. I have read through it a couple times and am using a few of your examples in my current .htaccess file. I am having one problem that I can't seem to figure out. It is in regards to the rewriting with variables deal.

What I'm trying to accomplish:

http://example.com/view.php?login=$login

... and rewrite it to:

http://example.com/$login

(much like you find on sites like Twitter where your username appears to be a directory.

My current RewriteRule looks like this:
RewriteRule ^(.*) view.php?login=$1 [NC]

... but it's not doing the trick.

Thanks for any assistance you can send my way.


muppetman - 06.12.09 7:11 am

Hi, I just wanted to say out of all the sites I've been to trying to figure out this code this is by far the BEST. You make it so even I can understand it (somewhat...lol).

I've purchased a SSL and am currently trying to make site so that only certain pages are secure. I'm having great difficulty figuring this out but after reading through your site I think I may be able to figure it out. Hope I haven't jinxed myself.

Thanks again!


George - 07.12.09 3:11 pm

I need to be able to make a folder access case insensitive.

eg.

http://server/ab/

I want is accessible from the following urls

http://server/AB/
http://server/aB/
http://server/Ab/

Please advise.


hussain - 10.12.09 9:50 am

Hi i need a little bit of help. my requirement is to fix the page name with extension and variable name and the www because i want to make my URL more short
like below url
OLD URL:
http://www.globalguideline.com/index.php?JScript=first_JavaScript

NEW URL (Required):
http://globalguideline.com/first_JavaScript

Please guide me,
Regards,
Hussain


ILikeToWhoop - 20.12.09 12:40 am

Awesome ... Awesome ...


jordan - 20.12.09 3:32 am

Hi,

i have a problem with .htaccess for main directory

like:

RewriteRule ^list/([^/]*)$ details.php?type=video&pname=$1 [L] 


in url show as example.com/list/blablabla
but how do i remove list(1st directory) like example.com/blablabla without (list) ?

this will work but will affect all other page
 RewriteRule ^([^/]*)$ details.php?type=video&pname=$1 [L] 


please advise,

thanks man


JD - 21.12.09 6:46 pm

Thanks for all your work on this site. It's been very helpful to me. I'm wondering if the following is possible using htaccess.

I'd like to send users to different webpages after they click one link based on their credentials. In other words,

User 1, clicks link "a" and goes to User 1 photo page.
User 2, clicks link "a" and goes to User 2 photo page.
User 3, clicks link "a" and goes to User 3 photo page.

Is this even possible. If it is, would you be able to point me in the right direction?

Thanks!


some webmaster - 22.12.09 10:58 pm

I have been trying to make a redirector that drops log entries on my server for outbound links.

The ideal format would be

http://www.myexample.com/link/http://www.google.com/search?q=something

should redirect to

http://www.google.com/search?q=something

while at the same time leaving a log entry on my access logs showing the 301 redirect.

I would also like this redirection to work for https:// urls.


So far, the best I have come up with this:

RewriteCond %{REQUEST_URI} ^/link
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://%{QUERY_STRING}? [R=301,L]

That will redirect, for example http://www.myexample.com/link?www.google.com/ to http://www.google.com/

This is sub-optimal because:
1) I can't pass query strings in the links
2) It has the "http://" hard coded and therefore won't work with https:// links.

Does anyone have any suggestions?



danilo - 23.12.09 4:25 pm

Hello, I'm making a server-side rewrite that reads foo=1 when user puts example.com/category/, thats my code:
Rewriterule ^category/ /?foo=1 [L]

rewrite mod works, its simple
But if exists a file called category.php, then rewrite mod doesn't work

I tried do this:
RewriteCond %{REQUEST_FILENAME} !-f
Rewriterule ^category/ /?foo=1 [L]

and this:
Rewriterule ^category/$ /?foo=1 [L]

and also I tried this:
Rewriterule ^category([^\.php])/$ /?foo=1 [L]

But doesn't work. I need help smiley for :erm: Is possible this rewrite?? or i have to delete category.php file?




Ajay - 25.12.09 1:42 am

Hi, very nice examples easy to understand most of it. But still I couldn't achieve what i was trying to do with one of my project.

i have a dynamic url
http://www.sitename.com/productshowcase.php?product=2342343&name=Ra Guit

and want to display as

http://www.sitename.com/travel-guitar/Ra-Guit

Can you please help me with this. I tried all i could find but still not able to get it.

thanks a lot for the article with self explanatory examples.




Casemon - 28.12.09 1:49 pm

Awesome page. Really does help break down mod_rewrite, and with clear and easy to understand examples.

Have a current issue, that might be inline with your examples.

Basically have a database driven site & have added a database driven forum. The site appears in the root, and the forum is hosted out of a child directory of root. Nav links on the site have the forum in 5th position (5th link from home), and a blog is in 2nd position.

Everything is working, save for 1 thing; the "current_page" markup is showing all the forum pages as being part of the blog. Basically the forum offers up 2nd order pages (from the forum subdir) and the navlinks are interpreting this as being from the blog.

Does that make sense? I really have no clue as to how to resolve this. Thought it might be a common mod_rewrite issue, given many people integrate forums into their sites, but haven't found any useful info on the matter.

Any ideas? Would it be worth addition to your already stellar mod_write...

Thanks in any case smiley for :)


email - 06.01.10 5:59 pm

This code works on my local server xampp php5+ but does not work on remote server (1and1) php4+.

Would like to rewrite
http://myexample.com/redirect/name-of-link
to
http://myexample.com/redirect/index.php?name-of-link

Options +FollowSymLinks

<IfModule mod_rewrite.c>
	RewriteEngine On
	
	RewriteCond %{REQUEST_FILENAME} -f [OR]
	RewriteCond %{REQUEST_FILENAME} -d
	RewriteRule ^(.+) - [PT,L]
	
	RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>


Thanks just something to work on if you have time.
Thanks Email


email - 06.01.10 8:45 pm

Thanks to anyone who started to help but
I found the answer. For those who love(me) or hate 1and1 all that was needed was a little time digging around the net (.75 hours) and experimentation.

Fix: 'AddType x-mapp-php5 .php' and 'RewriteBase /subdir' to .htaccess in subdirectory


AddType x-mapp-php5 .php
Options +FollowSymLinks

<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteBase /subdir
	RewriteCond %{REQUEST_FILENAME} -f [OR]
	RewriteCond %{REQUEST_FILENAME} -d
	RewriteRule ^(.+) - [PT,L]
	RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>



Thanks
Chow


George - 09.01.10 8:21 am

Nice page ! I managed to do a few rewrites based on what you have here, however, I'm stuck with 1...
I've changed my old picture-gallery to a new gallery.

Old links:
http://www.mysite.com/photos/details.php?cat_id=750&image_id=53974
http://www.mysite.com/photos/categories.php?cat_id=750

New site:
http://www.mysite.com/album.php?yr=2009&aid=0750

The image_id can be ignored as the new site works different with images, but we did manage to use the same category/album numbers. On the old site this was the "cat_id", on the new site it's "aid".

So it's the part where I put "750" that's important. Also, the new site works with 4 chars, so there needs to be an extra "0" for the aid..

I hope it's not asking too much... :P
In advance, thank you very very much !
George.


Stevens - 13.01.10 2:34 am

Hi,

I am facing a problem... I would like to send an HTML with my domain name "in the link", but while clicking the link I want it to be redirected to another website....

http://myexample.com/xxxxxxx --------------- Click ------------ "another website"

Is there anybody can help, it will be really appreciated...


fairuz - 13.01.10 3:15 am

i like to change my MLM script into simple referral url.

for example, http://www.penaja4u.com/fms016 instead of http://www.penaja4u.com/?id=fms016

can somebody help me with the complete code??

i u know, please email to fms016@gmail.com

thanks in advance..


Martin - 15.01.10 8:14 pm

After days of seeking a good htaccess tutorial I found your page.
In a few hours I read and test a few scripts on my server.

Thank you very much!!
I will read more of your great written tech!

Regards,
Martin



jesse - 20.01.10 7:40 am

Hello, thanks for the tut. I refer to this page often.

I am having a problem with a recent .htaccess that I would appreciate some help with. I have read the page several times, as well as other pages.

Here is what I am trying to do. 1 page on this site is secure for example https://someexample.com/purchase/pay-cc

but all other pages are not supposed to be secure. https://someexample.com/purchase/view for example should re-direct to http://..../purchase.

I have tried many variations, but this is the one I think should work and does not.

# only for https pages.
RewriteCond %{HTTPS} =on

# only for pages that do not end in pay-cc
RewriteCond %{REQUEST_URL} !pay-cc$

# re-write the url to a not https
RewriteRule ^(.*)$ http:\/\/someexample.com\/$1 [R=301,L]

The result of this is all request are re-written to http version of the url.

My understanding was that if a request like /purchase/view does not match pay-cc$ the re-write would stop evaluating and no re-write would happen.

Thanks in advance
jesse




Brian - 27.01.10 6:17 am

I did two htaccess 301 redirects, both sucessful, both pointing to one domain. The problem is with my Joomla site. The second redirect was from the index.php to keep everything on .com for SEO reasons. The moment I messed with php, I was not able to use the plugin's on my Joomla site such as "client login" or send an email directly from the site. Yes, removing the second redirect fixed all.

I was able to add a line to use the administration when the php redirect was up, but when I tried others to access my plugins, they did not work.

Question? Does anyone know a htaccess code that enables these plugins while at the same time using the 301 php redirect?

Thanks!


madeupname - 27.01.10 6:35 am

The best I have seen for htaccess tutorial. Kudos!

My only negative comment probably has to do more with regex, and my lack of understanding. But it is hard to decipher not knowing where the regex starts and where the htaccess syntax ends. You delved into it somewhat, but it would really help for some of the examples to be broken down char by char. ie

{HTTP_REFERRER} = php variable for anonymous requests from another domain.. or whatever it actually means.

(.*) = idk?

I guess what I'm saying is a step through of the syntax, not necessarily a regex tutorial.
I'm still not sure what the ^ does exactly.

As I say it is still very good information for getting my head wrapped around htaccess. Thanks for your time and effort.smiley for :cool:


TAJ - 27.01.10 6:46 am

Trying to figure out some rewrite rules to add su.pr short URLs on my domain. However everything I do is unsuccessful -- either the new code is not working or I have redirection issues.

The existing code in my htaccess file (http://trans-americas.com):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://www.trans-americas.com/errordocument.html
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^trans-americas\.com
RewriteRule ^(.*)$  http://trans-americas.com/$1 [R=301,L] 
Redirect 301 /wordpress http://trans-americas.com/blog
AddType x-mapp-php5 .php 


The code that is supposed to be added for su.pr
http://www.stumbleupon.com/developers/Supr:Short_URLs_on_your_own_domain/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[a-zA-Z0-9]{4}$ supr.php?supr=$0
RewriteRule ^supr_settings.json$ supr.php?check_install=$0
</IfModule>


No matter where I place this code it won't work.
Any help would be appreciated.

Thnaks







Uday Chatterjee - 04.02.10 6:03 pm

Hi all,
How do i redirect my site to a folder with .htaccess(i.e. if i type www.test.info it is redirect to www.test.info/myfolder). Plez Help.Its urgent.


Eva - 11.02.10 4:09 am

my site is <> can't use rewrite from php to html

I use .htaccess this below rule.

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.html $1.php

It's still error 500

Please, suggestion to me.

Thank you.
Eva


Sam - 12.02.10 11:54 am

Just to say that this is a very informative and easy to follow tutorial- thank you!


RMWChaos - 16.02.10 11:13 pm

Excellent work here, Cor! I think I've dnld'd most of your code now. smiley for :evil: Seriously tempted to press that "donate" button, too, which NEVER happens. I blame the economy.

One comment, and it's more of a philosophical choice, I suppose - passive-aggressive vs. just plain ol' passive...

When using redirects on 400/401/403 errors some prefer to to respond with some snotty message like "can't hack this, da-na-na-nah" etc. However, doing such passive-aggressive things tends to egg on those who are out to cause havoc and chaos.

So why not just (passively) redirect back to the main index page without any fuss like with 404 errors?? Hopefully, they just get bored and move on.

As my dear ol' Da used to say (still does, actually), "Don't get in a pissin' match with a skunk." Good advice. Will Smith says it better, "Don't start nuttin', won't be nuttin'."

As for your Auto Hotlink code ... sooooo loooove it!!!! This is one case where I'm more than willing to leverage off the bad behavior of others for my own benefit. It's all good ... or bad ... whatever. smiley for :evil:

Just my two-point-five cents.

Danke!

-RMWChaos


Gabe - 24.02.10 7:19 am

Awesome htaccess tutorial page smiley for :D!!!
Thank you so much for sharing your tips (and twicks)!!!
Bookmarked your page smiley for ;)!


binu - 04.03.10 6:15 am

i have a website url like this
www.website.com/packages.php
www.website.com/package_list.php?pid=25
www.website.com/package_details?pId=25&sid=1

I want to rewrite above url as follows, Please help me

www.website.com/packages/
www.website.com/packages/honey_moon/
www.website.com/packages/honey_moon/2days_3nights


RewriteRule ^packages/ packages.php
RewriteRule ^/packages/(.*) package_list.php?pid=$1

first rule is working .



YW - 09.03.10 1:55 pm

I have a domain that I want to redirect to another site that I own.

I want people to go to:

http://tiny.url/123

and redirect it to:

http://fullURL.com/directory/123.html

Thoughts?


CK - 09.03.10 11:14 pm

my website is facing script injection attack for a month which comes at the end of head tag..now my site hosted in linux..created with simple html..If I change the .htaccess file will it do?


CK - 09.03.10 11:18 pm

script injected just after the end of head tag or after the completion of html tag(at the end of the page)..what precisely can I do to stop this?


Ayo - 17.03.10 3:35 pm

Thanks, very helpful


gvs - 22.03.10 9:50 am

sdvsfb gf


Lau - 25.03.10 5:22 am

Excellent article ... the best I've found on htaccess ... well done ... much appreciated.

Regards
Lau


prashant - 27.03.10 11:33 am

https://corz.org/serv/tricks/htaccess2.php-fr

this link is not working and it still present. please update it to recent version

thank you.


Syphon - 05.04.10 7:31 am

@prashant

Works for me!


Nick - 07.04.10 3:43 pm

I've been at this for two days now. At one time we had it set up where we could type in fog and it would be redirected to http://10.65.12.6/fog/management/index.php This is no longer working for us. Any ideas/suggestions would be greatly appreciated. I've tried everything except for what works.


Bruce - 10.04.10 2:53 am

Hey Cor you saved my ass man (and probably my job too).

My specific problem was interfacing ExtJS to CodeIgniter.

The ExtJS client code wants to send URLs with parameters as query strings but the CodeIgniter MVC on the server side expects them as search friendly URL parameters or as CI call them "segmented URLS". As an option you can enable query strings within CI but you lose some of the functionality in the helper classes that rely on URL parameters. I really didn't want to have to resort to query strings.

There are so many tutorials out there on how to achieve the reverse; how to input friendly URLs on the client side in say a browser and have mod_rewrite converted the embedded parameters to a query string based URL for the server to consume.

After an hour of searching this is the only tutorial I found that helped with my problem.

Cheers dude!


dr3w - 10.04.10 9:44 am

Great tutorial! indeed very helpful.
thx a lot


CH ASIF MEHMOOD - 14.04.10 2:05 pm

I will be great full if you write the whole story in a simple way so it might be useful for some one.
Thanx


eric - 14.04.10 11:31 pm

Sweet post, thanks! I've definitely put some of this to use..

Any chance of an example .htaccess that will redirect to www.example.com when the .htaccess is loaded from example.com but ignore the rewrite rule when the domain that's being loaded is dev.example.com?


Tithi - 16.04.10 11:08 am

i have uloaded my website & it includes .htaceess file in it but when i see this on browser then it comes as index of/

i want to see home.php just written the url...
the .htaccess file is uploaded in public_html
help me in


Julien - 16.04.10 2:56 pm

Excellent, best tutorial accessible from 1st page result in google, well done.


Gepar - 22.04.10 10:09 pm

Cor, wonderful work!

I wanted to ask how to get rid of one inner dir, f.e.

sitename.com/vmchk/view-all-products-in-the-shop.html

to form

sitename.com/view-all-products-in-the-shop.html

How can I get rid of vmchk/ in this case?


Dez - 23.04.10 6:26 pm

Thanks for the help. I used the no www non-hardcoded version to make my dedicated server work correctly with multiple domains.


vivek - 26.04.10 8:06 pm

THIS MANI KUMARI


Ed7991 - 26.04.10 9:09 pm

HELP!!
i dont have a clue where to put my .htaccess file!
my file system is like so
>mysite
=>folder1
==>index.php
=>folder2
==>index.php
=>files e.t.c

i want to be able to redirect
mysite/folder1/foo
to
mysite/folder1/index.php?s=foo

this is my htaccess so far
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^folder1/( <^/>+) /folder1/index.php?s=$1
RewriteRule ^folder2/( <^/>+) /folder2/index.php?s=$1


but it doesn't work
any help will be appreciated


Jig Patel - 28.04.10 7:58 am

Hello

I have url like http://example.com?username=jigpatel

and i want to rewrite this url as

http://jigpatel.example.com

is there any one has idea how to do that?

Please help me its very urgent..


ed7991 - 28.04.10 5:03 pm

the way to do this is
call a file index.php (note you must have php enabled and installed)
[coderz]<?php
	header('location: http://'.$_GET['username'].'example.com/');
?>[/coderz]

and it works!!

note: replace "example.com" with your domain name


juanune - 02.05.10 3:19 am

There's no reason to do a PHP redirect here. You're better off with an actual rewrite because you can/should mark it is as a 301 request.

That being said, if you're going to do this, you'd better have an A name record set up for the users, which pretty much means running your own dynamic nameserver.

I expect that if you can't figure out how to do the URL rewrite though, that this will be beyond you. If you need it in a hurry, my recommendation would be to call a professional.


kjv1611 - 06.05.10 12:52 pm

Just wanted to drop a thank you for this page - .htaccess 2..

One piece of it helped me more than all the other .htaccess stuff I've read so far. Given that info, I plan on doing more reading across your site to see what else I can learn.

Thanks, and keep up the good work!


TheMuffin - 06.05.10 10:58 pm

You are NOW officially my GOD of the net!!! I thought I could teach myself enough to make things ok..I missed a whole bunch and lost one of the domains on a host with 3, thanks to my crappy addition to the .htaccess file...

THEn by the grace of all things webby... I found you and now all things make sense and work like a charm... I have even managed to have the links in the emailer redirect nicely!! (I had, like a fool, changed some file names,when i flipped the full site to php

Thanks again to you and your very clear article it all is as it should be .. and I no longer seem to be having so many blonde moments!!

You rock...
xoxosmiley for :roll:


Thankful reader - 07.05.10 2:44 pm

What an amazing write-up. I rarely come across well written articles like this one. Thank a lot!


Jan - 10.05.10 12:09 pm

what's the rewriterule for changing

http://www.example.com/topbranch/subbranch/leaf

into

http://www.example.com/topbranch_subbranch_leaf.htm

???


meszoltan - 13.05.10 6:09 pm

Hello

I want to send some parameters from image to database, and my idea was this:
Visitors can see images with links on the site, like 'http://mysite.com/image.jpg'
With apache config file i can redirect request to a php file, with parameters of the image.
After saving parameters to the database, php file is reading the image source, and user can't see anything from this, just a loaded image.

<?php
header
('Content-type: image/jpeg');
$folder urldecode($_GET["folder"]);
$file urldecode($_GET["file"]);
$url "anyfolder/".$folder."/".$file;
$header "filename=image.jpg";
header($header);
readfile($url);
?>


I think my problem is to know, how can i do a condition in apache config file:
just if script filename is NOT '/visit.php', then do next row, something like this:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{SCRIPT_FILENAME} !^visit.php$
RewriteRule ^( <^/>+)/( <^/>+).jpg /visit.php?folder=$1&file=$2.jpg

Can anyone help to resolve this problem?
Sorry for bad english!



Mark - 23.05.10 11:06 pm

I like the information here , but the site seems very hard to read the code based on the color schemes used for the code. I hope you think about changing this. Some of us are getting old!

I have searched high and low for rewrite rules. I need to do 2 things

I need to load a different favicon if the user comes to subdomain.myexample.com rather than www.myexample.com or myexample.com. I think I can do this in the .htaccess but can not find out how when it relates to only a specific file. for instance if they go to subdomain.myexample.com/favicon.ico they will get (transparently to them) othericon.ico, and all the rest will see the normal favicon.ico

Additionally, I want to know if it is possible to redirect a subdomain via the ht access file so that if I go to bob.myexample.com the server loads myexample.com?user=bob.

Any help on these is appreciated.

Mark
markosjal OVER AT gmail WITH A com




Musta - 01.06.10 9:05 am

First of all thanks a lot for this useful information to get started with the painlful world of htaccess. I have checked high and low for a solution to enable .htaccess files on my Macbook pro but in vain. Apart from the apache that is built in my machaine I have installed a Mamp server to have a local server for testing. Everything works fine but the problems arise when I tried to implement SEO friendly urls by using .htaccess files. When I include this latter in the root of my sites (MAMP/htdocs/Mysite/.htaccess) it does not work and cause the following error.
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, you@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Apache/2.0.63 (Unix) PHP/5.3.2 DAV/2 Server at localhost Port 8888


and in my Mamp apache log file I get the following error:
[Tue Jun 01 09:46:40 2010] [alert] [client ::1] /Applications/MAMP/htdocs/Mysite/.htaccess: Invalid command '{\\rtf1\\mac\\ansicpg10000\\cocoartf824\\cocoasubrtf480', perhaps mis-spelled or defined by a module not included in the server configuration, referer: http://localhost:8888/Mysite/index.php?DepartmentId=2&CategoryId=4&Page=4


These are the errors that I always get even when inserting an empty .htaccess file. I have changed the AllowOverride line from None to All in both the Mamp and the httpd.conf that ships with the machine but in vain. I have also made sure that the following lines: AccessFileName .htaccess andLoadModule rewrite_module modules/mod_rewrite.so are uncommented but in vain. I just do not know wht is going wrong with my local testing server. I hope that you could help me ride of this nightmare that I have been fighting for more than 5 days.

Any help would be greatly apreciated, And many thanks in advance


Mohamed Ali - 02.06.10 12:53 pm

Many thanks for such info, i have been working on improving my site's seo, and your article helped me allot on improving my site's indexing.


Eamonn - 08.06.10 11:57 pm

thanks man for all the research and for sharing.

The www. to non-www. redirect is excellent

Thanks
Eamonn
www.lifescience.ie


Zahi - 18.06.10 1:07 pm

Author, Expalinex each and every case in the post and very easy to understand and i also found the dubug example by the end of page and hopes it will give lot of new things to me

Thanks
Zahipedia.com admin




shay - 18.06.10 9:33 pm

this work


Arun Gautam - 23.06.10 4:17 pm

RewriteRule  images/(.*)  images.php?url=$1 [NC]

i have this but this does not work for url containing special character
like: images/gallery/thumb/black&white.jpg

i only get: images/gallery/thumb/black in $_GET['url']

please help.



Hungry Mind - 06.07.10 8:53 pm

Awesome Tutorial... Im Sure This Is The Only One Best Tutorial About .htacces, Gr8 Job =)


Hungry Mind - 06.07.10 8:56 pm

@Arun Gautam:

Can U Plz Explain What U Wanna Do Actually?

Do U Wanna Block This Image On Direct Hitting It's URL?


Bobby Ong - 09.07.10 5:32 pm

You saved my life! I've been looking dead hard for this guide.


Thanks a million!
BobbyOng.net


yoyoman - 11.07.10 11:06 am

Thanks man, you are a very smart dude. Saved the day smiley for :D

http://www.howtomakemypenisbiggerreview.com


Paul - 17.07.10 3:10 pm

Hello.
I have a nail biter. I have solved in part, but are missing 2 out of 3 parts, lol.

I am trying to use Scandinavian characters åäö in the URL but get stuck on that the .htaccess doesn't want to cooperate. I'll explain:

I have a Q&A (frågor och svar) link so I start with:
RewriteEngine on
RewriteBase /[L]


then this is a normal URL rewrite, pretty straight forward, loading index.php?id=10:
RewriteRule ^/?fragor-och-svar/$ index.php?id=10 [L]
No problems. Above rule works perfect.

But what if I prefer "frågor" (that means questions) in my URL before "fragor" (that doesn't mean anything):
RewriteRule ^/?frågor-och-svar/$ index.php?id=10 [L]
lead to a 404 error.

I know that å is translated to %c3%a5 or %C3%A5, so I test:
RewriteRule ^/?fr%c3%a5gor-och-svar/$ index.php?id=10 [L]
RewriteRule ^/?fr\%c3\%a5gor-och-svar/$ index.php?id=10 [L]
RewriteRule ^/?fr%C3%A5gor-och-svar/$ index.php?id=10 [L]
RewriteRule ^/?fr\%C3\%A5gor-och-svar/$ index.php?id=10 [L]
that all lead to a 404 error.

I am curious on what code there really is that å is translated to so I find after some elaboration that it is supposed to be EXACTLY 2 characters, although it comes out as index.php?id=%c3%a5 after using following code to [R]edirect me. Strange...
RewriteRule ^/?fr(.{2})gor-och-svar/$ index.php?id=$1 [R]
Above rule does NOT give me a 404. But it says that 2 characters equals %c3%a5 ???

And
RewriteRule ^/?fr(.{2})gor-och-svar/$ index.php?id=10 [L]
takes me to the right place! Problem solved! Almost... because I can't let it go! I am curious on what å really is supposed to be. And after looking around on many many pages I find that someone got a messed up code for å in his URL: Ã¥

Now I test it:
RewriteRule ^/?frågor-och-svar/$ index.php?id=10 [L]
It works like a charm!!! If I use Ã¥ for å.
Question is what strange code is this???
If I can figure that out then maybe I can do the same trick with ä and ö.
I think it has something to do with differences between UTF-8 and iso-8859-1.

Grateful for any insights.


Matthew R. Kee - 19.07.10 4:21 am

How can we direct request for corzissuper.com/guide/ to corz.org/guide/ and have corz.org/guide/ show in the browser while using domain alias.

corzissuper.com is a domain alias of corz.org to reduce hosting but it won't change in the browser.


Eric - 20.07.10 10:44 am

Thanks a lot. Really nice work.


chrisw - 20.07.10 12:50 pm

Thanks so much for this excellent page. I've always struggled to understand htaccess and have mostly thrown plausible bits and pieces together by trial and error. But this actually gave me a pretty good understanding, for once, and thanks to your examples I was able to write exactly the htaccess file I wanted within literally a couple of minutes of leaving your page and have it work first time. Excellent job!


vijay sharma - 23.07.10 12:32 pm

but where is the .htaccess file ?????? plz tell me


DarkNight - 27.07.10 12:17 pm

I have one .htaccess file, which i have to use in one of my project.
But I don't know what exactly it has, Can any body explain me what my .htaccess file can do?
my .htaccess file code:
---------------------------------------
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php
---------------------------------------

Thanks in advance,

DarkNight.


Jithin - 30.07.10 11:02 am

wow!!!!
Nice write-up!!


Tommy - 02.08.10 6:11 pm

Here is my current HTACCESS file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]


It is setup to remove function with/without a ".php" extension. What I want to try to emulate is how MVC handles URLs. I just need one more parameter added to the end so if I do:

http://examplesite.com/webapp/projects/add

Where "projects" is actually a page, projects.php, and "add" is a function I can automatically load if my web app finds that it is present. So, the basic problem is, how to add the "/add" to the end of the URL without causing an error and making it possible for my web app to view it.

An additional problem I am having is that if I point the domain name to http://examplesite.com/webapp/projects/ (With trailing slash), I get a 500 Error.

Any help would be greatly appreciated! Thanks so much.


Rajesh Singh Tomar - 06.08.10 12:30 pm

Hi,

I want to write the htacess rule like this.

http://www.mysite.com/xyz . (http://www.mysite.com/file.php?str=xyz)

For the english string i have write the rule:-

(xyz is utf-8 string . Ex:- this will be in russian languages also)


Please help me how to write the rewrite rule for this.

Thanks in Advance.


Prof.Yeow - 09.08.10 9:16 pm

THANKS SO MUUUUUUCCCHHH!!!
great guide! smiley for :D
now i can have my profile page and my project in the same server!
THANKSSSSSS!!!


flablog - 11.08.10 11:29 am

Hi there
thanks for this useful guide !

But i'm facing a little issue.
I've my own tinyurl domain, and I would like to add the option that if you add the domain at the beginning of an url it will made a tiny url automatically.
So everything is working but some special characters, like the // are rewrite just like that : /
so
myexample.com/http://www.google.com
became :
myexample.com/index.py?u=http:/www.google.com
and.. it doesnt work..

Here is my htaccess 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !index.py$
RewriteRule ^(.*)$ index.py\?u=$1 [L]

What can I do?

Thanks smiley for :)


Sanjay - 12.08.10 2:34 am

Hi there

I had created a new sub-directory and using .htaccess file redirected from root to new sub-directory. However, I see that the root/forum is also redirected.

I am using root/forum on the new web address in wrapper. Could you please help me in excluding root/forum from being redirected.

Thanks,

Sanjay


DrunkMunki - 16.08.10 7:19 am

@Arun Gautam: you should never use & in file names, just remove them and the url should work, because & is used in the link for scripts and such you should not use ? and & in file/folder names.


Bipul - 18.08.10 8:10 am

Hello,
good writeup but a little confusion for me here:
I have a website (developed in wordpress) with a lot of dynamic pages. we added some 350 posts to it (inserted to database directly) and have URLs-rewritten. However, google webmaster is showing these new pages as "NOT-FOUND" while all the URLs are actually working.

one of such URL's is - http://tinyurl.com/25jn2jk (using tinyurl to avoid my competitors able to find it).

we are starting to lose a lot of traffic :( please advice.

Thanks, Bipul



Snacky - 18.08.10 6:32 pm

You had just what I needed. TY!


Steve P - 18.08.10 10:03 pm

@DrunkMunki

Regarding your comment to Arun, what should you do when your URL is based on PHP parameters and one possible value for the parameter contains a special character like an ampersand?

For instance, if you are talking about college basketball teams, your parameter for the school/team might be "William & Mary" or "Texas A&M". In these cases, the rewrite rule breaks down and treats the parms as "William " and "Texas A".

What to do then? Anyone? Anyone?


Joao - 19.08.10 6:30 pm

Thanks for this article!


CoYaN - 21.08.10 2:04 pm

Hello

How can I get the redirect to
http://www.example.com/search.php?tag=keyword

become:
http://www.example.com/keyword

Thank you in advance

------------------------

Bonjour,

Comment faire pour avoir la redirection pour que
http://www.example.com/search.php?tag=keyword

devienne :
http://www.example.com/keyword

Merci d'avance


Bogdan - 21.08.10 3:54 pm

I usually don't comment on posts, even if them helped me. But this article is great ! Very good job smiley for ;)


raj - 27.08.10 5:09 pm

hi,

I have problem with hyperlink, when I try to insert, it takes
only index.php?........
But the url won't open unless has : http://www.example.com/web
is included.

please assist .

thanks


K - 01.09.10 3:42 pm

Hi Corz,

could this be a typo in the "simple rewriting" section:

Handy for anyone updating a site from static htm (you could use .html, or .htm(.*), .htm?, etc) to dynamic php pages;


Should it read:

Handy for anyone updating a site from static htm (you could use .html, or .htm(.*), .html?, etc) to dynamic php pages;


Maybe I got that extra "l" wrong?

K


Ragha - 02.09.10 1:15 pm

I want user to access only .php files and others (like .js) should be accessed inside php files. So how to prevent direct access to a file like .js or .css? Please help me...


praveen - 03.09.10 3:50 pm

HI this is nice script for freshers to implement this


ramesh - 03.09.10 7:16 pm

no comment
information is use full
thanks


Vanessa - 14.09.10 8:43 am

Hello. I would like to redirect http://www.example.com/travel/?p=1&q=arenal+volcano&region=1 so that it turns to http://www.example.com/travel/hotels/arenal-volcano

I don't know how to write the rule. I've tried several times, but it doesn't work for me.

Could anyone please help me?

Thank you very much smiley for ;)


Larry - 14.09.10 9:38 pm

I have three rules in my .htaccess and only one of them seems to work:


RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?p=$1&id=$2
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?p=$1&cat=$2
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?p=$1&sort=$2


So in essence if I have, say, www.site.com/somepage/123 this will translate correctly to www.site.com/?p=somepage&id=123 but the latter two rules won't work - why?

Here's my whole .htaccess file if it helps any. Everything up until the "cat" and "sort" translations works beautifully.


RewriteEngine On
Options -Indexes
Options +FollowSymLinks
RewriteRule ^admin/$ /admin/index.php
RewriteRule ^(.*)/$ index.php?p=$1
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?p=$1&id=$2
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?p=$1&cat=$2
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?p=$1&sort=$2




WILLIE - 16.09.10 10:07 am

Thnak you very much for this. It was very very useful. I resolved lots or problems I have been having with my website. Keep up the good work.smiley for :lol:


angelverde - 16.09.10 10:47 am

smiley for :eek: OMG work!


sandeep - 17.09.10 5:31 pm

i must tell that u have done very great and good research on .htaccess and all its peek uses.and i like the way u mention the things by using technical words and phrases.

thanks a lot for this


Neil - 21.09.10 12:56 am

Hello

I can't quite get the hang of it yet, could you help me please?

How do I change requests for:

http://www.example.co.uk/gallery/lakes/hello.htm

To request:

http://www.example.co.uk/gallery/lakes/index.cgi?hello.jpg

On the server side?

Any help would be great smiley for :)

So I want to remove index.cgi? and change .jpg .jpeg .gif to .htm

Neil


Jarek - 22.09.10 5:02 pm

VERY GOOD ARTICLE!!
Keep up the good job!


Nano - 23.09.10 10:22 am

How to redirect this url:
http://www.example.com/results/search.php?keyword=keyword1+keyword2+keyword3

into

http://www.example.com/results/keyword1-keyword2-keyword3.html

Please advice me



Daniel - 25.09.10 3:30 pm

Hi everyone! I have problem with this .htaccess url rewrite

may site: http://www.example.com/user/normal/user1name/index.php <<< HIDE and cant accetp this this link.

so I wan to be soft link and hide the original link

I want it to be http://www.example.com/nor-user1name/index.php <<< show this only so how can I do that?

I have 3 folders in user
normal
vip
friend

I want like this 3 diff name and folder
http://www.example.com/nor-daniel/index.php < for http://www.example.com/user/normal/daniel/index.php

http://www.example.com/vip-tommy/index.php < for http://www.example.com/user/vip/tommy/index.php

http://www.example.com/frd-janny/index.php < for http://www.example.com/user/friend/janny/index.php


tae - 29.09.10 4:55 am

hi everybody, i have some problems.

I want to redirect the "404 page not found" to my homepage.
How can i do that?

If somebody know, help me please. Thank you.


rafal - 30.09.10 8:44 pm

Guys,

I have a problem with my url's as well.

Here is how it looks now :

http://www.toprice.ie/docking-speakers/philipsfideliodsspeakerdockforipodandiphone,499245/

What kind of rule should I apply to get hyphens between words to have it look like this:

http://www.toprice.ie/docking-speakers/philips-fidelios-speaker-dock-for-ipod-and-iphone


Any help will be appreciated.

Rafal


C-the-best - 04.10.10 2:31 pm

Ragha look, if you need to restrict access to js and css.. use something like this...


Options +FollowSymlinks
RewriteEngine on
RewriteRule (.*)\.css http://www.sex.com [NC]
RewriteRule (.*)\.js http://www.sex.com [NC]



now to use that css without other people are able to download it ...

this is index.php ....

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>
<HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<style><?php include('ttt.css'); ?></style>
</HEAD>
<BODY>
  
</BODY></HTML>



and this is ttt.css:

#ciao{
color:green;
}







the same trick with .js

<SCRIPT LANGUAGE="javascript"><?php include('script.js'); ?></SCRIPT>



with this trick the people will able to download with the browser the style/the js code but the spiders,special downloaders to grab html code(html spider,httrack website downloader , free download manager and other download sh*ts) will not ..smiley for :lol:

cheese!!! any questions : razvi_bc@yahoo.com


johnny - 07.10.10 3:51 pm

Hi, i want to redirect all links in the form off sitename/word .. to sitename/word/ . Please help me. It's really important. Thanx!


juneD - 16.10.10 8:33 am

hellow.,., can u help me to shorten my url example:
http://www.example.com/resorts/resort.php?id=24 To be like this
http://www.example.com/resort/24


IMadeItUp - 20.10.10 6:23 am

Thank you very match!
I've been on many sites learning about httaccess, and this is the best.
I will try everithing I can!!!


Sascha - 27.10.10 6:29 pm

Hi All,
thought I was pretty good in .htaccess, but learned a lot now how to shorten a URL just certain folders easily. Very ... powerfull thing.
But you should be carefull with that, or do a lot of comments in case you start having a large .htaccess, like huge lines of PHP with some MySQL, for me smiley for :D
Cheers
Sascha


Twitt - 27.10.10 8:51 pm

Good material. Thanks a lot.


Sohail - 28.10.10 11:33 am

I am looking for something different approach with htaccess

1) validate incoming url
2) if incorrect redirect the visitor to some other url...

For example:
I have a domain... http://www.myexample.com

if a visitor is coming as...
1) http://www.myexample.com/admin.php
2) http://www.myexample.com/admin

... then Redirect the visitor to...
http://www.myexample.com

ElseIf a visitor is coming as...
1) http://www.myexample.com/admin.php?token=true

... then let visitor gets the actual page

Can someone figure out on how to do with this?



Ray - 28.10.10 7:15 pm

Very nice article, absolutely worth to read.


Rinto George - 04.11.10 2:03 pm

Great article , ever I met. This helps me so much I started my new website


ssjimmyx - 13.11.10 11:44 am

I wanted to say " Thank You very much, for putting so many hours of research and testing scripts for a web sites 1st level of access system, and the 1st level of security access in a Web Sites Pages and Security files, being the .htaccess file(s)." I know from personal experience you have many many weeks and probably months of learning and experimenting on this platform level.... TY!!!!
very appreciated from a fellow web hosting and designer tech... :-) smiley for :lol:smiley for :D


Marks - 17.11.10 5:28 pm

1st of all ty for the research and the sharing.

What's happening to me is that when I click on the "customized" link, the new page opens correctly with the "different" url, with the right contents, BUT with no images nor CSS.

This happens when in .htaccess I don't specify the full address in the 2nd part, as in:

RewriteRule ^this/that(someParameter)$ index.php?go=$1

While if I specify the full address, I get the right page with images and CSS BUT no customized url

RewriteRule ^this/that(someParameter)$ http://www.mysite.com/index.php?go=$1

Any help?



Marks - 17.11.10 8:32 pm

...about my above post, I've found that the problems is in relative links. The solution is to use RewriteRule to consider them in the right way.

For example:

RewriteRule ^.+(/img/.+)$ $1

will look for anything resembling "whatever/img/whatever" and serve it as "/img/whatever".

Hope it helps.


Mohit - 19.11.10 6:42 pm

Hey!.. I was trying to create virtual subdomain on my website.
I was able to redirect Url's like-
http://xyz.com/abc-123 to abc.xyz.com/123
Now if I want to point http://abc.xyz.com to a master page master.php, I get 404 when I try to access http://abc.xyz.com
How can I do this type of redirection using .htaccess


Mike Shafer - 20.11.10 4:04 pm

Excellent bit of coverage on a powerful but often confusing aspect of using Apache server.
Thanks for the work that went into this!

Cheers!


bobross - 22.11.10 5:44 am

Great site. I am having a problem with the hot linking to site code in https. The code works perfectly in http but when I go to my secure https pages, all of the images on those pages are using my "Keep out" hotlink gif. Is there a way that I can add an exception or work around? Thanks.




Chuck - 24.11.10 8:16 am

Thanks for the writeup, I have never read any htaccess info that has made this much sense..


mynsac - 26.11.10 7:25 am

Great Article! Very useful to me.


Randy - 27.11.10 6:13 pm

Just wanted to thank you for the great article. For some reason all the other mod_rewrite articles didn't make sense, yours made it clear for me.

Thank you


andoy - 28.11.10 4:35 am

Thank you for this wonderful article..

This article is very helpful to all beginners like me.


John - 28.11.10 7:18 pm

don't know how much time you spent to write this article but it's fantastic, thanks smiley for :ken:


Ryan - 01.12.10 4:05 am

Thanks for the great post.

I'm having issues redirecting the following to an external site. Is this even possible?

I want this URL:
http://www.example.com/test/index.php?_cn=Product%20Name&_pn=77-363-2173&_rb=75&_ec=M1

To Go To:
http://www.Newexample.com/test/index.php?M1

Thanks for any help!

Yes, it's possible. ;o)



sylehc - 17.12.10 9:42 am

First of all thank you for sharing the information.
I've modified my .htaccess to restrict the access to one local host.
ReWriteEngine On
Options +FollowSymlinks
RewriteCond %{REMOTE_ADDR} !192.168.1.10
ReWriteRule ^(.*)$ http://www.redirect.com [L]

But it works only partially: the host can access the site root (www.mysite.com) while the folders are not accessible (www.mysite.com/about/).
Not Found
The requested URL /about/ was not found on this server.


Do you know how to modify the rules to solve the problem?
Thank you in advance.

You don't need rewriting for this (which is like using a TE-72 to crack a nut). Check out part 1 of this article; believe it or not, the original purpose of .htaccess was to control access. ;o)



sylehc - 17.12.10 10:19 pm

Thank you for your quick reply smiley for :)
Sorry but I've found you site using google and now I'm starting to read many of your articles. WOW you have done an incredible work!!!

I modified the .htaccess in this way:
order deny,allow
deny from all
allow from 192.168.1.10

ErrorDocument 403 http://www.redirect.com


Still the same problem. When I try to access some folder (www.mysite.com/folder/) I receive the message "Not Found".
Well, the folders are logical. They are generated by Wordpress as permalinks but I don't known why it happens because the host is allowed.




sylehc - 20.12.10 10:20 am

Found the problem. This part of the file was missing.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Second version of .htaccess works like a charm smiley for :)
Thank you again.


Amar Yash - 21.12.10 11:50 am

the code of .htaccess is very good and vary useful. Thanks for the code.


laotse90 - 21.12.10 7:25 pm

hi
indeed complex... though a great complication

I missed however some information how to
exclude a particular directory/URL-path from rewriting
while all other directories should be rewritten.

help is greatly appreciated !!! thanks

Maybe read more slowly next time. ;o)



Hong Duc - 27.12.10 5:12 pm

this is such a great article about .htaccess configurations and regular expression examples, I've read many other sites and I find this one is the best !!! I'll bookmark this one to read it again and again


Remi - 29.12.10 3:20 am

Is it possible to redirect a file such as

http://www.OLDexample.com/index.php/products-mainmenu-64?page=shop.browse&category_id=26

to

http://www.newexample.com/newproducturl.html

Yes, it's possible.


I've got multiple (49) random urls to forward from/to

What would be the best way to work this?

Thanks for your help, this post has been helpful already!

You don't give enough information for me to determine the best way. ;o)



Ben C - 01.01.11 8:09 pm

Hi, you mention being paid, which is fine by me! I need some help rewriting URLs such as:

/?mod=11&p=215

Can you email me before I try someone else?

How about I just ignore you, instead. ;o)



Gaurav Kumar Arya - 03.01.11 7:42 pm

Hi,

really this is a very good tutorial for .htacess urlrewriting.thanks for this great tutorial.


jeppy - 06.01.11 1:04 pm

Hi,
Thanks for this page very well explained and completed. smiley for :D

Let me ask a question.
Is there any global method to deny access to some websites which permit to convert web pages into pdf files.

There are so many websites that it seems fastidious to enter each rewrite_cond
( and it's always possible to forget one or many ) smiley for :eek:

There is no such method. ;o)



Joe - 09.01.11 1:34 pm

Hi thanks for all the info but I am having issues with a redirect I am working on and wondered if you could advise pls pls with sugar?

What I want to do is have the .htaccess file get the URL (HTTP_HOST/SERVER_ADDR) and use that URL for redirections etc.

I have 5 sites that are all mapped to the same html_directory as they all use the same layout and some of the data is shared between them.

What I need is something that will allow the .htaccess file to do redirects to the URL in the address bar and if errors happen they are redirected to the URL shown and not the manual URL written in the .htaccess file.

Example One of two

Manually written

ErrorDocument 404 (http://)www.EXAMPLEURL.com/faq/unavailable
ErrorDocument 500 (http://)www.EXAMPLEURL.com/faq/unavailable
ErrorDocument 403 (http://)www.EXAMPLEURL.com/faq/unavailable


RewriteEngine on
RewriteCond %{HTTP_HOST} ^EXAMPLEURL.com$ [NC]
RewriteRule ^(.*)$ http://EXAMPLEURL.com/$1 [R=301,L]

Example Two

ErrorDocument 404 http://((GET HTTP_HOST))/faq/unavailable
ErrorDocument 500 http://((GET HTTP_HOST))/faq/unavailable
ErrorDocument 403 http://((GET HTTP_HOST))/faq/unavailable


RewriteEngine on
RewriteCond %{HTTP_HOST} ^HTTP_HOST$ [NC]
RewriteRule ^(.*)$ http://HTTP_HOST/$1 [R=301,L]



Cheers,
Joe smiley for :D


Htaccess Redirect - 17.01.11 1:16 am

It may seem like a super stupid bit of information, but a lot of tutorials forget to mention that htaccess files are set to "invisible" by default. In my own meanderings into needing to do an htaccess redirect quickly - on a deadline - support tickets submitted to two different hosting providers never came back with this important piece of information. I received lots of tips about what code to add to the file and how to set up an http: to www. redirect, but nobody told me how to access the darned file! It's amazing how small details often escape the "super geeks" and us "non geeks" are left feeling frustrated and, um, stupid!


Paul - 17.01.11 10:06 am

Hi!

I'm trying to make an bilingual website with MODx, and the problem is that TinyMCE outputs the links for the assets like this: http://localhost/modx/(language)/assets/etc....

How can I rewrite the links to be output as: http://modx/assets/etc... without the (language), because I want to have "assets" accessible from the root.

Your help would be greatly appreciated!

Paul

Simply capture the language part, and remove it. Otherwise get inside MODx's php and change it. ;o)



BEN - 17.01.11 11:19 pm

i need a url rewrite to do this.

if $2 has less than 5 characters, using script1 to rewrite, if more than 4 characters, using script2 to rewrite, and the output of the rewrite are looks exactly the same.

RewriteRule ^( <^/>+)/( <^/>+)\.htm$ script1.php?p=$1&q=$2

RewriteRule ^( <^/>+)/( <^/>+)\.htm$ script2.php?p=$1&q=$2

i am new to this. thank you for your help. please email too.



Cata - 18.01.11 11:37 am

Hi,

I want to rewrite a URL like this
http://myexample.com/group.php?a=x2424&n=mike to
http://myexample.com/group.php without parameters ,or better if it possible
http://myexample.com/group.htm/mike

In php script i use _GET to read those 2 parameters and of course this must work for rewrited URL

Can you help me?


Ashu - 21.01.11 6:35 am

Thanks for the great post.
I have learned so many things about htaccess. You done really a fantastic job by writing this useful post.

Thanks,
Ashusmiley for :)


Zri - 28.01.11 12:47 am

Thank you very very very much for this site, made my life a lot easier!


Michael - 30.01.11 8:51 am

When I use:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ /public/wu.login [NC]

All my example.com/____.htm links go to the /public/wu.login page

I want to redirect something like example.com/33

When I use:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/([0-9][0-9])/$ /public/wu.login [NC]

It just says 404 not found...
Any ideas?

Thank you in advance -

Extra slashes can be a problem, yes. ;o)



bwald - 01.02.11 2:46 am

I've been doing a lot of searching and I came across another site (http://datingking.net) that seems to be copying your content. The stylesheet is slightly different, and there's a blinking "(in progress)" near the top, but other than that pretty much exactly the same.

This probably seems like a good candidate for spamming. I found it by doing a Google search on "htaccess request_filename -d", 2nd result. It seems like a really odd domain name for the content, so if you don't find it maybe it's just something weird...?

Just thought you might want to know.

There are a lot of copies out there and yes, even entire domains devoted to (old) copies of my content! Google knows where the originals are, though. Thanks for caring! ;o)



Michael - 02.02.11 5:46 am

I got it to work!

I'm using the pre-packaged Apache that comes with Oracle.

I changed my .htaccess file to:

...
RewriteRule ^(.*\.htm)$ /public/wu.login?id=$1 [NC]


And the $1 showed up as /domains/fst/test.htm ... which is exactly where it was, because my files aren't stored directly in the /htdocs/ "root". Each of my virtual hosts has it's own root further down the directory tree.

If you're getting nothing but 404 errors, you might be able to use the code above to diagnose where your files are, and make this work!


IKRA - 07.02.11 2:26 pm

Hi,

Thanks for post. It's very nice ans usefull but I can't solve my problem.

Here is my situation:

In root folder I have 2 files and 1 folder.

File 1 - ser.php
File 2 - serv.php
Folder - TEST



1: I want, if in url is: http://localhost/something

Rewrite to http://localhost/ser.php?action=something


2: But if in url is: http://localhost/something/somethingelse

Rewrite to http://localhost/serv.php?action=something&subaction=somethingelse


3: This is most difficult for me:

If I have: http://localhost/TEST

NO REWRITE -> go to TEST folder

PLS, Help me


I see no .htaccess code to help you with. ;o)



anadolugezgini - 09.02.11 12:09 am

Hey there smiley for :)

Thanks for great tips..i've been crawling for the correct htaccess file and found it here and yes its working charm smiley for ;)


kartik maniyar - 09.02.11 5:17 am

About Rewriting URL in .htaccess file.

I want to rewrite the module. Your Post is very good but It is not help me so i have to ask u one Question about the .htacces file rewriting URL.

At this time my URL is look like this "http://www.example.com/index.php/catalogsearch/result/?q=test" but

I want to write url like this "http://www.example.com/by-design/test".

because i have one module is "Shop by Design". This module is already uploaded in another Website and in that website it is work properly but in this means above mentioned site.(i have changed the name of site).

if u have any idea then tell me please it is very important for me please help me.

Here's an idea; try writing some .htaccess code, along the lines described in the above article. Have fun! ;o)



Bluesp - 09.02.11 10:44 am

I am trying to alter a part of an url. The url I am trying to alter is this one:

index.php?case=subcategories&nc=33&nb=(any number here)

to this one:

index.php?case=subcategories&nc=204&nb=(matching number here)

Can't seem to figure it out so far.

Regards

It could be your lack of actual .htaccess code. ;o)



PHP Developers - 12.02.11 11:02 am

Hey thank you so much for this tutorial... I loved it!




Kyle - 14.02.11 5:00 pm

I am trying to force urls to lowercase. for example I need http://www.example.com/ThiSpAgE.html to be directed to http://www.example.com/thispage.html. I am using godaddy on linux and I have tried this code in my .htaccess file with and without the Virtual host tags(found somewhere in godaddy's help that I should try it:
<VirtualHost>
RewriteMap lowercase int:tolower
</VirtualHost>
Options +FollowSymlinks
RewriteEngine On
RewriteCond $1 [A-Z]
RewriteRule ^/(.*)$ http://www.example.com/${lowercase:$1} [R=301,L]

I keep getting a 500 Internal Server Error

Thank you!

GoDaddy don't allow .htaccess. Also, their CEO, Bob Parasite, thinks killing Elephants is a Fun Thing To Do; so much it swells him like a fat balloon. Try another host, and domain registrar soon, please. ;o)




Alan Lawson - 19.02.11 4:56 pm

Thank you. Your notes on lose the www were a real time saver and worked first time.


keepout - 02.03.11 12:36 pm

i would like to restrict one page on my website for viewing by one ip address. for instance i would like
http://www.mywebsite.com/keepout
to only be available to one ip address i.e. 902.313.14.1
but i would like
http://www.mywebsite.com/keepout/index.php?question=1234
available to viewer. i would really like to just lock the root folder keepout to only ip address and anything else leave it open
is there anyway to do that. thanks

Yes, see part 1 of this article. ;o)



techknow - 03.03.11 6:01 am

I want to set redirection of any page of a website to another website.

like if someone access http://www.domainone.com/anypage it shoule be redirected to http://www.domaintwo.com

Please help....


sam - 03.03.11 1:56 pm

Am currently migrating my site content from an old way of forming URLs to the new order of things, mainly for SEO purposes. The old URLs looked something like any of the following:

http://www.mydomain.co.ke/health/content.php?id=2000030206 OR
http://www.mydomain.co.ke/news/content.php?id=2000030206 OR
http://www.mydomain.co.ke/content.php?id=2000030206

I dont want to lose track of my previous content that google has already indexed using the old ULR formation as above. Instead, I would like these URLs to be redirected to the new URL formations as below (based on the above URLs content):

http://www.mydomain.co.ke/news/article/2000030206/the_nice-item-title-here

When a visitor hits on the URLs from google that are in the old format, they are automatically redirected to the new URL format, pointing to the same content.

I'd appreciate anyone's help on this. Rgds.

So long as you use [R=301], you will be just fine. ;o)





g1smd - 08.03.11 10:05 pm

Heh, great tutorial.

I have read a LOT of tutorials over the years and I knew only a few paragraphs in, that it was going to be a good one.

You cleverly avoided the common errors most people make when they try to explain this stuff.

Two comments on syntax:
- don't forget to escape literal periods in patterns,
- every RewriteRule needs the (L) flag unless you know of a really good reason to omit it (such as when the (F) or (G) flag are used - it isn't needed then).

The stream of "How do I do this?" questions will never dry up. This isn't simple stuff!

Many thanks! *Arrgh*! That [L] again! It's not required for every rule (or for teaching general rewriting) but it is darned important and needs more explanation. I have a stash, draft copies of sections for this page along the lines of "To [L] and back.." where I explain in clear terms, just how to get that flag to work the way you expect. None of them look like they make sense to anyone but me, yet. Thanks for another kick. I'll get on it ASAP. And.. Unescaped literal periods?? Aw shite! smiley for :lol: ;o)



Arif - 09.03.11 8:41 pm

Hello,

http://www.example.com/index.php/checkout/onepage/success/ is my current URL and I want to make this URL as http://www.example.com/hello

I want to use this in Magento.

Thanks!

Wonderful news! ;o)



Peter J - 12.03.11 10:09 am

Is there any way that you can use the .htaccess file to redirect image (*.jpg) downloads to a certain image-file as well as if someone links to this image from an external page it should redirect the link to the same image.

Yes. ;o)


Hope to get som help
//Peter


Hendrik - 23.03.11 11:59 am

Thank you for this site! I have to admit that rewriting is still like rocket science to me...

I used one of your examples and changed it a little bit. Maybe this is helpful for others as well.

My goal was to change
http://mysite.com
into
http://www.mysite.com
(exactly the other way round as in your example).

My solution I found after some trial and error was this:
RewriteCond %{HTTP_HOST} !^www\.(.*) [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NC,L]

I hope this is the correct way to do it. It seems to work though. If there is a better way, please comment.

Regards,
Hendrik

The (.*) in the RewriteCond is redundant (unless you want to replace %{HTTP_HOST} with what you captured), but aside from that, it looks functional enough. Just, WHY? smiley for :roll: ;o)



Patricio - 24.03.11 1:40 pm

Hi, i have a problem with modrewrite

if i use http://myexample.com/images/1 does not work but if i use this http://myexample.com/images/?1 works fine


RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?id=$1 [L]


Thanks.

It's probably because that's precisely what your rule tells mod_rewrite to do. Only one "/" is allowed in the URI, for a start, and it must be at the end of the URI. Change those things. Remember, the "?1" part of your second example URI, is the query string. ;o)



PHP Developer mavris - 25.03.11 4:04 pm

Thank you very much for your tutorial.
I have read many others but yours made me understand
most things about .htaccess files and i added S.E.O.
to my sites with very good results.

Thanks for giving very valuable information for free.
You helped me very much and i think lots of other people.

Continue the good work
Cheers smiley for :lol:
G.M.


Prosenjeet - 27.03.11 5:08 pm

Great tutorial!


Mohammad - 29.03.11 6:41 pm

Hello
at first I have to give you a big thanks for sharing these tricks!
I have two questions:
1- What is the language of these htaccess codes?!!! they are really confusing!smiley for :roll:

it is ".htaccess code", learn to love it.


2-I have a download website and I put the links in the main site and when the user clicks on the link, he redirects to another site which is just for downloading. So user goes to "www.mysite.com" then clicks on a link like this one: "download.mysite.com/cat/a" and after going to this site, he clicks on download links which are like this one: "http://download.mysite.com/?sth_dl=279"
I want to set a limitation that have these factors:

1- If a user typed download page address ("download.mysite.com/cat/a"), he sees a forbidden page.
2-If a user clicked download page link ("download.mysite.com/cat/a") on other websites, he sees a forbidden page. So the only way the download site could be loaded is that the user be referred from the main site. (www.mysite.com)
3- Just like number 1 and 2 but this time for the file link.("http://download.mysite.com/?sth_dl=279") the limitations must be.

And it may be important that I mention these sites (main site and download site) are on different hosts!
-----
I used these codes but the server load went high! (I think a looping was happend
!)smiley for :ehh:

#redirect users to www.mysite.com.
ErrorDocument 403 http://www.mysite.com
ErrorDocument 404 http://www.mysite.com
catch the thief!
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} !^https?://.*mysite.com/ [NC]
RewriteCond %{HTTP_REFERER} !^https?://.*mysite.net/ [NC]
RewriteCond %{HTTP_REFERER} !^https?://.*mysite.org/ [NC]
RewriteRule .*$ - [NC,F]


Why are you sending 404 and 403 errors to non-error documents? That's nuts.
Use something like this..
RewriteCond %{HTTP_REFERER} somedomain\. [NC,OR]
RewriteCond %{HTTP_REFERER} someotherdomain\. [NC]
RewriteRule . abuse.txt [L]
Note use of [OR]


-------------
I'll be really grateful if you help me solve this problem!

No you won't! ;o)


with best regards

Mohammad




Brandon - 01.04.11 10:28 pm

I love people like you that put together excellent tutorials. I owe you big time.

This gave me a great starting point for learning how to configure a .htaccess file.


stranger - 02.04.11 11:09 am

Hi,

Thanks for so great guid...But unfortunately I'm newbie about htaccess.. So I couldn't figure out how to make solution for my case.. Here it is:

I had moved all pages from http://www.mysite.com/folder to http://www.mysite.com
Now I can't just redirect all pages from folder to direct homedir
Any body suggest please, how can I make it..

mod_rewrite is what you need. See above. ;o)



Diva - 08.04.11 10:14 am

I must say that this is a wonderful tutorial. It help me a great deal with what I thought would be an overly complicated problem. Thank you for this lesson. I was wondering how I can create folders within my main folder that go from http://www.myexample.com/somfolder to http://somefolder.myexample.com? Thank you for your help. Diva

See here, it's the same principle. Remember, you need to have this setup in the DNS, which I assume you can get to from your hosting panel, creating subdomains and such.. ;o)



Zeko - 11.04.11 12:19 am

Hye. Is it possible to use

"deny for all" in combination with some "ErrorDocument 401 /index.html"

to make it go to index page? And how would you do it? Thank you!

See here ;o)



luca - 11.04.11 2:41 pm

Hi! There is my problem:
i have a forum in mysite/forum
i would that request for example mysite/forum/index.php?par1=1&par2=2&ecc....
become mysite/index.php?par1=1&par2=2&ecc....

There is a way to do with mod_rewrite?
i want only to copy name and value of parameters and "append" to another page...

Sorry for my bad english smiley for ;)


[ SOLVED ]
I've solved in this way (this .htaccess is in www.mysite.it/forum/):
RewriteEngine On
RewriteCond %{QUERY_STRING} (.+)
RewriteRule ^index.php(.*) http://www.mysite.it/index.php?myparameter=something$1 [QSA]


Now: http://www.mysite.it/forum/index.php?par1=1&par2=2

become: http://www.mysite.it/index.php?myparameter=something&par1=1&par2=2

Thank you


Toga - 12.04.11 11:20 am

Hello!

At first by following your guide, I got to a part where I managed to eliminate the variables in my url, like so:
RewriteEngine on
RewriteRule ^idea/([a-z]+) /include/idea.php?id=$1 [NC]


But my intention in the end, was to be able to drop the "folder" in the front too, in this case mysite.com/idea/

Couldn't figure this out by messing about with the above examples, so I went for a search through the Internet.

RewriteEngine on
RewriteBase /
RewriteRule ^([0-9a-z]+)/?$ /include/idea.php?i=$1 [nc]


This is what I found and it works fine, but I was wondering if you could shed some light on what's happening there.

I thought the "+" part on the left was replaced by the "$" stuff on the right, but now there is a "?$" on the left part as well. Also I'm guessing the base thing there sets the root? Seems to work fine without it, but the example code had it in, so not sure if I should get rid of it, even if it works.

Best Regards
Toga

The "$" has a different meaning in each context. On the left it's regular Regexp, on the right, mod_rewrite trickery. As for RewriteBase; a can of worms I guess one day I'll open fully here; I'd leave it out unless you know you need it. ;o)



Chris - 16.04.11 1:30 am

Excellent work!!
I want to know how to do this:
http://localhost/variable #variable maybe contained / character
if (request is from file in my server*) then
show http://localhost/variable elseif (variable=admincp|variable=admicp/)
show me http://localhost/admincp elseif (variable!="")
show me http://localhost/php/index.php?key=something else
show me http://localhost/php/index.php?key=index

In any condition the browser url bar doesn't change.

*Explain first if: index.php with <img src="../img/image.png"/>will return the image but if i put http://localhost/img/image.png in browser adress bar will return http://localhost/php/index.php?key=img/image.png --->This file will catch and show me an 404 error page
Is something like this is achievable??
It's SEO friendly (i believe)!



Mark Iliff - 20.04.11 2:43 am

Huge thanks for this. I've ploughed through loads of other stuff on .htaccess rewriting and none is as lucid as this.

Now I've begun to master your tricks, I might understand the lengthier documentation...

µ


Mark - 21.04.11 3:21 pm

Big thank you!
Been looking for something that explains 'the why' and not just 'the how' for htaccess (which is all new to me)
Entertainingly written too.

Will drop a little something in the box next payday

Many thanks!!!
M


Crazy Uncle - 22.04.11 6:34 am

Thanks for the tutorial. allowed me to reduce the number of files with the exact same content (php / smarty) yet the url looks like the file is there.


xXx - 29.04.11 9:24 am

Hi, thanks so much.
I really learned a lot from your site.
Recently, I meet a problem which I have no idea to solve it.
Please help me, thanks.

The code has been shown below is what I am using.
RewriteEngine on
RewriteCond $1 !^(index\.php|images\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]


I uploaded this part of code into my server.
It looked fine, but I noticed I couldn't access to other folders which are in the same server with images folder.

And there are a lot of folders in this server, I think it's stupid to type all folder's name follow by images.
So, is there any way which can let me access all the folders?

If I knew what you were trying to achieve, I would guess it was easier without the back-references. ;o)



Administer - 02.05.11 11:43 am

Hi
can you help me? :(

i want Rewrite this address:

http://www.site.com/forum/link

to

http://www.site.com/link

link= showthred.php, member.php, and etc

please help me
Tnx


seb - 03.05.11 1:34 pm

I need help. I want rewrite this adress:

www.domain.tt/012345
to
www.domain.tt/shortlink.php?id=012345

I have three additional redirect commands and an ErrorDocument command. I also would like to know who I position the RewriteRule command then have to write?

My idea is now as follows:

ErrorDocument ...
Redirect ...
Redirect ...
Redirect ...

Options + FollowSymLinks
RewriteEngine on

RewriteRule ^([:digit:]{6})$ shortlink.php?id=$1 [NC]

That'll work, but for the rewrite something like this..
RewriteRule ^([0-9]{6})$ shortlink.php?id=$1 [NC]
;o)



jokar - 05.05.11 2:23 pm

Thank you for this wonderful lesson

But I want to. htaccess

To prevent certain words from appearing on the site

Example

sexy to *******
And
Pussy to ******

Thus

This is best handled inside your site code (i.e.php CMS, or whatever). ;o)



kiro - 15.05.11 7:44 pm

how do i hide port 8080 from url.

example.
www.example.com:8080/hello to www.example.com/hello
www.example.com:8080/hello?p1=1 to www.example.com/hello?p1=1

I don't know of a way to do that with purely mod_rewrite. Perhaps with mod_proxy and mod_rewrite's [p] flag... ;o)



AndyiBM - 19.05.11 11:45 am

You beauty! Fixed my "multiple domains in one root" issue brilliantly.smiley for :D

Me (and loads of others) are so grateful for resources like yours. Thank you.


Alfie Punnoose - 20.05.11 7:08 am

Hi, I think the line
e.g. (.+)\.html? matches foo.htm and foo.html
must be
e.g. (.+)\.htm? matches foo.htm and foo.html

--------------^

isn't it?

Nope. ;o)



Salman - 27.05.11 12:39 pm

I'll never understand why the [L] flag does not work the way it is supposed to. Every now an then I end up sending the rewrite module or the browser into an infinite loop. Can you shed some light on how the [L] flag works (and why it does not work the way I expect). Or more simply, why does not this work:

RewriteRule ^foo.php$ /foo.bar [R,L]
RewriteRule ^foo.bar$ /foo.php [L]


It doesn't work (as you expect) because the [L] flag only applies to THIS RUN through the directives. After processing is complete, Apache must check the .htaccess rules against the new request.. Either way, it matches.

Unless you are working with httpd.conf, it's smart to be specific with your [L]'s, using RewriteCond statements to match against Apache's THE_REQUEST variable, which contains the browser's original request.

Here's a fun solution for you..
RewriteCond %{THE_REQUEST} !/foo.bar
RewriteRule ^foo.php$ /foo.bar [R,L]

RewriteCond %{THE_REQUEST} !/foo.php
RewriteRule ^foo.bar$ /foo.php [L]


THE_REQUEST is a "special" Apache variable, something of a phantom in testing, yet generally behaving as expected when in place and working. Perhaps I should say, something of a photon, then...

;o)



Eric - 03.06.11 1:34 am

Cannot seem to properly configure my 301 redirect.

I have a lot of incoming links from file extensions ending in .php. I have recently re-created my site and it no longer has a file extention.

For example went from example.com/selling.php to example.com/selling

All of my web pages are created through a CMS so I'm not sure if that is why everything i've tried so far doesnt seem to work or not (essentially all my pages are categories in a database.)

Tried a few of your example and didnt seem to work for me, any ideas?

Thanks for the help!

You did well enough to describe your situation, but "Cannot seem to properly configure my 301 redirect" does not at all describe your problem. ;o)



Collin - 05.06.11 6:17 am

Cool post. Helped me out a lot. Thanks! smiley for :)


Mondor - 18.06.11 6:29 pm

I don`t know if i am in right place but i put this question.
i had in my httacces this rule

RewriteRule ^detalii/.*/([0-9]+)/?$ index.php?module=products&action=details&prod_id=$1 [L,NC]


and need to keep it and redirect to new rule as
RewriteRule ^detalii/([0-9]+)/.*/?$ index.php?module=products&action=details&prod_id=$1 [L,NC]



for google purpose do not need for reindex.
Need old format to be redirect to new format.

Thanks in advance


Boggy - 19.06.11 6:37 am

I have a multi-user site and I wrote my own script for creating subdomains via cPanel at registration time. Everything is nice and dandy but now I need to implement a rewrite rule in order to display the right web address for my members. What I mean is this - member1 will have a folder created with the same name and also a subdomain allocated (called member1 too). When typing member1.mysite.com in the address bar the redirect will take the user to the location of "member1" folder inside my site - something like mysite.com/somefolder/member1. What kind of rewrite rule should I implement in order to keep the address bar displaying member1.mysite.com? Remember I have numerous subdomains so I can not create a rewrite rule for every single one of them. I need a general one to deal with a generic subdomain. My host does not allow for wild card subdomains so that is why I chose to dynamically create them through cPanel, but i have access to htaccess file. Any help will be much appreciated.

Thank you,
Boggy


Clearercape - 22.06.11 8:55 am

Hi
Great tutorial. Really helped. I need help with this one. I cannot get the rewrite to work on this string:

http://mydomain.co.uk/index.php?option=com_content&view=article&id=27&Itemid=55

to:

http://mydomain.co.uk/friendly_URL


What am I doing wrong?



Thanks


Manoj Thomas - 22.06.11 5:52 pm

Useful info, thanks!


Shaun Janssens - 07.07.11 10:40 am

Hello,

I have a problem with a blog and a URL shortener in the same folder.
My short url make them URL's like: sh4un.be/1 or sh4un.be/ 2 and so on.
But that conflict with mine. Htaccess.

For my blog:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond% {REQUEST_FILENAME}!-F
RewriteCond% {REQUEST_FILENAME}!-D
RewriteRule ^. + $ Index.php [L]
</ IfModule>

For my url shortener:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond% {REQUEST_FILENAME}!-D
RewriteCond% {REQUEST_FILENAME}!-F
RewriteRule (.*) index.php? Token = $ 1 [QSA, L]
</ IfModule>

As you can see the rewrites. Htaccess each url. Can it be adapted so only as a URL from a maximum of 4 digits long and is then rewrite but which may be carried out?

Thanks,
Shaun


shravan - 08.07.11 10:31 pm

i read only half of the second part.
i want thank you for this article. this is rally helpful article.
i learnt little regular expression also from this article.
overall wonderful article. thanks a lot.


Rob - 11.07.11 4:44 pm

Cor,

Very interesting article and I think I get most of it but not enough to solve a problem I am having.
I tried to redirect some files with the htaccess but so far did not succeed.
I want to redirect all files in my main folder like article_articlename.html
to articles/articlename.php
so basically I am trying to redirect all files starting with "article_" to the folder "articles"

This is what I tried:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^article_([a-z]+).htm$ /articles/$1.php [R,NC]

If I don't want the file extension to be specific (i.e. could be anything .html, .htm, .php) would it become easier?
I probably have to redirect to http://mysite.com/articles/ to make it more fool proof right?

Anyhow, I hope you can help me with this

Cheers... Rob

I see no reason why it wouldn't work, assuming..

* rewrites are working on your system (this is not redirection!)

* The requests are actually for http://yourexample.com/article_something.htm, which is to say, files in the root of the site with an .htm extension.

* The target files are in a directory in the root named "articles", and the corresponding .htm file namess now have .php extensions.

If this is all true, and it's not working, exactly what is happening? What errors are you getting? Where is Apache sending the browser? etc.

Making the extension a wildcard doesn't make anything easier, it simply makes the rule more loose. If that's what you need, do it. But I'm guessing the htm > php conversion is enough.

;o)



Ray - 12.07.11 6:58 am

Hey thanks for this, I've been scouring the web for this info, but all I've found are snippets here and there. Some of it out of date too. I was considering writing about it myself, but I'm just an amateur. No way could I have put together a comprehensive and entertaining article as interesting as yours.

Cheers! smiley for :lol:


yaniv - 14.07.11 4:48 am

Wow, amazing guide smiley for :) and I went over many guides for mod_rewrite smiley for :)
big thanks!


Rob - 14.07.11 11:05 pm

Cor,

Thanks for the comment on my question before.
Where I went wrong was that I tried to redirect(oops, rewrite) .htm files and the files are all .html, so by adding a simple l after .htm it worked.
It is all in the details...

Cheers...Rob


Rob - 15.07.11 3:31 am

Forgot to ask something else,

Can I actually put a second rule in?
I need to rewrite all files starting with newsletter_ to /news/*.php as well.
Can I just put another RewriteRule under the first one?

Thanks a bunch.

Cheers...Rob

Sure, some people have thousands. ;o)



Ali - 20.07.11 11:57 am

Hi,
i need to rewrite a URL in .htaccess file ,it is something like this
http://www.example.com/Services/Foam%20roofing/Foam%20roofing.html,
the problem with this url its too long ,and also space character is also %20 there in the URL,
can you please help me how to shorten the url and also to remove %20,as well,like how i define rule in .htaccess file ,what expression should
i write to first match this type of url and how to rewrite new url,
i will be thankful to you.

The normal pattern matching method will work fine. As for removing the space, from where? ;o)



nik - 25.07.11 9:44 am

httpd.conf  - linuxsuse 11 and apache2


ProxyPass /X1/ http://192.0.2.0:88/apex/f?p=220:1
ProxyPassReverse /X1/ http://192.0.2.0:88/apex/f?p=220:1
RewriteRule ^/X1$ /X1/ [R]



When I write in my computer URL I see that apex/f%3Fp=220:1 is not allowed.  - because the url is not OK. 
Apache make ? to %3F


How to repair that 

I've added AddDefaultCharset utf-8 in httpd.conf. but it doesn't work or I don't know how to use it.


There's no obvious reason why it would do that. I use the [p] flag, instead of ProxyPass, wherever possible. Then, if required, check out the [NE] flag, also. ;o)



WHATEVER - 26.07.11 10:47 am

Capisce, capisci, or capito. not capiche.

thx for the article btw

It's a joke, as in a spaghetti western. If you were to hover your mouse over the word, you would have seen the real version. Thanks for the laugh, btw. ;o)



Sajin - 26.07.11 4:29 pm

Can anyone help me to write a rewrite rule?

http://test.foodem.com/view/vendor/OTg.html?name=sajin
to
http://test.foodem.com/sajin

I already did, in the main article. ;o)



Sknz - 27.07.11 1:46 am

Hi! your site is better!

Well, i can use <Files> or <FilesMatch> to Deny All "except" file1, file2, file3 and file4 ?

For example, i want deny access to all files like> php|htm|html|shtml|xml but i need free access to files> 404.html, sys.php and work.htm

How i can do this? if possible..
tnkz

Follow the examples in the main article. ;o)



edward - 11.08.11 7:38 am

Do you know of a rewrite that works if only some variables are present?
Using:
RewriteRule ^location/([^/]+)/([^/]+)/([^/]+)? /store.php?state=$1&category=$2&rating=$3 [QSA]

example.com/store.php?state=ohio&category=toys&rating=5     will yield: example.com/location/ohio/toys/5       works fine

example.com/store.php?state=ohio&rating=5     will yield: example.com/location/ohio//5              doesn't work!  404 Error


any suggestions?

Use RewriteCond to check for it. ;o)



Anand - 26.08.11 10:19 am

Hi,
I got a queriy and wanted your help in this regard.
When people access my domain it is redirected to http://www.myexample.com/en/index.php file using php code.I used
RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 


to redirect people from non www to www but its not working. Still user can access http://myexample.com/en/index.php

Rewriting is NOT redirecting. The latter uses mod_alias, not mod_rewrite. Secondly, OF COURSE they can, unless you use some mechanism to prevent it. ;o)



Idham - 01.09.11 9:15 pm

Thanks a lot, your tutorial very helpful smiley for :)




Niko - 02.09.11 10:33 am

Hi there!

Great tutorial, well documented and with actual examples... thanks man.

I'm stuck with that niceexample.com redirect through 301 to my subdomain.mainexample.com where stuff is hosted.
Is there any way to maintain niceexample.com in the URL browser box, putting some rewrite magic inside the subdomain.mainexample.com folder ?

Can I do that with rewrite mode ?

Thanks and keep up the good work

Of course you mean REWRITE with R=301, so NO, the 301 code specifically instructs the browser to go to the new location, aka "External" rewrite. instead, consider an internal rewrite or use [p] ProxyPass to handle the request. ;o)



sayali - 06.09.11 10:29 am

Hello,

Its very nice tutorial. Thanks for sharing....

Also need some help for creating URL Like,
http://username.mysitename.com

currently I am having url like,
http://mysitename.com/pagename.php?username=username


Thanks.

Very nice! ;o)



zeshan Khattak - 14.09.11 6:54 pm

Great Article. Very much helpful.

I still have a bit problem with my url...
This is my original URL: http://www.dlcorner.net/article_detail.php?article_id=155

I want to convert it into the following URL.
http://www.dlcorner.net/categoryname/articleid(155)/articletitle.html

I am searching for help for one week but cant find solution.
I will be very thankful to you if helped.
Thanks in advance.

regards,

Read the article again, but more slowly, is my recommendation. ;o)



Olivier - 17.09.11 1:07 pm

I would like to block a browser language and I have added these line in my htaccess but it doesn't seem to work:

RewriteCond %{HTTP:Accept-Language} ^zh [NC]
RewriteRule ^.* - [F,L]

What would be the correct synthax?
Thank's in advance.

Content negotiation doesn't work like that; it's inclusive. If you are trying to deny access to all Chinese speaking peoples, why not simply deny all their IP blocks? Better yet, don't bother. ;o)



Maggie - 17.09.11 11:25 pm

Thank you very much for this article - I've been bashing my head against a ton of mod_rewrite docs (official and un-) and this is the clearest, most comprehensible I've run across. The conversational tone really helps.


Neville - 19.09.11 9:10 pm

I have renamed a few of my webpages, now I want do a redirect ot the 404 pages! I am hosted on an appache server, but I have never try my hand at .httaccess.

Can you please advise on the subject on how to do a redirect! Do I really need to use .httaccess?

I need help badly,

My regards,


Neville
smiley for :blank:

I have very limited knowledge of redirect, preferring rewrites, as above. Search part one of the above article for the phrase "404", and download what you find (my clever 404 script). If you use php on your site, its ability to jump users automatically to your missing pages will be most useful. ;o)



Stickyness - 20.09.11 9:05 am

Hi Cor,

Must say, I find your blog, its content and your explanations very informative and often enlightening. Thanks! I visit often when I'm stuck or other dudes don't explain as well.

I have some terrible mod_rewrite ailments and using your catch-all.php would help me guide those in the know to help me overcome the Apache Fever I currently have.

One question: when you say: 'make it your target' does the RewriteRule go in the .htaccess, I assume, and does it stand on its own?

Just trying to get a practical application of using this amazing debugging tool to end my nightmare.
Thanks!

Yes, the rule would go inside .htaccess (or anywhere else you might put a rewrite rule, httpd.conf, etc.), Here's a slightly useful example..

RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_URI} !debug-report.php
RewriteRule . debug-report.php?uri=%1

And Yes, it stands alone. If everything goes to plan, debug-report.php pops up in your browser with oodles of information.

;o)

ps. my blog is at /blog/, this is a finely crafted article!



shri ram - 20.09.11 1:19 pm

Hello Sir
I have site as xyz.com/123 and i have one more domain as abc.xyz.com

I want abc.xyz.com point to xyz.com/123 but it should not change the URL address. URL address must be abc.xyz.com

Can you please tell me how can i do that?

Use an internal rewrite (no [R] flag). ;o)



azanga - 22.09.11 10:05 am

Hello, thank you for this post, very helpful!

i have a small problem, i change the script i'm using for my website, and i want to redirect all url's to my new website...
How can i do it?
i have .htaccess

this is the old url look like:

http://www.oldexample.com/quick_search.php?sel=last_ads&sorter=3&order=2&page=14
http://www.oldexample.com/quick_search.php?sel=last_ads&sorter=2&order=1&page=35/
http://www.oldexample.com/quick_search.php?sel=last_ads&sorter=1&order=1&page=27
http://www.oldexample.com/quick_search.php?sel=last_ads&page=22&order=2&sorter=2
http://www.oldexample.com/viewprofile.php?sel=rent_ad&id_ad=942&view=map
http://www.oldexample.com/viewprofile.php?sel=rent_ad&id_ad=872&view=general

And i want to redirect to http://newexample.com
can i use redirect 301?
and how can i setup the .htacess to redirect all /quick_search?xxxxxxxxxxxxxxx
because i have 2800 url to redirect

Thank yousmiley for :lol:

Yup, a regular R=301 will work. Use the [qsa] flag to keep your query strings. ;o)



a - 23.09.11 11:58 am

If I want to remove the .php .html .htm etc extension and I want any case that user key in url, it change to lower case.
And I want to define my own 404 error. I had a 404 error page and I want user to be redirect to there when 404 error occurred.

How the rules going to be? Thanks.

As far as I know, changing the case would only be possible with twenty six RewriteCond statements! Why not? Defining your own 404 is covered in part 1, I think, the redirection part is automatic. ;o)



wanttobe - 26.09.11 8:26 am

how can i make virtual subdomain via htaccess on joomla?
when i write
http://subdomain.example.com/tid=1 on address bar, and in the background have that transparently translated, server-side, to
http://www.example.com/index.php?option=com_content&view=article&id=73&Itemid=267&tid=1&toko=subdomain

i have activated wildcard DNS on my server and also i have include some script on original .htaccess and there is my script:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]

#  Uncomment following line if your webserver's URL
#  is not directly related to physical file paths.

########## Begin - Joomla! core SEF Section
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

[b]#### subdomain alias
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{HTTP_HOST} !^www\.multitoko\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.multitoko\.com
RewriteRule ^([^.*])?$ index.php?option=com_content&view=article&id=70&pid=1&toko=%2 [L,NC][/b]


but it still not work :(

best regards

wanttobe

It's best to keep it simple, like in the example I  gave! ;o)



kb0000 - 30.09.11 5:19 am

I really like the Tutorial on .htaccess. Have read about .htaccess tips and tutorials earlier, but this one really provided easy to understand and basic instructions with examples.
And at the end Fatal Redirection proved to be very useful.. smiley for :)


Strobe - 02.10.11 1:43 am

Greetings corz

I've been using some of your .htaccess code in my hosting and been working excellent thanks a million!!smiley for :D

Since then I've purchased other tld's domain.net, domain.org, etc...
Currently I'm using the following in the root directory to add a www. prefix (I really like the www. prefix) :

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

All other .net, .org etc are pointing to one folder in the root directory with a the redirect:

Redirect 301 / http://www.example.com/

My question is, can this be done with more efficiently and if so can you please give me an example smiley for :roll:

Thank YOU!

Without knowing more about your setup, it's difficult to say what is more or less efficient. Generally speaking though, rewriting is faster, much more so when it is internal rewriting (no [R] flag). ;o)



Saki - 12.10.11 3:30 am

Is it possible to add code to an .htaccess file to make a username or password expire automatically—for example, after 24hrs or 7 days from the day it was created? I am not very tech savvy, so I may have missed it, but I don't think this was addressed in your discussion of .htaccess password protection. Thanks!

This is best handled with your preferred server-side scriptiung language (i.e. php). You can do it completely in .htaccess, sorta, using cookies.. Load this page with all comments and search for "expire", for example code. ;o)



Richie - 13.10.11 3:13 pm

I am trying to redirect a upload folder to another directory but it does not seem te work.

Here's the code:
RewriteEngine On
RewriteRule ^paginabeheer/.*$ - [L]
RewriteRule ^images/.*$ - [L]
RewriteRule ^paginabeheer/uploads/.*$ - [L]

RewriteRule (.*) paginabeheer/weergave.php?SitId=$1
RewriteRule uploads/(.*)? paginabeheer/uploads/$1 [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?ad-net\.nl/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://i.imgur.com/qX4w7.gif [L]

I try to get all files from uploads to paginabeheer/uploads. In this directory there are images, but the images are not shown.

The fourth rule "RewriteRule (.*)..." is catching every request and sending it to weergave.php ;o)



Sam - 17.10.11 11:31 am

Hi Cor, thanks for the great article.

Could you help me with following code?

I actually want the following link

/booking1/index.php?id=100&page=103&session=1500
/booking2/index.php?id=101&page=103&session=1500
/booking3/index.php?id=102&page=103&session=1500

to look like

/index.php?id=100&page=103&session=1500

Let me explain, if I put www.mysite.com/index.php?id=100&page=103&session=1500 it works, but
www.mysite.com/booking/index.php?id=100&page=103&session=1500 doesn't. So I would like to tell htaccess that every link has to go from the root (www.mysite.com) and not from the page there the link starts (i.e. booking1, booking2, booking3).

Here is my recent code :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule .* index.php

Thanks in advance.

Try something like this..
RewriteRule ^booking(.*)?  /index.php [NC,L,QSA]
;o)



prabhu - 24.10.11 6:34 am

i have two pages one is category page and other is product info page ...i use this .htacess but its get result in category page only and product info does not get result ., how to change the htacess so that i can get two paeges work correctly


Options +FollowSymlinks
RewriteEngine On


RewriteRule ^([a-za-z0-9-/]+)$ category.php?cat=$1
RewriteRule ^([a-za-z0-9-/]+)/$ product_info.php?pid=$1


thanks in advance

I ignored this question for ages because the code in your question made little sense. I just now realized you malformed your bbcode, obliterating the juicy stuff. If only people read that HUGE text right above the comments. Och well...

The answer is simple, your first rule is catching everything - fix that to match the name of your category page. If you have only two pages (on your site) you could leave the second line as-is, though I wonder why you repeated the a-z, rather than do a-zA-Z. Do you really need to catch all other requests and rewrite them to product_info.php? If not, fix that line, too. ;o)



Anthony - 29.10.11 12:39 pm

Cor,

I don't have a question for you... know why?

This page is awesome!

Thanks for taking the time to put together such a great resource. After reviewing everything here, I still have questions on certain things; but, now I know enough to search for those answers on my own.

This is really good stuff,
Thanks again!

~anthony

The comments here are also worth searching, so many questions have already been answered, some of those answers even make sense! ;o)



Tolo - 06.11.11 10:39 am

Thanks for this tutorial.

Clean, concise and with many examples.

I googled for a tutorial about mod_rewrite and I think this was by far the best I found. I just need to read it a couple of times more to digest it.

Just wanted to let you know.

Thank you Sir.

So many people think I'm joking when I say "Read it again", I wrote a poem about it. It is that discipline which makes my text dense. A second or even third reading often reveals whole new levels of meaning, enlarging understanding in the reader. But then, a lot of stuff makes more sense second time around. Thanks. ;o)



subh stewart - 10.11.11 5:20 pm

Hi,

i am hosting in a sharehosting plan.

I registered the domain http://xyz.com

then i have one addon domain http://abc.com

unfortunately i can access "abc.com" like this "http://xyz.com/abc.com".

could you assist me how can i disable that.

This sort of trivial rewrite is an essential skill for the serious webmaster. How far did you get with it? ;o)



azella - 10.11.11 8:21 pm

Thanks


matične celice - 12.11.11 11:08 am

Hello there. I'm trying to delete the folderurl from my url but i can't solve it.

the original url is: http://example.com/blog/items/lorem-lipsum-dolor-sit-amet
i want to make: http://example.com/blog/lorem-lipsum-dolor-sit-amet

If you don't notice, I just want to remove "items" from link.

Can anyone help me?

Best regards,
Matične Celice

My response to subh stewart (a couple of comments ago) covers this. ;o)



Confused - 18.11.11 5:13 pm

Greetings,

Trying to redirect typos in a search query when multiple words are used.

Example:

User searches for:

bicycl accessories

URL looks like:
domain.ext/search.php?query=bicycl+accessories

Need to redirect to:
domain.ext/search.php?query=bicycle+accessories

Thanks!

UPDATE: JUST now figured it out - lol


RewriteCond %{QUERY_STRinG} bicycl.accessories
RewriteRule search.php /search.php?query=bicycle+accessories



I noticed in Maths class, as a kid, sometimes just forming the question (for the teacher) immediately led me to the solution. Confused?.. Not. If only every post here went like that! ;o)



Confused - 19.11.11 10:30 pm

LOL - isn't that the truth.

Thanks for having a venue that helps us all!


jakee - 25.11.11 12:36 am

The requested URL /advanced.html?hl=en was not found on this server. That’s all we know.


Manu - 30.11.11 11:08 am

Dear All,

Any one help me for a redirection.

http://www.mysite.com/garantie/certificat.cfm?UUID=AF360A84-D77F-9F3C-8FFD56EAB5AC7090

TO

http://www2.mysite.com/garantie/certificat.cfm?UUID=AF360A84-D77F-9F3C-8FFD56EAB5AC7090

Any URL having /garantie/certificat.cfm?UUID="VARIABLE_PARAM" need to point to http://www2.mysite.com/garantie/certificat.cfm?UUID="SAME_VARIABLE_PARAM"

Please help me,
Thanks

I help people who at least try to help themselves, especially those who don't think it's clever to mail me a copy of their lazy postings. ;o)



maddes - 02.12.11 1:36 pm

Genius!

I spent 2 and a half hours trying to set up my virtual server (multiple domains on a cheap godaddy accound that doesn't let my point different domains to subfolders)

You just solved it in a minute!


sailor - 03.12.11 8:44 pm

Email me - I have a paying job for you. Our sailing site was compromised( I thing through the UBB threads board) and re-wrote a header ( a really really long one) to every php file in every directory - I wrote a script and deleted every instance (now more cancer right?) but the bastards can still re-write my htaccess. They are getting to my public and root access - 644 is what I have it set to. Im getting pretty pissed. I was going to nuke and restore to a point before the compromise but - do I really need too?

Hope to hear from you -

MKP

My email address is on every page. ;o)



Ken - 06.12.11 12:13 pm

An excellent tutorial and giving me lots of new ideas! smiley for :ken:

I'm having trouble with one concept, however, and that is of redirecting, i.e. using the [R] flag.

On the site I'm dealing with at the moment there are very many domain names and a small number of businesses, each business going to its own folder within the main domain, for example...


RewriteCond %{HTTP_HOST} ^(www\.)?animalroadshow\.co [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?animal\-roadshow\.co [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?theanimalroadshow\.co [NC]
RewriteRule ^(.*)$ "http://mydomain.co.uk/animal-roadshow/$1" [L]


I've tried specifying the RewriteRule options as [L], [R,L] and [R=301,L] but from the user point of view they all seem to have the same effect! (I'm clearing all browser cache data between tests).

I've read the Apache docs about this flag but am still having difficulty understanding what actual effect it has. smiley for :ehh: Can you help me out with another explanation? Or if I implement your debug-report.php code, will that show me what's going on?

The [R] flag specifies an EXTERNAL redirect. If you have already specified "http://" (an external URI) then the [R] flag is immediately redundant.



BTW, are you aware that the links in the section 'conclusion' refer to 'localhost'?


The answer to your question is contained within that section. Read it again! ;o)



HarDCOre - 07.12.11 10:26 pm

Hey all;My question is,if my link for "contact" is
e.g contact.php,what i need to do when i click to "contact" link next time to see something like "ask a question" or whatever i want as a text instead real one (default)link.Thanks!

<a href="contact.php">ask a question</a>
HTML 101, dude! smiley for :lol:

Of course, you might have meant that you want the link to be "friendly" (i.e. http://example.com/ask-a-question) and have it rewritten transparently to http://example.com/contact.php; if so, you are in luck, there's a big page all about that sort of thing right above these comments!

;o)



taline - 08.12.11 12:03 am

Hello, excellent article, thank you for taking the time and the willingness to help!

My question is the following, I have done some URL rewriting and some of the files that I am redirecting to are under the SSL protocol as follows:


htaccess code to redirect two folders to https

RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} /database/secured [NC,OR]
RewriteCond %{REQUEST_URI} /database/user [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]


and the part which does the rewriting is the following:

RewriteRule ^login$ /database/secured/user/logincontroller.php [NC]
RewriteRule ^business$ /database/secured/business/BusinessLogin.php [NC]
RewriteRule ^logout$ /database/user/logoutcontroller.php [NC]


It is not working, without the SSL part everything is ok, but once the SSL comes in, the friendly url is not being kept on the address bar.

Is there anything I could do to improve it?

Thanks in advance smiley for :D

EDIT.: Typo

If you provide SSL links to the user sections, it should all work great as-is, e.g..
<a href="https://yourdomain/login">login</a>
;o)



Sumit Bijvani - 08.12.11 5:09 pm

I am trying to implement URL rewriting in my PHP application. Can someone share a step by step procedure of implementing URL rewriting in PHP

In my application I want to implement following URL rewriting, I want to redirect

http://example.com/fast-five
http://example.com/300
http://example.com/13-b

to

http://example.com/movie-download.php?nm=fast-five
http://example.com/movie-download.php?nm=300
http://example.com/movie-download.php?nm=13-b
smiley for :roll:smiley for :roll:

Thanks in Advance

That side of things is best handled with .htaccess, as per this article. ;o)



HarDCOre - 11.12.11 11:05 pm

"Of course, you might have meant that you want the link to be "friendly" (i.e. http://example.com/ask-a-question) and have it rewritten transparently to http://example.com/contact.php; if so, you are in luck, there's a big page all about that sort of thing right above these comments!"

Hey Cor.So my "contakt"link actually is file from the system called contact.php.What i need to do,when i click to "contact" to see in browser only
www.mysite.com/ask-a-question (but not www.mysite.com/contact.php) or whatever i want?Thanks once again!

mod_rewrite is what you need. See article, above. ;o Cor



Mario Bruneau - 13.12.11 2:59 am

Hi, thanks for sharing this valuable information and knowledge.

I've been searching for weeks now to redirect a MultiSite enabled Wordpress site with sub-domains.

I would like to do some redirections in the french version of my site.

The main site is pianotuninghowto.com in English and in French it is fr.pianotuninghowto.com

The .htaccess file only takes care of the regular root pianotuninghowto.com so I can't find the way to redirect the fr.pianotuninghowto.com portion.

This was created with Multisite Wordpress 3.2.1 and it does not create a folder /fr/ on the server. Seems it is "virtual" in Wordpress Multisite.

Would be great to finally find the solution for this problem.

Thanks a lot

Mario Bruneau

I'm not familiar with Wordpress Multisite, and without a look at your actual .htaccess, I can't offer any solutions. ;o)



Nate - 13.12.11 4:39 am

Great article have bookmarked to your site on the caliber of this article, nice one.

Doesn't look like many people read the "Read before you post a question" part though smiley for :lol:

Or maybe they just pretend not to have read it. ;o)



Lawrence - 18.12.11 6:47 pm

Thanks for your tutorial. It's the first one I get ;-)

I've got a situation where I've created a page for each city and state in the country. We only have custom products for 80% of the cities, so in other cities normally the page would redirect to a default product page. Is there a condition that would allow a redirect to another page (e.g. default.php) if it was determined that no product was available?

This works as long as there is a product available:

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^custom_product/([^/]+)/([^/]+) /custom_product.php?ST=$1&CTY=$2 [NC] 


Apparently what happens now is that if there isn't a custom product page, it simply shows a 'Firefox has detected that the server is redirecting the request for this address in a way that will never complete.' error message (not even a Page Not Found) instead of redirecting to the default product page.

Thanks.






I got it now. The redirect to the default page was in a different directory so needed to make relative URL change.


Peter - 25.12.11 3:31 am

Hi,

Awesome article, really helped me along! Still, I can't get the following to work:

I want to redirect this:

http://www.example.com/index.php

to this:

http://www.example.com/index.php?page=home .

My full .htaccess looks like this:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301]

RewriteRule ^index\.php$ index.php?page=home

The redirect works if I just go to /index.php, I get redirected to /index.php?page=home.

The problem is, when I click a link to go to index.php?page=contact, I also get redirected to index.php?page=home.

So I guess index.php?page=contact also gets matches ^index\.php$ but I can't for the life of me figure out why. I thought the ^ and $ marked the beginning and end, so that only index.php would get matched..

Thanks in advance for any help,

Peter

The ^ and $ DO mark the beginning and end, but the query string is something else. Add a RewriteCond to catch query strings. ;o)



Ivan - 27.12.11 10:45 am

Hello
thanks for excellent easy reading article
Unfortunately I did not success to resolve my problem
Tried a lot solutions, but got Server 500 error, invalid redirect or simply no effect.

The problem is caused by some websites listing my links and inserting in query string codes like %3F and %3D instead of characters ? or =.
This produces the undesired not found errors in google reports.

How to redirect (or rewrite)
www.example.com/Zadar/Zadar.php%3Fprogr%3DA
or
www.example.com/Baska Voda/Baska Voda.php%3Fprogr%3DA
into
www.example.com/Zadar/Zadar.php?progr=A
or
www.example.com/Baska Voda/Baska Voda.php?progr=A

Thanks in advance
Ivan

Firstly, ensure it isn't YOU who is providing these links in the first place. Google is scanning YOUR site, remember. As for the current inward links, there is a solution, though it is rather messy. See here. ;o)



dreamluverz - 07.02.12 7:49 pm

I bumped into your site late last year when looking for references about mod_rewrite. Then today I'm trying to solve some rewriteRule and I remembered your site. I forgot the domain...but all I can remember is how descriptive and informative your article was plus the examples you gave...so I tried my luck with google with different keywords hoping to find your site again smiley for :) Luckily I found it! Thanks so much btw for this tutorial smiley for :)

Try: .htaccess rewrite


btw, I tried going to your poems https://corz.org/words/Work/ and digging your works there

Thanks


and while clicking "back" for the previous pages I got this message:

"Please don't hammer corz.org!
You must wait a few seconds before trying again.

If you believe this is in error, please mail cor about it!
<bugs at corz dot org>"

It's not a bug, it's Anti-Hammer (Google it). I was thinking about adding a Captcha for first-time "offences", a pass decreases the hammer's trigger time, for fast surfers. Having said that, most back() actions should be served by the browser's local cache, not re-fetched. Hmm..


Again thanks! smiley for :D

No Problem!
;o)



Jos - 08.02.12 9:20 am

Hi,

Thank you for this great article.
I try to make it work on my testserver, a macmini with Tiger on it.

When I use your codes none of them work.
So I probably made a basic mistake or the Apache server I use is blocking it.

To figure it out I went into the httpd.conf file.
Mod_Rewrite is on as I saw before when using phpinfo()

But I also noticed this text:
AllowOveride None

I made I screendump to show you.
Might this be the problem?
This is de location of the screendump:
http://home.versatel.nl/vecht049/AllowO.png

Thanks for your tutorial.
Hope to hear from you,

Jos

Yes, that seems to be the problem. Switch it to "All" (no quotes) and restart your Apache server. ;o)



Jos - 08.02.12 9:58 am

Thanks a lot. Got it working.


Kiss - 08.02.12 3:31 pm

Best user agent detector in the world here
<snipped -ed>
Last update 02.02.2012 Please make a topic about this.©

You can find something similar, except in PLAIN TEXT and without the extra misinformation, right here. ;o)



ofir - 12.02.12 6:28 pm

my mind left the building and still runnig away from those unexpected combinations of letters.you are brave man. and really talented.but how can you write so nicly about somthing so repulsive?!!.anyway loved the first part thanks smiley for :)


Levi - 14.02.12 5:21 am

Hey Cor. Seem to be having an issue. I tried to follow this the best my little brain would let me.
I have an htaccess file, and its working great for some things (thanks to you!) but seem to be stuck at something really puzzling. Before I try to explain what its doing - or rather, what its NOT doing.. ill post it for you and maybe you can spot something obvious.

# Start of .htaccess file.
RewriteRule ^index$ index.php
RewriteRule ^cpanel$ cpanel.php
RewriteRule ^virtual$ live/directory/
# the line above this is the one thats not working.

RewriteCond %{REQUEST_FILENAME} !cpanel.php$
RewriteCond %{REQUEST_FILENAME} !index.php$
RewriteRule ^(.+)$ index.php [NC]
# End of .htaccess file.


If its not abvious what im trying to accomplish, ill explain.

First im redirecting only 2 files, mysite.com to mysite.com/index.php.
For clarification, I know most websites automatically redirect to index.html/index.php by default..
I added it just in case. (What could it hurt?)
and the second is mysite.com/cpanel to mysite.com/cpanel.php

This part works excellent! Now, the tricky part is getting the mysite.com/virtual to work.
I want this to redirect ALL other file/directory requests to a certain directory.. NOT a file.
I found that I can EASILY manipulate mysite.com/virtual to go to mysite.com/live/directory/file.php
by using this line:
RewriteRule ^virtual$ live/directory/file.php


but again, for clarification - I want all file/folder requests to go to a certain directory RATHER than a file IF it doesnt match index.php or cpanel.php.

So, maybe im missing something rather obvious, but I cant seem to wrap my head around it..
Also, another quick question.. Would it be possible to use 2 (or even more!) virtual directory listings?

Like say:
RewriteRule ^virtual_1$ live/directory/
RewriteRule ^virtual_2$ test/directory/


I really hope you reply to this message. I could really use the help!! Could you perhaps email me the reply also? Ill try to check back on this website but I usually check my email more often.
Thanks in advanced.
-Levi

You could simply add a third RewriteCond..

RewriteCond %{REQUEST_FILENAME} !live/directory

Also, if "virtual" refers to a directory, users may use "virtual/", so it might be wise to alter that RewriteRule to..

RewriteRule ^virtual/?$ live/directory/

And yes, there's nothing to prevent you creating multiple virtual directories.

;o)



Halfdogg - 15.02.12 11:00 am

Hi,
I have scanned the internet for a mod rewrite solution to turn my urls from:
http://www.example.com?section=this&catid=1&articleid=1
to
http://www.example.com/about-us/the-team (like they do in WordPress)

It has forced me to hard code all my dynamic links in my htaccess which becomes tiresome when you add more content onto the website because this method is not automated. eg.

--------------------------------------------------
RewriteEngine on

RewriteCond  %{REQUEST_FILENAME} !-f

RewriteCond  %{REQUEST_FILENAME} !-d

RewriteRule ^the-team$ index.php?section=this&catid=1&articleid=15 [NC,L]
--------------------------------------------------


Please help me come up with a workable automating solution just like they do in Wordpress. Note: All the content is stored in a database.

See above. Particularly the section entitled "a simple flat link with two "path" variables.. ". Of course, you will also need to code your CMS to produce such links in the first place. ;o)



Chris - 17.02.12 9:03 pm

Great article and some really useful advice thank you.

I do have an issue though and despite trying pretty much everything I can think of including your suggestions I simply cannot resolve. I wonder if you could help and in doing so it may be a useful addition to your blog post?

Currently, what I am trying to do is redirect the blog on my site to new pages within my main site. For example www.mydomain.co.uk/blog is a wordpress blog. I have been unable to make that
blog site take on the look and feel of my main site / store so what I am going to do is recreate these pages within my site and then 301 the blog pages to the new ones.

One example is this page:

http://www.mydomain.co.uk/blog/?page_id=78

Which I want to redirect to this page as the content is the same:

http://www.mydomain.co.uk/index.php?route=information/information&information_id=20

However, no matter what 301 I try, it makes my site fail to an internal server error (500) or just does nothing i.e. the old page still comes up (in new browsers, on different computers etc).

I know you're not there to be my tech dude and I appreciate that but I'm so frustrated so wondered if you could offer any thoughts?

Thanks for reading.

Show me the .htaccess code you are using and tell me where you are putting that code. Then I can offer some thoughts. ;o)



Chris - 18.02.12 11:55 am

Thanks, I've just been trying to post the htaccess content but it won't let me publish? It says:

warnings..


note: your tags are not balanced!
(check your bbcode)

notes..
to produce a square bracket, double it! -> [ ! ]
to insert shell code or ascii art, use [pre][/pre] or [tt][/tt] tags.
for php web code, use
 tags..
[ccc]<?php
echo 
"foo!";
?>

for more information, check out the instructions!

If you are happy to email me direct I could email the content to you?

Many thanks

You followed the instructions above these comments and used [pre] tags, right? ;o)



toogs - 18.02.12 1:44 pm

Hi, this is a great tutorial. However, i still cannot do a seemingly simple redirect... I could use some help...
here's what i want to do:

I want :
www.domain1.com/file1?x=1&y=2&z=3 to be redirected to
www.domain2.com/file2?y=2&z=3

sorry if for some of you this is trivial, I 've tried many things but nothing seems to work....

Thank

Something like this..
RewriteCond %{QUERY_STRING} y=(.+)&z=(.+) [NC]
RewriteRule file1 http://www.domain2.com/file2 [QSA,NC]

will work well and is quite flexible, basically passing all the original variables through to the target file ("Query String Append"). The only disadvantage is that you have an extra "x=x" at the target end; the target URI becomes..

http://www.domain2.com/file2?x=1&y=2&z=3

In most applications this would have no negative effect. If you really must keep it exactly "x/y/z -> y/z", use something like this..

RewriteCond %{QUERY_STRING} y=(.+)&z=(.+) [NC]
RewriteRule file1 http://www.domain2.com/file2?y=%1&z=%2 [NC]


I'm assuming that in each case you want the variables to be variable!

;o)



Chris - 18.02.12 2:32 pm

Hi, I have tried to follow the instructions but I'm not a coder so I'm really struggling unfortunately. After your last message I tried again for the last few hours but I don't understand what I'm supposed to do to allow it to submit?

Sorry for being a novice, hope you are still able to help?

Many thanks

No need to apologise, we're all novices at some point, at some thing. It's simple, click the button for Preformatted Text, the one that looks like this: an image The PRE tags will appear inside the input, like so:

[pre][/pre]

Then you simply paste your code into the middle of that, between the two tags..

[pre]<CODE HERE>[/pre]

If that isn't working for you, mail me the code and I'll replace this post with it.

;o)



Chris - 18.02.12 6:07 pm

Thanks, here is the htaccess code as it stands with no redirect attempts:

-----------------------------------------------------------------------------

# 1.To use URL Alias you need to be running apache with mod_rewrite enabled.

# 2. In your opencart directory rename htaccess.txt to .htaccess.

# For any support issues please visit: http://www.opencart.com

Options +FollowSymlinks
RewriteEngine On

# Prevent Directoy listing
Options -Indexes

# Prevent Direct Access to files
<FilesMatch "\.(tpl|ini|log)">
Order deny,allow
Deny from all
</FilesMatch>

# SEO URL Settings
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/

# RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.co\.uk
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.mydomain.co.uk/ [R=301,L] 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

### Additional Settings that may need to be enabled for some servers
### Uncomment the commands by removing the # sign in front of it.
### If you get an "Internal Server Error 500" after enabling any of the following settings, restore the # as this means your host doesn't allow that.

# 1. If your cart only allows you to add one item at a time, it is possible register_globals is on. This may work to disable it:
# php_flag register_globals off

# 2. If your cart has magic quotes enabled, This may work to disable it:
# php_flag magic_quotes_gpc Off

# 3. Set max upload file size. Most hosts will limit this and not allow it to be overridden but you can try
# php_value upload_max_filesize 999M

# 4. set max post size. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value post_max_size 999M

# 5. set max time script can take. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_execution_time 200

# 6. set max time for input to be recieved. Uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_input_time 200

-------------------------------------------------------------------------------------------

I have tried to add variations of 301's at the very end after point 6, and have also tried adding them to the end of the rewritebase section.For example:

redirect permanent /blog/?page_id=78 www.mydomain.co.uk/index.php?route=information/information&information_id=20
redirect 301 /?page_id=78 www.mydomain.co.uk/index.php?route=information/information&information_id=20

I have also tried these with .htm / .html at the end of the url's. I contacted my host who couldn't help - 
server is set up as necessary and it was they who gave me the link to your article as it happens...

Hope you are able to help? Many thanks.


I am able to help, but as this isn't a page about mod_alias, I'm disinclined to (it's a page about mod_rewrite, which does rewriting). But as you've went to so much trouble, all lovingly recorded smiley for :lol:, and to prevent further confusion, I'll spill the beans anyway.

If you RTFM, you will see it right there at the beginning of text, "The new URL should be an absolute URL beginning with a scheme and hostname.". Note my bolding.

In other words, you need to use the "http://" part, or it won't work. One would think you would have tried this, or reading the manual, first.

;o)



Chris - 18.02.12 10:05 pm

Hi Cor, appreciate the advice even though it's slightly different however I have tried your suggestion and followed all instructions exactly and it STILL doesn't work?? Now, sure, this may well be my novice coding abilities but I don't understand what the problem is? The line I am adding to htaccess is:

redirect /blog/?page_id=78 http://mydomain.co.uk/index.php?route=information/information&information_id=20

Yes, that's better!


I have added it at the very end of the content detailed above and also tried it at the end of the rewritebase section and I have also tried it in both locations with .htm and .html at the end of each url?

But have you tried it before the RewriteBase Section?


Any idea what could be the problem?

Thanks again for your advice it is very much appreciated.

Best
Chris

I really should have studied your entire pasting instead of rolling my eyes at it - I tend to glaze-over at other people's code smiley for :ken: the top of it scrolled out of view and I could see the problem right there! It's more relevant than first suspected. That "RewriteBase Section" you refer to is actually a "mod_rewrite" section - . RewriteBase is just one of mod_rewrite's commands. As luck would have it, this is a page all about mod_rewrite! In a sense, it's a mod_rewrite issue.

mod_rewrite does rewriting of URI's; the request comes in and the rewritten (aka. "altered") URI goes out. The request is changed. By the time it gets to your mod_alias code (the "Redirect" commands) it is not what it was before, think about it.

The question is, will it still match your Redirect's "URL-path" (the left-hand URL)?

Of course I could have just dropped my previous remark, left it at that. I'm explaining all this because firstly, the comment at the top of your .htaccess..

# 1.To use URL Alias you need to be running apache with mod_rewrite enabled.

is potentially confusing for someone who also wants to use mod_alias, i.e. the "Redirect" commands (blame the creators of opencart, I presume).

And secondly, because it is good for webmasters (that's you) to have at least a rough idea about what is going on in their config files. Mistakes here can be devastating for your site.

Finally, it should be noted, you could use mod_rewrite (what this page is about) to do the exact same thing, basically replacing your Redirect command with a RewriteRule.

Have fun!

;o)



Chris - 20.02.12 1:39 pm

Hello there

Thanks again for a detailed response, unfortunately I am still having absolutely no joy...cue rolling of your eyes smiley for :)

I have tried it before the rewritebase section as you suggested, again tried it in various formats (.htm / .html) but nothing works. I am flummoxed...

I guess I should perhaps accept my limitations and pay someone to take a proper look at the code as there may well be something I've been unable to relay to you that is affecting it?

Any other thoughts welcome though and thanks again for all your help so far.

Remember, a 301 redirect is a "browser thing", and permanent. Have you restarted your browser? Tried a different one since fixing it?

I don't understand what you mean about trying various formats; the links you want to redirect to/from are fixed, no? changing them is pointless.

;o)

ps. it's always wise to work with temporary redirects until you have it working, and then switch to permanent, as explained in the main article.

pps. if it were my .htaccess, I'd use mod_rewrite for this.



Strobe - 24.02.12 1:38 pm

Greetings Corz,

Ok I have web hosting with multiple tld's: domain.com, domain.net, domain.info

The problem:

I've setup the web hosting to send all these domains to the same directory, as a result the site can be viewed via different tld's ...

I wish to have all tld's rewritten to domain.COM and any future tld's will also be rewritten to domain.COM

NB: I also wish to keep the www. prefix.

Really looking forward for your responsesmiley for ;)

Strobe.


Too easy! Probably a stranger passing you in the street could write this.
Anyway, I see no code to help with.

;o)

ps. serving the same content from different TLD's is Google Suicide.



Ben Racicot - 02.03.12 3:46 pm

First off, this site rocks! It's my new edu on htaccess.
I hope I can ask you pro's about my unique issue with a mod-rewrite
on my SuPHP server. I got my script to load and function by commenting out
the php rules. (only for use with php.ini I found out) but now I get 404's.

How do we know what to do with php rules in the htaccess file when we're
forced to use a .ini for them?

All .htaccess php directives have a php.ini equivalent. Generally it's something like:

display_errors = off

Esentially removing the "php_flag" part.

#php_flag display_errors Off 

RewriteEngine on
#RewriteBase /mypages/
RewriteRule ^sitemap.xml$ /sitemap.php?target=google [L] 
RewriteRule ^urllist.txt$ /sitemap.php?target=yahoo [L]
RewriteRule ^outgoing/(.*)$ /out.php?url=$1&%{QUERY_STRING} [L]
RewriteRule ^pages/(.*)$ /page.php?page=$1&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?%{QUERY_STRING} [QSA]

After commenting out Rewritebase the script works but returns a 404 when
clicking outbound links.

Thanks guys.

Without knowing the make-up of your filesystem/CMS it's not possible to say whether your RewriteBase command is or isn't valid. It sounds like your "out.php" file is simply not where it's expected to be. With the forward-slash in front of it, Apache will expect it to be in the root of the site.

It should be noted, your %{QUERY_STRING} parameters are long-winded and/or redundant. Use only [QSA], instead.

;o)



Steve - 06.03.12 8:52 pm

when adding to htaccess the following...
RewriteEngine on 
Options +FollowSymlinks 
RewriteBase / 

RewriteCond %{HTTP_USER_AGENT} Baiduspider [OR]
RewriteCond %{HTTP_USER_AGENT} Baidu
RewriteRule .* - [F,L]
the hit counter stops working (Front Page extention)
which is also in the same .htaccess file
which begins...
# -FrontPage-
Options None
<Limit GET POST>
order allow,deny

can someone help me? please.

I know nothing of FrontPage.
If you send me your entire .htaccess file, I might be able to figure out what's going on.

;o)



SK - 08.03.12 5:57 am

This is an absolutely great piece . Very clear , simple to follow and nice examples.

I have bookmarked this site.

I had a problem with my site
To allow friendly URLs I had rewrite done in the .htaccess

however when a user clicks on a link /sitemap it picks up sitemap.xml instead of redirecting to sitemap.php

I wanted the sitemap.xml for the search engines but want a formatted PAGE for normal users who click on the /sitemap URL

I am using this for the sitemap rewrite
RewriteRule ^sitemap$ /sitemap.php [QSA,L]
Can you help ?

Thanks a lot for the tutorial !!

There's probably something going on the the parts of your .htacces that you didn't post. Without seeing that I can only advise you move the rule above the other stuff or change the name to something else. BTW, you shouldn't need the QSA flag. ;o)



SK - 08.03.12 12:29 pm

Hi Cor,

Thanks for the immediate response.

The sitemap does contain may more lines of rewrite which work fine.

I assume you mean the .htaccess file.

Only the sitemap url doesnt get redirected to sitemap.php WHEN the sitemap.xml is present.

If sitemap.xml is renamed / removed it gets redirected to sitemap.php

It sounds like mod_negotiation is automatically redirecting the user to sitemap.xml. You could disable that, or try this in your .htaccess file:

Options -MultiViews

I havent tried using this line exclusively. will give that a try.
Also if you permit i can mail you my .htaccess since it will be too big to post here.

Thanks

If you are still stuck after trying the above, you could mail me your .htaccess, I make no promises about when I'll get to look at it. ;o)



CindyC Barnard - 14.03.12 6:58 pm

You said it... redirects can lead to many headaches...

Working on a WordPress site now, and redirects keep "redirecting in a way that will never be resolved" (my project is a new site design going to a new domain name and I need to redirect all old page; and page names have all changed, except blog pages.

So this isn't working:
301 redirect
Redirect 301 /this page http://www.newdomainname.com/this-page/
I don't know much about mod_alias (I generally avoid it), but I'm pretty sure that first line is wrong.

I've been told to place it below, and sometimes above, the WP premalinks rules, which are:
RewriteEngine On
RewriteBase /
# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
And tried a rewrite rule too:
RewriteCond %{HTTP_HOST} ^www.olddomainname.com/blog$
RewriteRule ^/?$ "http\:\/\/www\.newdomainname\.com/blog" [R=301,L]
/blog is not a part of %{HTTP_HOST}, is it? Also, all those back-slashes (and quotes!) on the rewrite target are unnecessary (also "^/?$" is unlikely to match any actual requests, try "(.*)").

Also trying to understand if by redirecting non-www to www, say, that this may be adding to the redirect errors; so I did remove all canconical redirects.

It's "canonical". Do it early in your .htaccess and you should have no problems.

Any suggestions?
_Cindy Barnard

Firstly, you need to put your "redirect" (though I would definitely use mod_rewrite, not mod_alias for this) on your OLD SITE. Think about it; that's where requests for olddomainname.com end up. The only .htaccess code you need on your new site, is the WordPress stuff (and perhaps your canonical redirect).

Start again!

;o)

ps. PLEASE use [pre] tags for code.



CindyC Barnard - 15.03.12 12:28 pm

Thanks for your reply. Thing is I am working on the htaccess file in the
OLD site.

Only thing in the htaccess file on the new site is the permalink stuff and a canonical redirect.
But I appreciate you comments, gets me closer to an understanding. Unfortunately, I'm getting most of the
code samples from the WP forum.

So considering I am writing rules on the right site (old site) maybe something like this would work (I am probably way off base):
RewriteRule ^./blog http://newdomain.com/blog [R=301,NC]
or
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,NC]

With the last rule, would this catch all pages that are same name, inserting new domain instead of old; and what would happen to pages that aren't the same? Could I write additional rules for those pages and still use a wildcard?

If this is anywhere close, where does it go in the WP htaccess file
- would the permalink rule need to come first if I am using permalinks on the site, and using the permalink name/web address to write the redirects... urghhh rewrites?

And what's the difference between a rewrite and a redirect?

Your post here has been the most useful, I have found several sites addressing htaccess and mod-rewrite, but none that can address the issue of WP permalinks and redirects from old site (old domain name) to a new site with new domain and where most page names have changed.

Any suggestions,or help is much appreciated.
Thank you.
Cindy

Ahh, so the old site is Wordpress, too! Gotcha.

Your second RewriteRule looks reasonable (the first, not).

But remember, as soon as the RewriteRule takes effect, users are sent directly to the new site (newdomain.com). Once in place, ALL requests will be captured, so further rules in the old .htaccess will become redundant.

Yes, you could create further rules to catch any pages that aren't the same, so long as you do that at the new site. The new request will be exactly the same as the original request, except for the new domain, so quite predictable. Note: you might want to also use the [QSA] flag, to keep any query strings that are present in the original request, e.g. [R=301,NC,QSA].

As for the difference between "Redirecting" and "Rewriting": They are controlled by two different modules (mod_alias and mod_rewrite, respectively). mod_alias is simpler to use but only provides a few simple mapping functions, essentially alias and redirect. mod_rewrite is slightly more complicated but can do anything.

It should probably also be noted that Rewrite rules are processed before Redirects, regardless of their position in your .htaccess file.

;o)



Rhett - 15.03.12 10:13 pm

Hello Cor,
I read your article, but was a bit confused...

This is my current .htaccess file:


Options +FollowSymlinks
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} subdomain.hostingdomain.com
RewriteCond %{REQUEST_URI} !^/home
RewriteRule ^(.*)$ home/$1 L


It works for displaying the content, but I would like it to be able to redirect all requests to my main domain which is not the same as "hostingdomain.com"

Currently the hosting domain is fatcow.com, my hosting provider, and subdomain is what most people would use (www.mydomain.com). I'm currently using a CMS that generates sitemaps using the mydomain.fatcow.com, I want google and other search engines to use mydomain.com straight and not mess with the whole subdomain junk.

Any help would be greatly appreciated.

This doesn't sound like something you want to use .htaccess for. Your domain name should point to your server (that's a DNS thing), and your server should assign your hosting plan to a folder you own. In other words, you should be able to get directly to yourdomain.com without messing around with your host's subdomain addressing.

If your host isn't allowing you to use your own TLD, get a new host. Or possibly it's just not setup in your hosting panel. Did you add your domain there, yet?

Also, tell your CMS to use your own domain name, not the hosts's subdomain. If it is accessible that way (see my above point) then indexing will work fine. If not, get a new host.

;o)



Vincent - 16.03.12 7:00 am

Hi Cor,

This htaccess tutorial is very helpful. I've seen other tutorials about mod_rewriting and this is by far the most comprehensive tutorial. It has help me a lot in building the a RewriteRule and made the url more user friendly.

I am currently using the RewriteRule in my site. The links after using mod_rewrite are working. However, I do have some questions regarding website links and files link (eg. style.css, *.jpg, *.png) after using the mod_rewrite.

Here is the dirty url
http://www.example.com/file1.php?category1=name1


Here is the seo-friendly url
http://www.example.com/file1/name1


This is working fine, and it access the correct data and file, the only thing is that the style.css is not displaying correctly, or is referenced incorrectly. As well as the links on the page.

As I checked the generated source code it shows the correct path as href="css/style.css" and href="file1/name1", href="file2/name2" but when I hover the link, it shows
http://www.example.com/file1/css/style.css

as for the links, it shows
http://www.example.com/file1/file1/name1
http://www.example.com/file1/file2/name2

It seems that the file1 is treated as a folder.

How do we go about this without using the <base> tag and without putting the complete file url in the href (href="http://www.example.com/css/style.css") / (href="http://www.example.com/file1/name1") and show only (href="css/style.css") / (href="file1/name1").


This is my RewriteRule
RewriteRule file1/(.*)$ file1.php?category1=$1 [L]
RewriteRule file2/(.*)/(.*)$ file2.php?category2a=$1&category2b=$2 [L]


Thanks a lot and more power!

The best way to get styles into your pages is to use @import with root addressing (with a forward-slash at the start), like so..
<style type="text/css">
/*<![CDATA[*/ 
@import "/css/main.css"; 
@import "/css/comments.css";
@import "/css/footer.css";
/*]]>*/
</style>
..which would load styles from inside a folder named "css" in the root of your site (above the /file1 stuff). This way, the location of the css is unaffected by your rewriting.

;o)



CindyC Barnard - 16.03.12 3:51 pm

Wow, that makes sense, and seems so apparent -- yet not me at the time!
And logical if you think of process... so send all the files on over to the new site, and then deal with those pages that aren't the same with similar rules on new site. But all those blog posts with the same page names can be handled by the one rule on old site.

Correct!  (except you aren't sending files, you are sending requests)

One more question if possible,
WordPress has a standard set of rules for the permalinks...
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

So any rules I write, especially if I'm using the permalinks name and not the php name, come after this set of WP rules?

I'm not quite sure what you mean; either you use permalinks, or you don't. If you want your old links to work on the new site, simply use the exact same permalink structure inside Wordpress on both sites (the Wordpress Options page gives you actual .htaccess code to insert into your .htaccess file.)

Any permalink requests will be most certainly be caught by the WP rules (which catches requests for all files that don't actually exist), so any rewrite voodoo you want to perform on those will need to come before the WP rewrites. Like I said, with the configurations matching, you shouldn't need any more rules. But then, I don't know exactly what voodoo you are considering so I can't say for sure.


Am I thinking logically here, so browser reads top down in the htaccess file (except rewrites come before redirects -- and probably other exceptions that I'll have to figure out).

Many, many thanks, Cor.
_CindyC

Firstly realize all this happens long before the browser sees or does anything (except, of course, send the initial request).

.htaccess file processing is performed by the web server (browser's shouldn't be able to access them) and generally speaking the .htaccess file is processed as you would expect, top-to-bottom, except where directives might rely on the setting or output of other types of directives, such as in rewriting (mod_rewrite) + redirecting (mod_alias).

If you are doing a lot of this stuff I definitely recommend you check out the apache mod_rewrite page and URL rewriting guide, links in the Useful Links section, above.

Thanks for the generous donation!
Have fun!

;o)



rosh - 20.03.12 5:47 pm

The greatest articale on mod_rewrite ever.
+1 / bokmarked / like


chris - 22.03.12 8:08 pm

This is a great article, found a lot of useful stuff, but having a little trouble redirecting requests for an old php page to a new php page, like so:

I want to redirect:
www.eclipserecords.com/artist.php?do=a&aid=3&am=bio
to this:
www.eclipserecords.com/forum/erc_artist.php?aid=3

I toughtthat this should have worked, but apparently doesn't...

RewriteEngine On
RewriteCond %{QUERY_STRING} aid=(.*)
RewriteRule ^artist.php\?do=a\&aid=([0-9])(.*) /forum/erc_artist.php?aid=$1


Anyone able to tell me what I am doing wrong?
Thanks in advance smiley for :)

You cannot test for particular query strings inside a RewriteRule. Use a RewriteCond, instead, as explained above. ;o)



pedro - 22.03.12 10:15 pm

Hi Cor (& everyone else),

This is the greatest mod_rewrite page on the web by far. I was hoping for a new pair of eyes with the following:

I'm trying to display in browser http://domain.com/view/myfile
when a user requests http://domain.com/index.php?view=myfile

I really just want to simplify and mask the url rather than redirect to anything

My rule is:
RewriteEngine On
RewriteCond %{QUERY_STRING} view=(.*)
RewriteRule (.*) /view=?%1

But it outputs:
http://domain.com/view/?myfile

I thought this would be easy but it's not proving to be real pain.

Hoping someone can point me in the right direction.

Thanks in advance,
Pedro

You were close, try:
RewriteRule .* /view/%1
;o)

ps. it does seem kinda back-to-front, though. Normally people want users to click (and see) "pretty" links and have them translated transparently to uglier query-string type links. But each to his own..


Thanks for that Cor. It's still a bit wonky but I can work on it some more.

If I did it the other way round with the prettier links (how I had it initially), is the P flag the only way to get the links to stay in the browser address bar? This caused huge issues when these links were acted upon further (calling APIs on the pretty link).

Thanks again,
Pedro

The idea is, "pretty" links are ONLY for the users. Ideally, you get your back-end (CMS, whatever) to spit out the /view/myfile link on your page. The user clicks that and is taken to (what APPEARS to be) /view/myfile, which is what stays in their address bar.

In the background (.htaccess) the link has been rewritten to /index.php?view=myfile without the [R] flag (no redirection, no change in address bar).

Generally, one would use something more like this:

RewriteRule ^view/(.*) /index.php?view=$1 [NC]

;o)



Tamas Benke - 23.03.12 10:02 am

Hi Cor,

Thanks for the article!
Do you have any advise how to specialize the redirection of two urls with one letter difference?

I mean I have two urls "aperitiv" and "aperitive" and I use this to redirect "aperitiv" to "aperitive":

RewriteRule ^/aperitiv /aperitive/ [R=301,L]

But it leads the url into an endless loop, I tried methods from your article but I failed.smiley for :ehh:

Thanks in advance
Tamas

You must have missed the bit about Anchors. All you need to do is add a "$", like so:

RewriteRule ^/aperitiv$ /aperitive/ [R=301,L]

;o)


Great, thanks!


Andy Jones - 25.03.12 12:05 am

hi Cor

Brilliant site it has helped me loads with some neat .htaccess tricks and i ahve just downloaded some of your php scripts to use on my own personal site. would it be possible to get a copy of your logo so i can put you on my sponsors links on my personal site ? as i think this site can provide visitors of my site some nice info on .htaccess and the likes smiley for :)

Once again thanks for all your info it has taught me some nice .htaccess and hotlinking tricks smiley for :)


wssrpnc - 25.03.12 12:51 am

Hello guys,

I have a problem, i don't know which example should i choose.

In my Wordpress site i want to redirect all pages to folder. The structure is like that: root/folder/pages/ (they are without any extensions visible)

I want that all pages to be redirected to root/folder/

Any idea please which example should i take?

Thanks


Chris - 29.03.12 7:22 pm

Hi Cor,
Thanks for your response above. I've corrected the rule to look like this (which does the trick):

RewriteEngine On
RewriteCond %{QUERY_STRING} aid=(.*)
RewriteRule ^artist.php(.*) /forum/erc_artist.php?aid=%1 [R=301,NC]

It accomplishes my goal, which is to redirect the input url:
www.example.com/artist.php?do=a&aid=3&am=bio
to
www.example.com/erc_artist.php?aid=3

The only problem I would like to fix however is that it actually rewrites it like this:
www.example.com/erc_artist.php?aid=3&am=bio

Where has /forum/ gone? Is this intentional? Is the rule you posted the one you are actually using?


Since the 'am' var is no longer needed in the new query string, how would go about passing ONLY aid=3 and not the other unnecessary stuff (&am=bio)?

Thanks smiley for :)


If you know that it will always be there, you can do:
RewriteCond %{QUERY_STRING} aid=(.*)am=(.*)
RewriteRule ^artist.php /forum/erc_artist.php?aid=%1 [R,NC]
Otherwise, don't worry about it, simply ignore it in the target script.

;o)

ps. note, I also removed your redundant (.*) from the end of the test string.



Andy Jones - 30.03.12 8:52 pm

hi corz

having problems with your anti hotlink htaccess code :
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?andrewneiljones.co.uk\. [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(.+)\.google\. [NC]
RewriteRule ^(.*)\.(gif|jpe?g|png)$ http://%{HTTP_HOST}/path/to/hotlink.php?url=$1.$2 [R,NC,L]

it seems to strip all my images off my site and doesnt even generate anti holink img i have gd library installed and working

the script doesnt seem to work at all it just blocks all images on my site

The script works fine. It is your implementation of the .htaccess code which is flawed.

This is because you have added an actual dot "\." to the end of your referer RewriteCond, for some reason - I'm guessing you copied the code from my blog and inserted your full domain, including TLD, by mistake (use either "yourdomain\." OR "yourdomain\.TLD", but not both).

You might also want to double-check the /path/to/hotlink.php
Is that really the path?

;o)



Martijn - 05.04.12 1:52 pm

Hi, tnx for all the useful information.
I've got the following setup.


RewriteEngine On
RewriteBase /
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]

RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ index.php?/$1 [L]

RewriteCond %{HTTP_HOST} ^m\.domain\.com [NC]
RewriteRule ^(.*)$ index_mobile.php?/$1 [L]

IndexIgnore *


When I go to m.domain.com it still goes to index.php but when I go to m.domain.com/whatever it grabs index_mobile.php. So it works but not when first accessing the site.

Any ideas?

Not really. It should work as-is. You might want to take a look at your DNS setup in your hosting panel, ensure everything is correct there. ;o)



Foujil Ameen - 09.04.12 10:57 am

Hi Cor,

I have doubt for .htaccess the rewrite the files or using main domain file.
How i can call the file sub.mydomain.com to mydomain.com ?

RewriteEngine on
RewriteCond %{HTTP_HOST} subdomain.mydomain.com
RewriteCond %{REQUEST_URI} !^/home/public_html
RewriteRule ^(.*)$ home/public_html/$1 L

give me solutions

Thanks & regards
Foujil Ameen.A


surya - 12.04.12 9:49 am

On one of the top websites displayed Browser URL - http://www.example.com/start-your-own-business.html will show the contents of the page on http://www.example.com/business/start-a-new-business/start-your-own-business.html.

This is great because it has shorten the URL by hiding two directories.

However, if you type the long URL http://www.example.com/business/start-a-new-business/start-your-own-business.html directly on the browser then it will show error page. Which means there is no duplicate content.

Also when you type the short url on the browser http://www.example.com/start-your-own-business.html you will see the breadcrumbs as home/business/start-a-new-business/start-your-own-business.html, Which shows that actual directory structure on the server side is not touched at all to shorten the URL. It is just the browser display that has changed to shorter URL.

That is great!

How can we achieve this kind of URL shortening by hiding folders through .htaccess ?


See above! ;o)



David Spalding - 14.04.12 12:45 am

I've got what seems a unique use case -- judging from search results -- that I hope you can help with.

I have custom 403, 404 error pages:
ErrorDocument 403 /403.htm
ErrorDocument 404 /404.htm
ErrorDocument 500 default


I want to catch certain flagrant user agents and give them the ol' 403 heave-ho. This works:
# block known spam collecting bots, and retrievers
RewriteCond %{HTTP_USER_AGENT} ^$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^Java [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^\\$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^PHP [NC]
RewriteRule .* - [F,L]

This issues a 403 just fine, but then with HTTP1.1 a 500 error "trying to find a 404 document." Well, clearly, if the user_agent is blocked from getting anything, they can't be given a special 404 error document on the same domain. I can redirect to a special "Get bent!" page, but I'd like to return a 403, not 200 or 301. The only thing that seems to work is
ErrorDocument 403 default / ErrorDocument 404 default

... which makes me sad. No custom error pages. :(

I've tried various combinations of <files 40?.htm> (order deny, allow / allow from all) and <FilesMatch> sections to allow anyone get those specific error pages, but these all seem to result in 500 errors, or just don't work. Am I missing something obvious like the heffalump in the room, namely if you give a 403 to someone you cant' mitigate it? TIA...!

Nope, you called it.

A better solution is to rewrite the request directly to a custom 403 page with sends its own header. Note: the upcoming version of Anti-Hammer now deals with user agents, IPs, dodgy requests and much more (as well as the hammers) and takes care of this automatically.

In the meantime, do something like (in .htaccess)..
RewriteRule . err/403.php [L]
And then in your 403.php..

<?php
header
($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
header('Status: 403 Forbidden');
header('Connection: Close');
echo '<PAGE HERE>';
?>

;o)

ps. and don't forget, you can do..

ErrorDocument 403 "Feck off!"



Olivier - 21.04.12 3:52 pm

Hello, I use that rule
RewriteRule ^job/([^/]+)/([^/]+)$ job-$1.php?j=$2 [NC]

In my "job-1.php" page, which is well called, I use images and includes which don't work anymore...
...seems the "base" has changed...

Any help would be appreciated!

Oli

See the notes directly above these comments. ;o)



Ree - 02.05.12 10:48 pm

Hey Cor

Referring to your "multiple domains in one root" example:

#two domains served from one root..
RewriteCond %{HTTP_HOST} domain-one.com
RewriteCond %{REQUEST_URI} !^/one
RewriteRule ^(.*)$ one/$1 [L]

RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URI} !^two
RewriteRule ^(.*)$ two/$1 [L]

If you go to domain-one.com/two you can get to the 'root' of the second site.

Do you have any thoughts on getting around this?

Thanks. R

As the example is written, that shouldn't happen. You should get "404: The requested URL /one/two/ was not found on this server". Unless you have a symlink back to the second root, or have altered the paths in some other way.

;o)



PC - 06.05.12 3:55 pm

You can shorten your "lose-the-www" rule a little, as the entire string is automatically matched and referenced by $0:

RewriteCond	%{HTTP_HOST}	^www\.(.*)	[NC]
RewriteRule	.*		http://%1/$0	[R=301,NC,L]

There are a few examples that could be shortened, often they are written as much for readablity as absolute efficiency - which is why there's nothing like the exmple from a few comments up: "RewriteCond $1..." - mod_rewrite beginners have a tough enough time!


If you happen to use https connections (maybe mixed with unencrypted ones), consider expanding it to handle all cases:
RewriteCond	%{SERVER_PROTOCOL}	(.*)	[NC]
RewriteCond	%{HTTP_HOST}	^www\.(.*)	[NC]
RewriteRule	.*		%1://%2/$0 [R=301,NC,L]

That's nice work! Would make a fine addition to the page. I may use it myself! ;o)



Percy - 17.05.12 10:41 pm

I'm having a really tough time with my wordpress install. So I decided to use mod_rewrite to solve it, but after dealing with it for a week, I'm hoping you can help me out.

I'm trying to figure out what to put into my .htaccess file to translate 2 types of requests I'm getting..

http://foo.com/?/bar/ should be written as http://foo.com/bar

and if sometimes I'll get a url that will be:

http://foo.com/bar/none where I need to eliminate the "none". So it should be translated to: http://foo.com/bar/

Obviously, in both cases, "bar" can be anything. Do you mind helping out? I'm more than happy to donate.

I think what you mean is that you want users to interact with URL's that look like http://foo.com/bar and have them translated in the background to http://foo.com/?/bar/. This seems slightly weird (why not http://foo.com/index.php?var=bar?) but something like this will achieve that result..
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]*)/?(.*) /?/$1/ [L]
If you mean something else, let me know.

;o)



James - 18.05.12 4:53 pm

Hello Cor,

I have enjoyed reading this article, you have given a lot of valuable information. I am not strong on the code side at all and was wondering if you could help me with a question. A few months ago i had a bunch of bad links point to the non www version of my site and I feel that most of them did more harm than good. I am now currently trying to rebuild this site on a new domain. What I am trying to do primarily though is break the links coming to the page that someone bowled me with. So I want to return a 410 page gone first on the non www then do a 301 to only transfer the links left on the www version along with the other pages on the site. I do not even know if this is possible. So far I think the 301 just seems to transfer everything and it ignores the 410 so far, which then passes the penalty to the new domain eventually. I know you have no reason to help me but I am pleading at the good mercy of your heart =) Any help you can give is most appreciated.

Respectfully,
~James

It's usually best to first decide on a canonical scheme (with or without "www") then redirect all requests using the other format to use the correct format. Put this near the top of your .htaccess, an external rewrite as in the main example.

Then (and I do mean after) redirect all your incoming links to their correct pages. Unless the site sending the links is a "bad" site, there's no harm in getting those links to their destination ASAP, if their is a destination. And if there is, a 410 response is inappropriate.

But again, I may not fully understand what you are trying to achieve. If you can do more to explain that, please do.

;o)



Marc - 21.05.12 3:15 pm

Hello Cor!

I am running WAMP on a local server idswell, with it I have a dyndns setup, so clients can view development work on the local host. But when it comes to WordPress development is where the wheels come off.

The WordPress URL for development & in the WP database is for example: localhost:99/website, but to the client will be for example: dyndns.com/website, but because the architecture of WP it will only accept localhost....

I'm no WP expert, but I find this highly improbable, or else the millions of WP site out there could not function.


Is there a way via htaccess to change the URL from dyndns ... to localhost... to bluff WP that the domain is in fact localhost...

Have tried all the basic htaccess syntax wrt to redirecting etc but still have not got it working!

Any ides?

Regards

Marc

It sounds like the trouble is in the DNS somewhere. WP shouldn't care what domain it's being served from, unless there is some rewrite rules specifying "localhost", which again sounds unlikely. I think you need to look elsewhere for your trouble.

;o)



John - 03.06.12 1:26 pm

Thanks... the logging of mod_rewrite helped me fix the problem I was having. Very valuable.


Chris Kurisu - 06.06.12 10:14 pm

Wow. The best article on this topic, by far! Easy to understand and enjoyable to read. Thank you.

I also read with interest the part related to rewriting urls from www to the base url. I have the opposite need, to rewrite base urls to include index.php (if they don't already, that is).

Which is definitely NOT the opposite of the "lose the www" rule!


I've crafted the following:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} domain\.org [NC]
RewriteRule ^(.*)$ http://domain.org/index.php/$1 [R=301,NC]

I ask optimistically, am I good to go?

It depends what you are trying to achieve. Your rules are problematic. And please, NEVER use a 301 redirect when testing rules!! EVER! Please close your browser, restart your system and try again using plain [R] when you are 100% certain everything works, THEN switch to 301. I explained this in the main article.


My fear is that requests for
http://domain.com/index.php?view=myfile

would be rewritten to
http://domain.com/index.php/index.php?view=myfile

Yes, that is exactly what would happen, or more likely, http://domain.org/index.php/index.php/index.php/index.php/index.php/ ... and so on. As well as catching requests for images, CSS and everything else. Probably not what you want.


Thank you again!

Okay, let' assume that you are trying to remove the "www". Copy the rules from the main aticle, they work fine.

Next, you say you are trying to catch all requests that don't contain "index.php".. and ensure they do, creating a PATH_INFO parameter out of the original request. Something like this will work better..
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [R,L]
The first two lines check that the requested resource doesn't actually exist (including requests for index.php itself). It's slightly wasteful, but required for your catch-all rule. The RewriteRule catches all other requests and directs them to /index.php/*something*.

*something* will then be available as your PATH_INFO variable.

If you don't want the new address to show up in the user's address bar, remove the [R] flag.

;o)



Saji - 08.06.12 10:30 am

Hello Cor,

Its awesome support!

We have a requirement that to download zip and video files from the site, but I want to hide the original path of the file and need to validate the user login before download start then only user can download the file.

I am using codeigniter for development.

The .htaccess content of the codeigniter is as follow,

Options +FollowSymlinks 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond $1 !^(index\.php|style|js|images|uploads|cron_task|robots\.txt|tmp|user_guide)
RewriteRule ^(.*)$ index.php/$1 [L]
Could you please help me with this.

Thank you so much..

Thanks,
Saji

Check out the distro machine. ;o)



Guy - 22.06.12 4:38 pm

Hi Cor,

great explanation! Still I have an issue that I can't resolve, I tried a few things but then gave it up, cause my site was mostly broken...

could you help me a bit please?

I have a site with multiple languages (Joomla). Currently it may also store the chosen language selection in a cookie, but I dont know for sure.

Anyway it also shows the chosen language (mostly) in the url.
The URLs normally are index.php? + parameters, but are translated in SEF URLs using an extension called sh404sef, which settings I could adjust a bit, but not enough to get usable results...

Page default language is german (de) and second language is dutch (nl)
I do suppose though, that Joomla might be checking where my browser IP is located geographically (Germany)

I have 2 domains pointing to that site: www.mydomain.be & www.mydomain.de

So now, if I visit www.mydomain.de or I visit www.mydomain.be it allways starts with my default language (german)

If I switch from german to dutch, then in the url there appears a path extension /nl/ for dutch and /de/ for german, although it is possible to visit www.mydomain.de/de/content/page.html even if www.mydomain.de/content/page.html is the same content. (double content!)

I want to achieve that:

1. www.mydomain.de/nl/ goes to www.mydomain.be/ (+path, but no /nl/)

2. www.mydomain.be/nl/ goes to www.mydomain.be/ (+path, but no /nl/)

3. www.mydomain.de/de/ goes to www.mydomain.de/ (+path, but no /de/)

4. www.mydomain.be/de/ goes to www.mydomain.de/ (+path, but no /de/)

So that in the end somebody visiting www.mydomain.be only sees the dutch content, and somebody visiting www.mydomain.de only sees the german content... and avoiding double content...

many thanks in advance!
Guy


john - 02.07.12 4:03 pm

any idea how can i redirect everyone based on their IP but except a subnet (allow anyone from 193.193.*.* but redirect all others )?


Vandhana Srivastava - 26.07.12 9:25 am

Hi..
Very informative tutorials ..
This is the best mod_rewrite information on the internet.
Please keep posting.


Roms - 26.07.12 3:07 pm

Hello,

first of all thanks for theses ressources

In the section cookies, how would you do that ?

If %{HTTP_HOST}=='www.mywebsite.fr' then
session.cookie_domain www.mywebsite.fr
Else
php_flag session.use_cookies off
# because the other subdomain concerned is a media one for .jpg, .css, .js,...
End IF

Thank you for your help


gawrrell - 05.08.12 11:55 am

Hello,

First of all thanks for the tutorial.

I have a small problem with the htaccess redirect method:

Lets say the user type this into his browser: http://domain.com/77.36.64.123
I want this to be redirected to: http://domain.com/client/index.php?ip=77.36.64.123

I also want to verify that this 77.36.64.123 is a IP like string. I mean the string "77.36.64.123" should have 4 numeric blocks, each block has min 1 max 3 numbers from 0 to 9, also the blocks are separated by dots "."


Could you help me?


Master DJon - 26.08.12 6:25 am

@gawrrell
I wanted to train myself a bit. Here is what could fit your needs :
RewriteCond %{REQUEST_URI} /.*/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$
RewriteRule .* http://www.google.ca?s=%1 [R,NC]

@Cor
Really great page ! I recently used the multiple domains on one trick, but I'm not sure I got the solution on your site at that time. Even though, I pursuit in htaccess modif and it is how I found your site when I got into troubles smiley for :) But I'm not writing you about these that brings me here, but old ones. (The first ones being functional now)

So, as I said at first, I used the multiple domains trick. It works perfectly except for one detail one my subdomains. Those are managed via cpanel.

So, typing :
sub.domain.com works correctly and this is in fact located at domain.com/sub which is in fact maindomain.com/domain.com/sub. So, my first thoughts were that only the first address would works. Mmm.. Here's what I got :
domain.com/sub/ = sub.domain.com (which is OK and expected)
domain.com/sub --> domain.com/folderUsedForDomain/sub (which is also working as both above, but not desired !)

So, not having a slash for a sub domain --------------- oups, just tested with any directory and it does the same thing.

Finally, adding a slash to a directory gives the expected result, without, it adds before it the domainNameFolder. I tried different things, nothing does it.

Here is one domain :
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(domain1.net|www.domain1.net)$ [NC]
RewriteCond %{REQUEST_URI} !^/domain1.net/
RewriteRule ^(.*)$ /domain1.net/$1 [L]

Without the request uri condition, it ends at the same address, but it shows a page saying that /domain1.net/domain1.net/sub doesn't exist.

I really can't see how to fix the problem, can you ?

Hoping not having obfuscated you by answering to one of your reader.


daniel - 27.08.12 2:42 pm

•Hello,

this is my .htaccess
RewriteEngine on
RewriteBase  /
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /html/index.html?main=$1&sub=$2&action=$3&project=yes [L]
RewriteRule ^([^/]*)/([^/]*)$ /html/index.html?main=$1&sub=$2&project=yes [L]
RewriteRule ^([^/]*)$ /html/index.html?main=$1&sub=$2&project=no [L]

Everything works fine. And the Links looks like

domain.nic/news/030412

But how do i submit moore variables if nessesairy?

domain.nic/news/020412?user_id=200

Hope you can help me smiley for :)

Thanks…


suma - 11.09.12 12:56 pm

hi,
my style sheet is missing when iam using the rewrite rule,
is there any way to get my style sheet without using full url?
my code
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php?category=$1&subcategory=$2


Yvan - 14.09.12 12:34 am

Thanks for the site, even though I find it very difficult to understand.
I have to say that the foo and bar variable name are mixing my mind.

Why coder prefer foo and bar instead of clear name is a mystery to me.
It would be so easier to understand, anyway I am probably to dumb.

I will keep trying until it works, because I want to use these on the meditation website I am building.
As you can see, I don't ask any question.
I prefer finding my mistakes and share my results.
If you don«'t mind.

Again, thank you for your site


Salma Noreen - 26.09.12 8:37 pm

I want to rewrite my site links but unable to get the exact code.

<?php
For rewriting dog-name-info/?id=2 to dog-name-info/buddy.html I have used following code.

[
block][sm][b]RewriteRule ^dog-name-info/([a-zA-Z0-9_-]+).htmldog-name-info/?id=$2[/b][/sm]
[
coderz]RewriteEngine on[/coderz][/block]

but nothing happened on mysite.com/dog-name-info/?id=2
?>

smiley for :erm:©


Dave - 03.10.12 7:33 am

Great article. Have a quick question. Say I have a site and I make a rewrite rule to make

domain.com/index.php?page=services

to

domain.com/page/services

In my pages do I link to the proper php page, or link to the clean version of the url. If I don't have to change all my webpage links to the clean version, when I hover over an insite link will it show the clean version, or the proper version in the status bar?

Thanks
-Dave


ken - 05.10.12 5:07 am

Cor,
Others stroke themselves on being an htaccess guru, but your info is the tops! Great work and thank you for your time.

[Yvan]
Wikipedia foobar for an explanation. In short, it's a programming-thing. (Students are generally introduced to foo bar in CS100 or similar.)


Sanjeev - 08.10.12 7:44 pm

Hello,

I could really do with some help on something I have been working on for some days with little knowledge of mod rewrite.
I have the following url:
http://betatest.bracknell-forest.gov.uk/help?fb_action_ids=372043216205703&fb_action_types=og.likes&fb_source=aggregation&fb_aggregation_id=288381481237582

I want to strip off all the query parameters to get
http://betatest.bracknell-forest.gov.uk/help
and return the page.

This is my code:
I already have code that returns a clean url:
# Any bare URL will get rewritten to a URL with .htm appended
RewriteRule ^/([\w]+)$ /$1.htm [I,L]
RedirectRule ^/(.+)\.(htm)$       http://betatest.bracknell-forest.gov.uk/$1 [R=301]

and I am trying to add the following condition:
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} ^fb_action_ids(.*)
RewriteRule ^/(.*)$ $1? [R=permanent] #remove query string

RewriteRule ^(.*)$ $1/ 
RewriteRule ^(.)?(.*)$ /%{REQUEST_URI} [QSA,L,R=301]  

The trouble is my first url that comes from facebook does not have .htm and I think this is what breaks everything else but I seem to have got into a right muddle.


vinik - 27.11.12 5:18 am

nicesmiley for :erm:


Alex - 08.12.12 9:14 pm

Hi,
need help, getting frustrated with htaccess rewrite rules.
have been testing my htaccess code via http://htaccess.madewithlove.be/ but still not doing what i want.

I would like to change my url, say : http://domain.com/?cp=11 into http://domain.com/gg-11.
my htaccess code:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.*)/?cp=([0-9]+)$ /gg-$1 [[R=301,L]]


please, can anyone please advise me or guide me on how to fix it?


Master DJon - 09.12.12 5:45 am

Hi Cor !

Finally, I have been able to fix my problem and I'd like to share you my solution.

First of all, the summary of the problem. I'm using the multiple domain trick though when using an address http://domain.com/folder without an ending slash, then I was redirected to http://domain.com/folderOfDomain/folder. Which wasn't the case with the ending slash.

Here was the old code :
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteCond %{HTTP_HOST} ^(domain.com|www.domain.com)$ [NC]
RewriteCond %{REQUEST_URI} !^/domain.com
RewriteRule ^(.*)$ domain.com/$1 [L]


I realised that using the first condition made the POST requests to fail. So I removed it, but even though, my problem still exists. So I decided to add another redirection using this removed condition with the opposite of the REQUEST_URI condition.

Which finally makes :
RewriteCond %{HTTP_HOST} ^(domain.com|www.domain.com)$ [NC]
RewriteCond %{REQUEST_URI} !^/domain.com
RewriteRule ^(.*)$ domain.com/$1 [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteCond %{REQUEST_URI} ^/domain.com
RewriteRule domain.com/(.*)$ $1 [R]


This fixes the folder problem without a last slash plus it counters the possibility to have the address http://domain.com/domain.com

If you have any suggestion for amelioration, I'd like to receive them. Otherwise, you could maybe update your trick to include this "counter" trick.


pcman - 02.01.13 9:44 pm

hey was looking at wordpress. how does it do permalinks?

do the links change from ex- index.php to HomePage in the php code itself?

i guess i am wondering would i have to hard code the links to work as permalinks.

can i use php in the htaccess file? some of the code looks similar.smiley for :lol:


Paul - 18.02.13 6:34 pm

Hi,

I use
RewriteCond %{HTTP_HOST} ^(example\.com)(:80)? [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]

to redirect non www to www......

is there a way that i can get it to ignore this rule for the url

example.com/index.php?option=com_pmform&task=payment_confirm&payment_method=os_worldpay

I hope you can help


Pete - 20.03.13 2:22 am

Hello,

Can you tell me the trick/code to display 404 Error Page through .htaccess for the files that I do not want the users to see, but are available on the server for internal use?

Regards,
Pete


Ashish Kushwaha - 19.05.13 5:32 pm

Any One Please Help me..

How to restrict Addon doamin access to main doamin ?

I try To more code by searching on Google but no one work..

please tell me Right tested code...

Email : designerguptaji@gmail.com




hmm - 20.07.13 1:58 am

Now I'm on maindomain.com

I want to go to subdomain.maindomain.com
At the same time I want to keep maindomain.com in the address bar. Is this possible?


suge - 16.08.13 4:39 pm

Hi Cor,

just gave your Auto-Hotlink script (beta) a try - great idea!

Unfortunately it didn't work out-of-the-box due to usage of PHP 5.4.

Two lines needed to be patched (use NULL instead of ""):
imagepng($auto_hotlink['img'], NULL, $auto_hotlink['compression_level']) or die("there was an error. sorree");

and
imagejpeg($auto_hotlink['img'], NULL, $auto_hotlink['compression_level']) or die("there was an error. sorree");


Just my 2 cents
Gerd


Yeah, all my image scripts will currently bug out on newer php versions. Fixes soon. Thanks php team! ;o)



Mukesh - 02.09.13 2:36 pm

i have installed wordpress on server and in that wordpress i installed a vbulletin. nw i want that when i type mydomain/forum then it will redirect me to the vbulletin folder. i m suceeded in this bt the url is not same i.e. /forum. it changes to mydomain/vb/activity.php. i want that it remains mydomain/forum and shows me my vbulletin page ... plz help me out !!!!

Thnks in advance


sreekanth reddy ila - 22.09.13 2:06 pm

Hi:

Thanks a lot, its look great tutorial.


bernard - 24.09.13 10:00 am

Nice tutorial !! Thanks...


David - 02.10.13 6:14 pm

I know it's been a while since folks have posted here, and understand my odds of receiving a reply are practically none (based on the site owner). However, I have searched everywhere for this answer and can't seem to find it. Perhaps because it just can't be done.

Anyway, here goes...

Is there a way to eliminate the "index of/" text which automatically shows up above our directory listing? We offer PDF file (forms) for download, and have a directly listing them all using an .htaccess file. However, along with the listing it displays a header which looks like this ..

<standard Apache fancy indexing screen-cap snipped>

Just wondering if there's a way to remove (or hide) the "index of/" and just have the directory listing.

As a bonus, I'd welcome if the "Size" and "Description" columns were gone too, but I welcome whatever help I can get. smiley for :roll:

Thanks for considering.
David

Stuff like this belong in part one. The answer is there, too. (link above these comments) clue: you want Options +Indexes, but without fancy indexing. ;o)



David - 02.10.13 7:36 pm

Wow. My apologies for my ignorance. I pasted the following code on the bottom on my htaccess file and it killed the directory

<standard Apache fancy indexing screen-cap snipped>

I'll keep looking to see if I find the command your referencing.

Thank you,
David


David - 03.10.13 6:07 pm

Can someone please provide the code needed to eliminate the "Index of /" header from my directory listing? I tried searching everywhere here, also cutting and pasting what I believed was the answer to no avail.

Please help. I'll gladly donate using PayPal if someone can help me with this ordeal. :-(

Thanks,
David smiley for :ehh:

If you want to disable indexing, check out part one of this fine article set.

If you want directory listings, but with your own custom text, you will need to use a custom header in your indexes (again, part one), or use some kind of custom directory lister, like this.

;o)



Rolf - 17.10.13 2:26 pm

Hi Cor
Thanks for a great resource! Find these explanations very clear and easy to follow. Was always hesitant to play with .htaccess and let support do it for me. Now, I've found a few examples that are well explained. Could easily reproduce it on my account. Keep your address on file in case I get stuck and need expert (paid smiley for :lol: ) help!
Rolf



Gilberto - 13.11.13 3:42 am

Hi Cor

Excellent tutorial, I read it first about 3 years ago but now I'm re-reading it and I've noticed something I didn't in my first read. I think the explanation of the "!" sign is missing, I understand it because I know PHP. I also know that it might be overkill to explain that because most people that will read this article sure are experts with other languages but it would be a nice add-on for beginners like I was some years ago =)

Cheers.
Gilberto Gomez


Good catch! I'll add something. ;o)



Jorge - 22.11.13 1:30 am

Nice site


Mukera - 28.11.13 10:01 pm

Thank you!smiley for :D


Ellie - 26.06.14 5:57 pm

What's the way we can strip extensions (www.mysite.net/about.html and final slash symbol "/" too) aWd make necessary redirect, just in case of url misprint like (www.mysite.net/about.htm | php or just / (slash)) - to avoid Error pages ?

Don't misprint URLs and do use error pages to help you catch your errors! ;o)



osas - 07.07.14 6:53 pm

Please i'm having issues with this .htaccesa!

i would like to convert this url (http://localhost/tvnollyblog/pgcnt.php?pg=Nollywood_Awards_and_Pageantry_)

To something like (http://localhost/tvnollyblog/blog/Nollywood_Awards_and_Pageantry_)


Oh! I know a great article all about this stuff! Let me see if I can find the link.. ;o)



Tony T - 27.08.14 3:16 am

Fascinating, I was only looking for the simple redirect and now I have a great resource for the time I become both more proficient and more demanding.

BUT, a suggestion regarding language translation, please don't include anything that will look up where you are and give an automatic translation. It's so annoying! I'm in Austria and my German is minimal. With some sites I end up using a proxy to disguise where I am!

Thanks for sharing your efforts in getting to grips with this.

Tony

My own translation links (in the footer) offer a choice of languages which should, in theory, give you what you ask for, regardless of location. As well as one that tries to work it all out automatically. It all depends on how the web master implements it. ;o)



Naresh Sharma - 04.09.14 6:52 pm

hello dear
I have a problem regarding htaccess path actully i want to add 404 page in my website for this created 404.php file and given the path in .htaccess is 'ErrorDocument 404 /404.php' it is work good but after some time it('ErrorDocument 404 /404.php') is being remove from .htaccess file so it is not redirect the custom file. please give me suggetions.


It sounds like you need a new web host.

Note: it may be possible to add custom error documents through your host's control panel.

;o)



Linda Cox - 13.09.14 6:42 am

I have a related question which I've asked here: http://stackoverflow.com/questions/25766014/rewriterule-in-htaccess-stoped-javascript-in-address-bar

I hope you can give me a useful solution.


Related to what? ;o)



Ivan - 20.09.14 4:17 pm

Hi

Great article.

What if i only want the main url of the site to be shown. What do I need to do?

So only www.mydomain.dk gets shown even if the actual url maybe is www.mydomain.dk/somecontentsname


There are a few ways to do this.

One would be to capture all requests and rewrite them to an index.php (after rewriting to the bare domain) which included/loaded files based on the captured, prettied, user-filtered query parameters.

I would warn against doing this, however. It is deceptive, for starters. If a user clicks a link (which their browser displays as "/some-page"), they expect to get "some-page".

Users can't bookmark pages inside your site. Some will feel slightly lost. Not recommended.

;o)



pradip vanparia - 18.10.14 8:19 pm

hello.

in .htaccess file below page not access.

when i click on my account link.

RewriteRule MyAccount/$ ?do=my_account

so. how i can solve this error ?

thanks..


You need to target an actual file..

RewriteRule MyAccount/$ index.php?do=my_account

;o)



abhay - 27.10.14 12:40 am

I have a stupid question. how to redirect

example.com/hi/iam/waiting.html
to
yoyo.com/hi/iam/waiting.html


thanks in advance.


Snide - 05.11.14 7:33 pm

Hi,
I used WAMP since yesterday, and I'm gone to LAMP...
But my .htaccess rewriting rules doesn't work as it was before...
If someone can explain where is the config problem...

Here are the description :

my site is http://www.mysite.com/

I have these files in /var/www/root:
index.php
action.php


I have this rule :
RewriteRule ^(.*)-([0-9]+)\.html$ /index.php?id=$2 [L]


All is working good when I call http://www.mysite.com/page-17.html.

I have this other rule :
RewriteRule ^action/([0-9]+)/(.*)$ /action.php?id=$1&name=$2 [L]


It doesn't work when I call http://www.mysite.com/action/25/snide

After tracing with RewriteLogLevel 8 I see this:
add path info postfix: /var/www/root/action.php
strip per-dir prefix: /var/www/root/action.php/25/snide


and then, the action.php is executed... but cannot use parameters!

thanx for any help !


Snide - 05.11.14 7:55 pm

rahhhh
Always finding solution after asking for help...
So, if anybody got this problem like I did... Here is the solution!


In your .htaccess, add this :
options -MultiViews


Answer for abhay...
Try this.
RewriteCond %{HTTP_HOST} ^www\.exemple\.com
RewriteRule (.*) http://www.yoyo.com/$1 [R=301,L]



cheers smiley for :)


Simon Vetterli - 19.11.14 12:10 pm

Hi Mukera

I still not get it trough all the htaccess things:

My follow issue is:

I have a multiglingual website, at the moment 4 different languages (en, ge(de), fr and ru):

My file structure are follow:

index.php
openwaterdiver.php
discverscubadiver.php
rescuediver.php
contact.php
aboutus.php

and so on.

My link structure is the follow:

index.php?lang=en for english
index.php?lang=ru for russin
index.php?lang=fr for french
index.php?lang=ge for german

Once I have the variable lang - it will show the correct content - each language.

Eg the actual link for my domain: http://www.example.com/ directs to english, or
http://www.example.com/index.php?lang=en

I like to have my follow link structure for SEO friendly url's:

http://www.example.com/en/ -> http://www.example.com/index.php?lang=en
http://www.example.com/en/openwaterdiver/ -> http://www.example.com/openwaterdiver.php?lang=en

http://www.example.com/en/rescuediver/ -> http://www.example.com/rescuediver.php?lang=en

each language.

In a htaccess file I have the follow, but it not works. It gives me an 404 error:

RewriteEngine  on
RewriteRule    ^/([a-z]{2})/$  /index.php?lang=$1
RewriteRule    ^/([a-z]{2})/([a-z]+)$  /$2.php?lang=$1


What should I do, that it will work correctly?

Need some help, please.

Kindly Simon

As well as adding an erroneous forward-slash at the start of the rules (that's only for Apache config files like apache.conf, remember!), you anchored it with "^". Double-Whammy! So that's the first problem. Remove the errant forward-slashes.

The next problem is the missing forward-slash from the end of the second condition, so it was capturing and looking for "rescuediver/.php". Try something like this, instead..

RewriteRule    ^([a-z]{2})/$  /index.php?lang=$1 [L]
RewriteRule    ^([a-z]{2})/([a-z]+)/$  /$2.php?lang=$1 [L]

And maybe read the article (above)!

;o)



Wilfried Rutjes - 27.11.14 4:52 pm

Hello,

I see examples of changing parts of the url that is in the beginning of the url, I want to catch situations where someone enters a url like this

http://proces
and I want that to be changes to
https://proces.intranet.example.nl

So I want it to change to https and I want to add the whole domain name.
http://proces works because of the fact that it is an internal website.

Hope you can help me.

Kind regards,

Wilfried

You will need to add some code to the root .htaccess of whichever server http://proces leads to, something like..

RewriteCond %{HTTP_HOST} ^proces$
RewriteRule	(.*)  https://proces.intranet.example.nl/$1 [R=301,L]

;o)



Wilfried Rutjes - 28.11.14 3:44 am

Hello Cor,

It works like a charm. Thanks alot!!

Kind regards, Wilfried


Elam - 06.12.14 6:45 pm

Hi,
This is a great stuff, a novice like me can also get some understanding about. I have a url as below:

www.example.com/sres.php?country=cntry&city=cty&category=cat

I would like to place it as below

www.example.com/cat in cty

Appreciate your help sir.

I can't help with your code, because you didn't post any, but I can advise strongly against using spaces as delimiters in URLs. Firstly, in the real world, they translate to "%20", e.g..

http://www.example.com/cat%20in%20cty

That's what users will see in most places (of the browsers I regularly use and test on, only Firefox will show spaces as actual spaces, which I personally think is a mistake), so there is little visual benefit. It will negatively impact transportability, readability, and more. At this time, there is too much potential for mishap.

Instead use a simple "-" which is one character vs three when using a space. In your particular example (city in country) I would recommend using a directory structure appearance, e.g..

www.example.com/cty/cat/  or..  www.example.com/cty/cat

Code for this sort of thing is up in the main article.

;o)



alice - 22.12.14 10:09 pm

Ciao Cor,
Maybe you can help me!
i have a new web browser which is in a subdirectory, let's say dir2. I want the user can link to the root of the website, let's say www.example.com and get the content of dir2.

I wrote this
RewriteEngine On 
RewriteBase / 
RewriteCond %{REQUEST_URI} !dir2/
RewriteRule ^(.*)$ /dir2/$1 [L]

the real problem is that my client had an old website (with a different structure), in another folder, let's say dir1, which still can be found in web search.
Si, i need to send all request to www.example.com/dir1 to www.example.com.

In a few words:
the root refers to /dir2/
when i find a link to /dir1/page.jsp or whatever i should be redirected to the root.

Can you help me please?

You are almost there. You will need two rules to cover this. The first in the root, something like the above, and the second inside /dir1/, something like..

RewriteEngine on
RewriteRule . http://example.com/ [R=301,L]

You could do it all in the root .htaccess, but this way is probably more readable.

;o)



Scott - 01.01.15 2:09 am

This should be so simple but after a few hours I'm still scratching my head

I want to take a URL - mydomain.com/brand and change it to mydomain.com/index.php?brand

I can get this to work: mydomain.com/b/brand -> mydomain.com/index.php?brand - but this is not what I want.


RewriteRule b/(.*)$ http://mydomain.com/index.php?$1

(without the leading b I get an error)

And yes, I've read the above over and over again smiley for ;) and swear this should work - lol

Thanks in advance if you can assist.

Scott


Assuming this is in .htaccess, not httpd.conf (or similar server config file) I think you are looking for something like this..

RewriteRule ^(.*)$ /index.php?$1


If you wanted to also use http://mydomain.com/brand/ (which I recommend), you could do..

RewriteRule ^([^/]*)/?$ /index.php?$1


The "^" and "$" anchors are optional here. In each case, your script will have the "brand" variable set. It will be empty. You might want to consider..

RewriteRule ^([^/]*)/?$ /index.php?brand=$1


So now your script has the variable set to whatever the URL was, e.g..

http://mydomain.com/foobok/ --> http://mydomain.com/index.php?brand=foobok


Have fun!

;o)



Admin - 09.03.15 6:30 pm

Dear Sir,

I have face some problem on my url see below

http://www.example.com/index.php?page=mkx
how do i make it
http://www.example.com/mkx/
i am using vbulletin


This has been explained too many times! ;o)



toM - 21.03.15 6:07 pm

i try to fix following problem

1234.firstdomain.at/xcvlkjahds/kjhlkj/lklkhk
--> 1234.SECONDDOMAIN.at/xcvlkjahds/kjhlkj/lklkhk

i tried several - any idea?


I see no evidence of your trying! ;o)



Jon - 06.05.15 9:19 pm

How can add HTTP 301 (Moved Permanently) redirects from Office 365 website-formatted URLs to GoDaddy-formatted URLs. That is (.aspx) to (.html) The thing is I am migrating my website from office 365 website which has URL formats ending in .ASPX to godaddy website which has URLs ending in HTML. Can RewriteRule do this trick. microsoft say is not possible to add HTTP 301 (Moved Permanently) redirects from Office 365 website-formatted URLs to GoDaddy-formatted URLs. I dont know if this possible with htaccess on a differnt host


mod_rewrite doesn't care about the format of the URLs. Almost anything is possible, so long as the server can do rewrites, that is. ;o)



dang - 05.08.15 2:38 pm

Hello Sir,

I am facing some problem on my url see below-

http://www.example.com/a/b/c.php

how do i make it

http://www.example.com/c.php


What you need is mod_rewrite. I have a page about it somewhere. ;o)



michael - 12.08.15 6:27 am

I have a system that allow search engines to crawl for text files on my server on behalf of users.
What i want to do is anytime the crawlers come to crawl for the files i would like to check if the user of that file(i.e .txt) is from a certain country(i.e from the database) before the crawler crawls his file else deny the crawler access to the file. sample url is "http://example.com/users/123/file/user123.txt". Thanks in advance for your assistance.


This really belongs on page one of this tutorial. You don't need mod_rewrite for this.

The only way to achieve this in .htaccess, afaik, is to use an external filter (mod_ext_filter). However, this method produces so much overhead, you will need to test whether there is anything to be gained at all.

Why not allow the crawler access? If you don't want crawler X from country Y crawling your site, why not ban it outright, rather than check if crawler XY is access each and every file? It would be much more resource friendly.

Also, there are other ways to check if an IP is from a certain country. Is accessing your own database for each request really the most effective method?

If I were you, I would have a bigger think about this.

;o)



metin - 14.08.15 6:49 pm

hi,

my url sproducts.php?IndustryID=53&CategoryID=417 how is rewrite? i want to write this industryname/53/categoryname/417

thanks.


padi hitam - 06.09.15 9:20 pm

how to rewrite this :
localhost/a/b/c/index.php

to be :
localhost/a/


Holaal - 15.09.15 6:35 pm

Hi,

On my webpage i have listing of products. On click of one of the product request is made to "www.abc.com/product_detail.php?id=89". After that, common header page is loaded before product_detail.php so in common header page i have created new url like "www.abc.com/women/ko5698/details" here 'ko5698' is name of product and 'women' is category name.
so, ihave genrated this url using php and made new request using header location.

now in htacces i have used this rule
RewriteRule ^(.*)/([A-Za-z0-9=]+)/details$ /product_detail.php?id=$2 [L]
so when it found 'details' in url request goes to product_detail.php with name of that product.

Now i want to do this i want to remove or hide 'details' keyword from url but when i remove that keyword my htaccess does not know for what request is made up so 404 error is came. so how can i hide that keyword from url. Thanks in advance.


tomyzzg - 30.12.15 8:47 am

hi ,
my web url:xxxxxxxxx&from=singlemessage&isappinstalled=1
how can i write the RewriteRule
and it result as:xxxxxxxxx,

cut the string from"&from=】....."

thank u.



Abhishek19sept - 19.02.16 2:16 pm

hi,
my web url: http://www.example.com/frmAdmission.php

I want the result as http://www.example.com/Admission

means remove "frm" keyword from my every webpage and remove ".php" extension from my every webpage.

please help me out.


Dennis - 02.03.16 6:02 am

Hi Abhishek19sept

Just create a file called Admission in the root, then add a index.php or html inside the file with the information you want to display.

If is wordpress, create a page called admission, then add the pages you want and put admission as a parent page in the page attributes

Hope this helps you


Gopinath Mukherjee - 24.03.16 2:16 pm

My .htaccess file is in root,i am create app and website,website is in wordpress and app is in codeigniter.When ever i try to access codeigniter site 404 error is shown.can anyone help me.Thanks in advance.


A.D. - 29.03.16 7:11 pm

Want to write a rule to replace the special characters from the URL with '-' sign
for e.g.
original url /wps/wcm/connect/a%20b/c_d/a+b want to change to /wps/wcm/connect/a-b/c-d/a-b
how to write generic rule for such URLs. after connect it can go to any number of levels.


Teddy - 12.07.16 5:09 am

Hi,
I have 2 requirements as follow,
1 All http://mydomain.com/eu/en/* redirects to http://mydomain.com/de/de/* (1:1 redirect)
2 If the above page does not finds a redirect page then it need to redirect to its parent page like below,

http://mydomain.com/eu/en/about/news/1024.html is not found in de/de

then the page need to redirect to
http://mydomain.com/de/de/about.html


For #1 i have a rule setup like below
RewriteRule ^/eu/de/about/(.*)$   http://www.mydomain.com/de/de/about/$1   [R=301,L]

But for #2 i have no idea....

your suggestions are highly appreciated!!!

Thanks!!!


Rob - 31.08.16 5:11 am

Regarding payment, yeah.. i'm one of those who would rather pay a few quid for a quick answer,, Please email me for more information. Cheers.


Jim - 03.09.16 7:00 am

Mighty fine show!

I have been experimenting with the ".htaccess"-file myself. My current project with one, is to really tighten up security on my personal websites/blogs. This "second-helping" article has given me some good ideas and perhaps some much-needed simplifying help.

Lately,

I have also been playing around with creating/setting new environment variables (more appropriately, they are stored as: $_SERVER-array indices rather than $_ENV-array indices). There is so much one can do (both good and BAAAD) with the "SetEnv" directive!

Anyway,

Just wanted to say "Thanks again" for another finely written tutorial.

- Jim




pettlepop - 13.09.16 5:39 am

funniest coming out story
https://youtu.be/EI4kH4wQVEk


Sujoy - 18.01.17 3:33 pm

Hello,

Please check you tell me , How to display 3rd party url display like in my own server url?
Example:
https://code.jquery.com/jquery-2.2.4.min.js when it load its display in my own site like www.example.com/js/jquery-2.2.4.min.js using with .htacess.



mihaip007 - 24.01.17 6:57 pm

Hi I have a problem and perhaps you can assist me.

I edited my dns to allow for * (wildcard subdomains), then I added a cpanel subdomain for * and pointed it to my "root_folder/subdomains".

In there I added a .htaccess file and an index.php file. The .htaccess file is supposed to check for the following: If I go to foobar1.mydomain.com, if there IS a folder in there called foobar1, then redirect to it silently. In other words make that subfolder "foobar1" become the root from which the website is served from.

Instead of getting foobar1.mydomain.com I am getting an actual redirect to foobar1.mydomain.com/foobar1.

How can I stop that from hapening?

Here is the content of my .htaccess file:
RewriteEngine On
RewriteRule ^.htaccess$ - [f]
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ^([^.]+).mydomain.com
RewriteRule ^$ /%1 [l]


Is there something I am misunderstanding here?
Thank you in advance.




Steve1200 - 19.08.19 9:07 am

Great article! I recently found I need to modify some of the URLs on my site and have not found a way to do it. My URLs are:

some code..
http://domain.com/folder/search.php?itemID=I17230&car=Car-1


or

some code..
http://domain.com/folder/groupsearch.php?groupID=F11969&car=car-1


This rewrite condition seems to find the car-1 variable:

some code..
RewriteCond %{QUERY_STRING} ^(.*)car-1(.*)$


But I cannot determine how to change Car-1 or car-1 to car1

If I missed something you covered, please let me know.


Amar Sonkar - 03.10.19 6:07 pm

Hii Sir i want to rewrite this http://example.com/blog-details.php?title=Twin%20Baby:%20IVF to http://example.com/blog-details.php?title=Twin Baby: IVF through .htaccess please help me. please write htaccess for asap. Thanks


Jason - 12.01.21 5:55 pm

I stumbled upon this page nearly 2 decades ago. It was incredibly helpful to me back then as a teenage developer still trying to figure out what I'm doing. These days I'm director of engineering at a medium sized business and doing well for myself. We were recently discussing CORS policies for a client, and out of nowhere I thought of you. I'm so happy to see that this site is still up. It brings back so many memories. Anyway, I just wanted to pop in and say thank you for this, and I hope you're doing well.

Kind Regards,
Jason


First, confirm that you are human by entering the code you see..

(if you find the code difficult to decipher, click it for a new one!)


Enter the 5-digit code this text sounds like :

lower-case zed, Upper-Case Tee, too!, lower-case aitch, Upper-Case Kay


 

Welcome to corz.org!

I'm always messing around with the back-end.. See a bug? Wait a minute and try again. Still see a bug? Mail Me!