2

I would like to redirect http://abc.xyz.ac.uk/folders/abc/lmn http://abc.xyz.ac.uk/folders/abc/lmn.html

Here is the code where I am enabling this

 <Directory /var/www/folders>
            RewriteEngine on
            RewriteRule ^/abc/lmn$ ^/abc/lmn.html [NC,L]
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>

What is wrong with this rule and is this the correct place to include rewrites?

Thanks

3
  • Why have you determined it's 'wrong'? Are you seeing errors in your logs? Is it just not working? What troubleshooting steps have you taken so far?
    – user143703
    Jun 21, 2013 at 21:55
  • One thing is that I cannot get access to the logs on the Ubuntu server I am using, I did do a tail of the access log, but there are no rewrite errors and the rewrite is not firing.
    – user10211
    Jun 21, 2013 at 22:26
  • @user10211 Do you want to redirect so that the client has the .html in their address bar, or do you just want to display the content from the .html file without sending the client a redirect? Jun 22, 2013 at 6:57

3 Answers 3

3

Rewrite paths work differently inside a <Directory > context. The path you are matching is the full filesystem path with the current directory (including the trailing slash) removed.

The effect of this is that you need to remove the leading slash from your regex and the replacement or move the rewrite rule outside the directory context where it will be matching the URI instead of a filesystem path.

Note that a .htaccess file is implicitly in a directory context because it is in a directory.

You also shouldn't have a caret in the replacement. The second argument is not a regex.

You mentioned a redirect in your question. To make that happen you need [R] or one of the more specific codes ([R=301] or [R=303]) after the second argument.

0

Try this instead:

RewriteRule ^/abc/lmn$ /abc/lmn.html [NC,L]

As per https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule, the arguments to RewriteRule are a pattern, a substitution, and some flags. The ^ character will be treated as a literal in the substitution part, which is probably not what you want.

mod_rewrite has excellent logging, which can be helpful for debugging this sort of problem. Enable it with LogLevel alert rewrite:trace3, and tweak the level anywhere up to trace8 to get the level of detail you need. More details on the logging here:

https://httpd.apache.org/docs/current/mod/mod_rewrite.html#logging

1
  • I am afraid your suggestion did not work. I did set the loglevel to alert and setup my own logs, but there is nothing in there.
    – user10211
    Jun 21, 2013 at 22:37
0

Try the following:

<Directory /var/www/folders>
        RewriteEngine on
        RewriteBase /folders
        RewriteRule ^/abc/lmn$ /abc/lmn.html [NC,L]
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
</Directory>
1
  • I have tried all the above suggestions. This is how the final looks like and unfortunately it does not work code Document root /var/www/folders <Directory /var/www/folders> RewriteEngine on RewriteBase /folders RewriteRule ^/abc/lmn$ /abc/lmn.html [NC,L] Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory>
    – user10211
    Jun 21, 2013 at 23:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .