# URL Rewrite

**URL:** https://boards.straightdope.com/t/url-rewrite/553943
**Category:** Factual Questions
**Created:** [September 16, 2010, 5:52am UTC](https://boards.straightdope.com/t/url-rewrite/553943 "2010-09-16T05:52:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DanBlather](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@DanBlather](https://boards.straightdope.com/u/DanBlather)
#### Post date: [September 16, 2010, 5:52am UTC](https://boards.straightdope.com/t/url-rewrite/553943/1 "2010-09-16T05:52:46Z")

</div>

OK, so I want to replace [example.com/x](http://example.com/x) with [www.example.com/X](http://www.example.com/X). When I follow the in-line tutorials I get: www.example.comX  
Here is the .htaccess snipet

RewriteEngine On  
RewriteCond %{HTTP\_HOST} ^example.com$  
RewriteRule (.\*) [http://www.example.com$1](http://www.example.com$1) [R=301]

---

<div class="post-metadata">

### Author: ![sharding](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@sharding](https://boards.straightdope.com/u/sharding)
#### Post date: [September 16, 2010, 6:02am UTC](https://boards.straightdope.com/t/url-rewrite/553943/2 "2010-09-16T06:02:31Z")

</div>

Put a / before the $1 in the RewriteRule.

I assume the difference in capitalization is just a typo, because you’re not doing anything there regarding that…

---

<div class="post-metadata">

### Author: ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)
#### Post date: [September 16, 2010, 6:14am UTC](https://boards.straightdope.com/t/url-rewrite/553943/3 "2010-09-16T06:14:11Z")

</div>

Since you’re doing this inside a .htaccess file (as opposed to the main server httpd.conf), the rules are slightly different. mod\_rewrite will remove everything up to the current directory level and _then_ apply the match. So your (.\*) pattern is matching ‘X’ instead of ‘/X’.

There’s a confusingly-worded explanation of this in the mod\_rewrite documentation [here](http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule):

> [@TFM](#):
>
> Never forget that Pattern is applied to a complete URL in per-server configuration files. However, in per-directory configuration files, the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done.

So, two possible fixes:

1. Put this in the main httpd.conf, which is where it should probably be if you’re doing redirects at the server root.

2. Change your RewriteRule to:  
RewriteRule (.\*) [http://www.example.com/$1](http://www.example.com/$1)

---

<div class="post-metadata">

### Author: ![DanBlather](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@DanBlather](https://boards.straightdope.com/u/DanBlather)
#### Post date: [September 16, 2010, 7:11am UTC](https://boards.straightdope.com/t/url-rewrite/553943/4 "2010-09-16T07:11:40Z")

</div>

As usual, you guys rock
