Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can apache rewrite rules go in httpd.conf

How do I create a simple apache rewrite rule to rewrite:

http://domain.com/vanity to http://domain.com/foo/

Also can rewrite rules go in httpd.conf or do they have to go in a .htaccess file?

Thanks in advance.

like image 983
Ben Paton Avatar asked Oct 17 '12 19:10

Ben Paton


People also ask

What is the best way to rewrite a URL in Apache?

Other documents go into greater detail, but this doc should help the beginner get their feet wet. The Apache module mod_rewrite is a very powerful and sophisticated module which provides a way to do URL manipulations. With it, you can do nearly all types of URL rewriting that you may need.

Is URL rewriting difficult?

With it, you can do nearly all types of URL rewriting that you may need. It is, however, somewhat complex, and may be intimidating to the beginner. There is also a tendency to treat rewrite rules as magic incantation, using them without actually understanding what they do.

How are rewrite rules matched against the hostname?

It is initially (for the first rewrite rule or until a substitution occurs) matched against the URL-path of the incoming request (the part after the hostname but before any question mark indicating the beginning of a query string) or, in per-directory context, against the request's path relative to the directory for which the rule is defined.

What is the difference between per-server and per-rule rewrites?

The main difference with per-server rewrites is that the path prefix of the directory containing the .htaccess file is stripped before matching in the RewriteRule. In addition, the RewriteBase should be used to assure the request is properly mapped.


2 Answers

Put this in your website's <virtualhost>.

RewriteEngine On
RewriteRule ^/vanity$ /foo/ [L]

The [L] will silently redirect, so the user will still see /vanity in the URL. You could use [R] to force a redirect.

like image 114
artfulrobot Avatar answered Sep 27 '22 22:09

artfulrobot


They go in httpd.conf. Check out the doc

like image 30
amphibient Avatar answered Sep 27 '22 23:09

amphibient