Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does RewriteBase work in .htaccess

I have seen this in a few .htaccess examples

RewriteBase / 

It appears to be somewhat similar in functionality to the <base href=""> of HTML.

I believe it may automatically prepend its value to the beginning of RewriteRule statements (possibly ones without a leading slash)?

I could not get it to work properly. I think it's use could come in very handy for site portability, as I often have a development server which is different to a production one. My current method leaves me deleting portions out of my RewriteRule statements.

Can anyone explain to me briefly how to implement it?

Thanks

like image 481
alex Avatar asked Apr 01 '09 04:04

alex


People also ask

What does IfModule mod_rewrite C mean?

The <IfModule mod_rewrite. c>... </IfModule> block ensures that everything contained within that block is taken only into account if the mod_rewrite module is loaded. Otherwise you will either face a server error or all requests for URL rewriting will be ignored.

What is rewrite condition?

RewriteCond is an apache mod-rewite directive which is used for conditional URL rewriting on Apache server. We use RewriteCond along with RewriteRule%{HTTP_HOST} , %{HTTPS} etc.

What is URL rewrite in Apache?

URL rewriting purpose is to change the appearance of the URL to a more user-friendly URL. This modification is called URL rewriting. For example, http://example.com/form.html can be rewritten as http://example.com/form using URL rewriting.

What is RewriteOptions inherit?

With the RewriteOptions inherit option, any rewrite rules in a parent directory are essentially appended to the end of any rules in the sub directory. Apache 2.4 has an additional option of appending them before or after the rules in the sub directory. The directive will only affect mod_rewrite and rewrite rules.


1 Answers

In my own words, after reading the docs and experimenting:

You can use RewriteBase to provide a base for your rewrites. Consider this

# invoke rewrite engine     RewriteEngine On     RewriteBase /~new/  # add trailing slash if missing     rewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L] 

This is a real rule I used to ensure that URLs have a trailing slash. This will convert

http://www.example.com/~new/page 

to

http://www.example.com/~new/page/ 

By having the RewriteBase there, you make the relative path come off the RewriteBase parameter.

like image 89
alex Avatar answered Sep 21 '22 15:09

alex