简体   繁体   中英

How to Edit .htaccess File

Right now i have htaccess file

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"

</IfModule>

And I wanted to add some code

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Where I can Put this code Inside or Outside.Please Suggest/guide me Proper Solution.

This is what you should do

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
 RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</ifModule>

see here for reference

also you can check this for more detail on .htaccess

Open a new IfModule under your mod_expires . You may refer to here

<IfModule mod_expires.c>
// your code
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>

Or Just

<IfModule mod_expires.c>
 // your code
</IfModule>

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

The IfModule checks if the given module is installed and is active. In your example file, its checking if mod_expires.c module exists. If yes do the stuff inside the tag.

Apache rewrites are handled by mod_rewrite.c apache module. So you should do:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>

You can get away by removing the tags completely. If there were no tags and you dont have mod_rewrite installed, apache will error out. IfModule gracefully ignores line inside the tag if the required apache module doesn't exist.

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