简体   繁体   中英

Linux CentOS 7 - Configuration of httpd.conf file through .htaccess

  1. I need to configure through .htaccess in Linux CentOS 7 that my info.php file is shown without extension. That is, calling http://server address/info instead of calling http://server address/info.php .

  2. Also, in .htaccess , add that phpinformation redirects to info . That is, http://server address/phpinformation redirects to http://server address/info .

I have followed the part of the following article .

In the httpd.conf file, I have changed AllowOverride none to AllowOverride AuthConfig .

What are the next steps?

To rewrite your files you should overrite the FileInfo type of directive, not the AuthConfig ones (see https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride for references)

Enable the mod_rewrite module in the apache configuration and in the .htaccess file use a similar configuration:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^phpinformation$ info.php [L]
    RewriteRule ^info$ info.php [L]
</IfModule>

A more general configuration:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^phpinformation$ info.php [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ $1.php [L]
</IfModule>

The first rewrite rule is the same, if you have to serve a request with an url containing just phpinformation, rewrite it to info.php and serve that url instead. The L modifier means that the rewriter does not need to search for additional rewriting and can just ask apache to serve the resulting (info.php) rule.

The second rule is a bit different, the rewrite engine perform the rewrite only if ALL the previuous condition are met. In this case the original url does not resolve to an existing file (!-f) or directory (!-d). If the file/directory exists it will be served as usual

You may also want to perform an external redirect to force the client to access to an official url for a resource, in this case the first example can change in something similar:

RedirectMatch ^/phpinformation$ /info
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^info$ info.php [L]
</IfModule>

Why do not use the RedirectMatch for both the urls? The reason is that the client user sees the redirected url on the browser and thus the .php suffix, we need to rid off, would pop up again.

References

Mod_rewrite documentation
AllowOverride documentation
RedirectMatch documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM