Mod Rewrite: Removing the leading '/' on a relative link

I am trying to use mod_rewrite to remove a leading ‘/’ on a relative link, so that:

	<link rel="stylesheet" type="text/css" href="/css/nav.css" media="all" />

becomes:

	<link rel="stylesheet" type="text/css" href="css/nav.css" media="all" />

(no ‘/’ before the css directory).

I’ve tried several variations around:

RewriteEngine on
RewriteRule ^/css/(.+)$ css/$1

but nothing seems to work (I have checked that the Rewrite Engine is on).

Can someone put me right, please.

Tim,

The leading / is NOT a relative link as it makes the link an internal ABSOLUTE link. IMHO, you probably do not want to remove the /'s because it’s likely that you’re changing the directory level of the request with your other mod_rewrite code. Besides, how can you tell the / is there as that merely requests (first, the root of the server then) the DocumentRoot of the domain. You just can’t see that in a URL (unless you’re getting the root of the server).

As for your code, Apache 1.x will require the leading / to match after the ^ but Apache 2 will not match! Use ^/? if you want to match either.

Sorry, unlike your previous posts, this is a non-problem for the above reasons.

Regards,

DK

Hello DK,

Thanks for your prompt reply. I understand what you’re saying. I have tended to regard any link that doesn’t start ‘http…’ as relative, but I know that ‘/’ always means the root, so in that respect of course it’s absolute.

My need arose because I’m trying (WAS trying) to run a site as an add-on when it’s remote (i.e. at my hosting ISP, as a sub-domain but with its own URL) but as a sub-directory on my local server. I’ve concluded that that’s unwise (even foolish). Why was I trying to do it ? Because adding sites to my Virtual Hosts is such a pain. Win7 (64) is making it so difficult to edit the ‘hosts’ file. It’s a permission issue (despite being the Admin). This isn’t a Windows forum, so I won’t elaborate here.

Doing the above would require altering the links all over the place, and that’s more work than fighting Win7 over editing the hosts file once per new site. So I’m abandoning the idea.

Thanks for helping me to clear my mind.

Tim

Off Topic:

If you go to windows explorer, to the c:\windows\system32\drivers\etc\hosts file, right click, properties, security and sharing and then in the security tab somewhere -not sure because I’m not on a windows machine now but linux- is an option to change the owner of the file. Change the owner to your own username, save the changes, and after that you can just open and save that file as you please without windows ever whining about permissions again.

Alternatively you can start notepad as real admin (right click on notepad in the start menu and “Run as administrator”) and then you should be able to save to. This is not needed if you applied the first trick.

[ot]

After trying to work out what that kind of link should be called for quite some time, and searching a lot on the web to find out how it’s most commonly referenced, I finally settled on “root relative” as the most commonly agreed term.

I think the argument was that it’s not stricty “absolute” because it won’t mean anything outside the context of the site itself. An “absolute” link would point to the same location from anywhere, in any context. But I guess it’s all semantics, or how you choose to define something. “Internal absolute” is a logical alternative to “root relative”, of course. :slight_smile: [/ot]

Off topic:
ScallioXTX: Thanks for reminding me of this. I have tried it again, but my text editor (UltraEdit) still refuses to save ‘hosts’. In fact although I have (somehow) got ‘hosts’ open in UtraEdit, I can’t even see the ‘etc’ directory in the UE file open dialog ! Probably time I contacted the makers of UE.

Tim,

Rémon is normally spot-on but, with Win7 (and Vista, I believe), I right clock EditPad Light (my version of your UltraEdit) and Run As Administrator. I have it open hosts and it will save. It’s the only PITA left if you’ve installed outside the “protected directories.” There are few other files which I must Run As Administrator but that’s a trick to get around the WinDoze PITA now that M$ has been awakened to security issues. I’ll give them credit for that but believe that they could have done a far better job on security from Day One.

Ralph,

Apache identifies both the http:// and / links as absolute which meet the requirement for the mod_alias family of directives. Obviously, http:// is an external link and / is an internal link. Note, too, that / is interpreted first as the root of the server THEN as the DocumentRoot of the domain. That’s why I used the internal absolute terminology.

Regards,

DK

Thanks David. :slight_smile:

DK,

Thanks. The thing I never understand is that although I’m signed in to Win7 as Administrator it keeps ‘pretending’ that I’m not and requiring me to do things ‘As Admnistrator’. Often I just dismiss that dialog (click whatever it wants clicked) and it does what I want anyway (so where’s the security in that?), But not in this instance.

At the time of writing I’m having to edit ‘hosts’ and save it somewhere other than its correct location, and then use Windows Explorer to drag it across to the system files.

But we’re getting way off topic.

Tim

Tim,

No, no, no. If you save elsewhere, you’ll not be able to overwrite the on in the etc directory.

I’ve just confirmed that I can run my text editor by RIGHT clicking on its icon in the quick launch area (you can do the same thing on your desktop or in the Start menu) and select Run As Administrator. From the editor’s menu, I selected File | Open and navigated to the C:\Windows\System32\drivers directory where etc was not visible. Clicking in the path at the top of that window, I could add \etc and that showed me the etc directory where I merely clicked on hosts to open. It saved normally without any difficulty.

Don’t give up as you can defeat the @#$% that M$ has put in your way. Using your own computer as a test server is VERY handy!

Regards,

DK

Hello DK,

Thanks for your reply. Although I only did it this morning, I’ve forgotten precisely what I did (but it most certainly worked). I think I saved the new ‘hosts’ as ‘hosts(2)’, dragged that across to the ‘c:/windows/…/etc’ directory, and then did some renaming. I wonder if that worked because Windows is inconsistent and allows renaming where it won’t allow over-writing ?

It no longer matters exactly what I did, because I’ve now changed the permissions on the ‘hosts’ file and its path directories, and I can open ‘hosts’ with Notepad without having to ‘Run as Administrator’. I’ve also been in touch with IDM Computer Solutions, the makers of UltraEdit and they’ve given me a line to put in their ‘ini’ file which enables display of the ‘etc’ directory so I can now open ‘hosts’ in UE and save the edits. My problems have melted away. M$ might say my files were now at risk, but I’m the sole user of this home office machine, and I’ll take my chances. Now that I can open ‘hosts’ in UE it may be I can work with the permissions reverted, we’ll see.

I’ve been running a local server for a long time, and only ran into difficulties with editing ‘hosts’ in my new Win7 (64) machine. My original work-around had been to run web sites under development in sub-directories of a domain already set up as a virtual host. That worked to a degree on a simple site, but I ran into trouble with directory levels and PHP includes. Hence trying to use mod_rewrite to remove the leading ‘/’ from links, which started this thread. I no longer need to do anything like that.

My thanks to you and others, talking around this problem has led to a far better (and more ‘standard’) solution.

Tim

:tup:

I’m sure that you’re well aware that you can also create domains which mimic the online domains, i.e., you can duplicate files, etc., but drop the .com/.net/.etc or merely abbreviate the domain name as I do (on my test machine). All it takes is a quick edit of the hosts file and httpd-vhosts.conf and you’ve got a test domain which should not interfere with the online domain - even on the same box. Okay, just use care with your database access as you should “duplicate” the database (or just a table) and not play with live data.

Regards,

DK

Regards,

DK

Hello DK,

Yes, I do that all the time, and I have a complete Apache Server running locally. Originally set up with XAMPP (5-6 years ago), but now I have one I’ve installed and configured myself. My problems arose from the absurd level of protection Win 7 provides for system files, even from the Admin. I suppose M$ have learned the hard way that most Home users are Admins, and they’ll fiddle with stuff they don’t understand.

Where I ran into trouble (and started this thread) was in trying to mimic the behaviour of ‘add-on’ domains. I was having so much hassle editing the ‘hosts’ file I was looking for a work-around. But now that I can freely edit ‘hosts’ I can do it properly. Better solution all round.

I also came upon your 'htaccess tutorial. I’ve used .htaccess before, but I now have a better understanding. Thanks.

Regards,

Tim