My Craft CMS 3 project has the following rewrite_rule in the /web .htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
Trying to implement cache busting has described here however struggling to combine the existing and proposed rewrite_rules.
https://nystudio107.com/blog/simple-static-asset-versioning
This is the cache busting rule I need to add.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?\/)*?([a-z\.\-]+)(\d+)\.(bmp|css|cur|gif|ico|jpe?g|js|png|svgz?|webp|webmanifest)$ $1$2$4 [L]
</IfModule>
Below is the code I'm now using to solve this issue. Hope it helps someone.
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?\/)*?([a-z\.\-]+)(\d+)\.(bmp|css|cur|gif|ico|jpe?g|js|png|svgz?|webp|webmanifest)$ $1$2$4 [NC]
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,NC]
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Port} !=443
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
</ifmodule>
# Example to cache images and CSS files
# adjust and extend to your needs
<ifModule mod_headers.c>
# images expire after 1 week
<filesMatch ".(gif|png|jpg|jpeg|ico|pdf|svg|js)$">
Header set Cache-Control "max-age=604800"
</filesMatch>
# CSS expires after 1 week
<filesMatch ".(css|js|woff2)$">
Header set Cache-Control "max-age=604800"
</filesMatch>
</ifModule>
Related
Can someone please help me redirect the php pages to html (only in the root directory, without subfolders)? I tried dozens of solutions, but I didn't succeed. The code below redirects everywhere (including in subfolders...) I don't want the subdirectories to be affected.
My domain: https://www.example.com/
Thanks in advance!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+?)\.php$ /$1.html [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)\.html$ /$1.php [L]
</IfModule>
<FilesMatch ".(ico|css|flv|js|gif|jpeg|png|woff|woff2|eot|svg|ttf|webp)$">
Header set Cache-Control "max-age=604800, public, no-cache"
</FilesMatch>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Have it like this:
RewriteEngine On
# add www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# external redirect to .html from .php request
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([^./]+?)\.php$ /$1.html [R=301,L,NC,NE]
# internal rewrite to .php from .html if corresponding php exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)\.html$ $1.php [L,NC]
<FilesMatch ".(ico|css|flv|js|gif|jpeg|png|woff|woff2|eot|svg|ttf|webp)$">
Header set Cache-Control "max-age=604800, public, no-cache"
</FilesMatch>
Note that [^./] will match any character that is not a dot and not a / thus matching only in root directory skipping all sub directories.
I'm trying to redirect my subdmain in this manner: anysubdomain.my_domain.com and its URLs must be redirected to root domain and its URLs respectively.
For example (1) if a user tries http://anysubdomain.my_domain.com it should redirect tohttps://www.my_domain.com.
And (2) if a user tries http://anysubdomain.my_domain.com/anyurl it should be redirected to https://www.my_domain.com/anyurl.
I was able to achieve (1) using below htaccess code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} !^www.my_domain.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
</IfModule>
yet I could not do the (2) part. I'm sure we can do this with a modification for above htaccess code, but I don't know how.
I already have wildcard cname, that is why (1) perfectly working.
Also it is a WordPress site, if that matters.
Put these rules first :
RewriteCond %{HTTP_HOST} !^www.my_domain.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
like this :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?my_domain\.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Note: clear browser cache the test.
You may use this .htaccess to handle all subdomains:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} (?:^|\.)(my_domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Make sure to test it from new browser to clear browser cache fully.
I have an .htaccess file. It needs to rewrite URLs in two cases: for the MVC framework and in a more special case. I want to rewrite any requests to "/resources/newspaper" if the user doesn't have a cookie named "cfnw-hash". I've tried putting the code before the MVC code and after it. Doesn't work. It worked before switching over to the MVC framework, though I really don't have enough .htaccess knowledge to see if that's causing the issue. Here's code:
Options -Indexes
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_COOKIE} !cfnw-hash [NC]
RewriteRule \/resources\/newspaper.* http://www.example.com/error/401 [R=401,NC,L]
Combine your rules and I think you RewriteRule might not be matching because of the prepending / and also the cookie matching. Try this. Notice the cookie part that you update with your cookie.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !cookie_name=specific_value; [NC]
RewriteRule ^resources/newspaper/?(.*) http://www.example.com/error/401 [R=401,NC,L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
Have it this way:
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !cfnw-hash [NC]
RewriteRule ^resources/newspaper /error/401 [R=401,NC,L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
Test this after clearing your browser cache.
I changed my permalinks and it working fine, but I need to redirect old tag folder to archives/tag to not lose links which google indexed
below is my .htaccess file in the root of my recipes site http://foodonia.com
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Example: http://foodonia.com/tag/fudge/ should redirect to http://foodonia.com/archives/tag/fudge
I tried to add each one of blow codes but redirect not working
#RewriteRule tag/(.*) archives/tag/$1 [NC,L]
#RewriteRule tag/$ /archives/tag/$1 [NC,L]
#RewriteRule tag/ /archives/tag [L]
even I tried redirect 301 ^/tag/fudge/ http://foodonia.com/archives/tag/fudge/ but it didn't work
I try to search for solution but can't find similar post
You need to use rules in correct order i.e. to use this 301 first before other WP rules.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule (tag/.*)$ /archives/$1 [NC,NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I want http://my_domain.com/forum/index.php/blah_blah_blah to not being redirected to anywhere.
But I want http://my_domain.com/something_else_that_is_not_forum/blah_blah_blah to be redirected to http://my_domain.com/index.php/something_else_that_is_not_forum/blah_blah_blah
So basically, only everything without http://my_domain.com/forum/ prefix should be redirected.
I have this .htaccess:
<IfModule mod_rewrite.c>
# Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^(?!forum.*)(.*)$ index.php/$2 [L,QSA]
</IfModule>
I think the regex should do exactly what I want, but in this case, seems that I'm wrong.
http://www.getnocms.com/forum/index.php?action=admin;area=manageboards;sa=newcat;df105e678e9b=e1a979a0631bd203b6794debc16ceced
Does anybody know how to do that correctly?
EDIT:
I use this
<IfModule mod_rewrite.c>
# Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^\/forum
RewriteRule (?!^forum(?:/.*|)$)^(.*)$ /index.php/$1 [L,NC,QSA]
</IfModule>
That roughly say:
If request is not pointed to file or directory or symbolic link, and it is not /forum then
If it is not started with forum, then point to /index.php/url.
It seems to be logical (well, we don't need RewriteCond %{REQUEST_URI} !^\/forum actually), but it still failed when accessing this url: http://www.getnocms.com/forum/index.php?action=admin;area=manageboards;sa=newcat;a5272d5=2fcf142818fc9df2aabb1364942a1d14
Maybe rewrite rule doesn't work with semicolon? I'm not sure.
Your rules are almost correct but some minor changes are needed. Replace your code with this:
<IfModule mod_rewrite.c>
# Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^forum(?:/.*|)$)^(.*)$ /index.php/$1 [L,NC]
</IfModule>
RewriteRule !^forum/ index.php/something_else_that_is_not_forum/blah_blah_blah
Anything that doesn't begin with...
You could also try:
RewriteCond %{REQUEST_URI} !^/forum
RewriteRule ^(.)$ http://domain.com/$1