How to regex a url to redirect user
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, mod-rewrite, regular-expression, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :Using the regex and the .htaccess file, I want to redirect users to a certain URL.
I want to redirect:
http://www.example.com/*but nothttp://www.example.com/CDadmin/*
I have tried the following:
http://www.example.com/CDadmin/[*-^$CDadmin/*]
But does not work when I test with regex101.
Since you've not clarified the specifics of your question, I'll make some assumptions:
- You wish to redirect to a different domain. eg.
example.net example.netpoints to a different server./CDadminis a physical directory on the filesystem.
At the top of the
.htaccessfile in the document root ofexample.com, place the following:RewriteEngine On
RewriteRule (.*) http://example.net/$1 [R=302,L]The only regex here is
(.*)- which will basically redirect everything. (Read on...)This is a temporary (
302) redirect. Change to a301if you want to make it permanent.Then, in the
.htaccessfile in the/CDadminsubdirectory, simply enable the rewrite engine:RewriteEngine OnThis will effectively override the mod_rewrite directives in the parent
.htaccessfile, so nothing will be redirected when accessing/CDadmin/<something>.
Comments
Post a Comment