Search code examples
.htaccessmod-rewritepyrocms

url redirection in the default .htaccess file


I would like to map "http://www.example.com/abc" to "http://www.example.com/test/abc" for having the shortest route possible. I am using pyroCMS for my users and content.
the default pyrocms file:

# Multiple Environment config  
# Set this to development, staging or production  
# SetEnv PYRO_ENV production  

<IfModule mod_rewrite.c>

    # Make sure directory listing is disabled
    Options +FollowSymLinks -Indexes
    # disable the Apache MultiViews directive if it is enabled on the server. It plays havoc with URL rewriting
    Options -MultiViews
    RewriteEngine on

    # Keep people out of codeigniter directory and Git/Mercurial data
    RedirectMatch 403 ^/.*/(vendor|composer\.json|composer\.lock|system/cms/cache|system/codeigniter|system/cms/config|system/cms/logs|\.git|\.hg).*$

    # Send request via index.php (again, not if its a real file or folder)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    <IfModule mod_php5.c>
        RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>

    <IfModule !mod_php5.c>
        RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>

</IfModule>

I would like to add this rule to the file:

RewriteRule ^([a-z0-9_]+)$ test/$1

To rewrite "BASE_URL/abc" to "BASE_URL/test/abc"

However,

i tried many positions as to where to put this RewriteRule, my website keeps giving a "Page Missing".

Is my RewriteRule ok? And where do i insert it?


Solution

  • It all depends on what your rule is supposed to do and how it is supposed to interact (or not interact) with the rest of your site. And considering your entire htaccess file is mostly commented out code (which I removed to make it halfway readable), you just want to place it under RewriteEngine On.

    However, since it blindly routes everything into test you need to add a few conditions and make it something lilke:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/test%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/test%{REQUEST_URI} -d
    RewriteRule ^([a-z0-9_]+)$ test/$1 [L]