Rewrite CSS and Javascript files in Folder to Gzip Version - apache

I use the code below in htacess to redirect css and javascript files to the gzip version.
RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.jgz -f
RewriteRule (.*)\.css$ $1\.css.jgz [L]
RewriteRule (.*)\.js$ $1\.js.jgz [L]
AddType "text/stylesheet" .css.jgz
AddType "text/javascript" .js.jgz
AddEncoding gzip .jgz
But i notice that in Wordpress Admin a lot of javascript files are blocked because they didn't exists as gzip version.
So i would like to redirect only javascript and css files from these folders
domain.de/styles/
domain.de/scripts/
I want to change this two lines,
RewriteRule (.*).css$ $1.css.jgz [L]
RewriteRule (.*).js$ $1.js.jgz [L]
and put the folder /styles/ and /scripts/ into it.
How do I have to adjust the code above? I can not get it though. The styles are no longer displayed.

You can match the folders in the regex.
RewriteRule ^((styles|scripts)/.*)\.css$ $1\.css.jgz [L]
RewriteRule ^((styles|scripts)/.*)\.js$ $1\.js.jgz [L]

Related

Redirect all .php requests to .html using .htaccess

The following code works to hide .php and replace it with .html
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^.]*)\.html/?$ [NC]
RewriteRule ^(.*)$ %1/%2/index.php [L]
I would like to redirect all .php requests to .html.
All .php files are inside a sub directory in "https://www.sitename.com/user/".
For example
https://www.sitename.com/user/login/index.php
https://www.sitename.com/user/name/index.php
https://www.sitename.com/user/register/index.php
https://www.sitename.com/user/logout/index.php
https://www.sitename.com/user/dashboard/index.php
https://www.sitename.com/user/contact/index.php
It should redirect to
https://www.sitename.com/user/login.html
https://www.sitename.com/user/name.html
https://www.sitename.com/user/register.html
https://www.sitename.com/user/logout.html
https://www.sitename.com/user/dashboard.html
https://www.sitename.com/user/contact.html
Adding separate line of .htaccess code for each folder will be difficult, can someone help with simple code to automatically detect and redirect .php to .html ?
Explanation:
If some try to access "sitename.com/user/login/index.php", it should load "sitename.com/user/login.html".
sitename.com/user/login.html should be the only URL that is visible to users. Even if someone try to access "sitename.com/user/login/index.php", it should redirect/rewrite to "sitename.com/user/login.html".
Enter sitename.com/user/login.html in browser = sitename.com/user/login.html
Enter sitename.com/user/login/index.php in browser = sitename.com/user/login.html
With your shown samples, could you please try following. Please make sure you clear your browser cache before validating your URLs. As per OP's comment edited rules, as per .php and .html formats. I believe you should avoid giving both kind of urls to users and ask them to hit only .html urls in case anyone hits .php url you could forbid it(in case they are directly hitting it, another reason could be because if they are hitting .php directly then you want it to change URL on browser to .html which is actually being served by a .php file itself in backend), if they hit .html then that could be served by respective index.php of passed uri IMHO. NOTE: This is IMHO only and should not be used if someone has more requirements on this one.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/user/([\w-]+)\.html/?$ [NC]
RewriteRule ^(.*)$ user/%1/index.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} index\.php/?$ [NC]
RewriteRule ^ - [F]
You can use the following rules to convert your php URLs into html :
RewriteEngine on
#Redirect and rewrite php URLs to html
#redirect /user/foobar/index.php to /user/foobar.html
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^user/([^/]+)/index\.php$ /user/$1.html [L,R]
#rewrite /user/foobar.html to /user/foobar/index.php
RewriteRule ^user/([^.]+)\.html$ /user/$1/index.php [L]
The rule #1) triggers when /user/foobar/index.php is requested and redirects it to /user/foobar.html . Since the .html file doesn't exist the second rule maps the .html request back to original .php page but your URL remains the same in browser address bar.

How to apply chunk / modx template changes?

I make changes to the template files, upload them via ftp (or through the admin panel). In the admin panel the chunk's code changes, but the site itself does not change. Why?
I tried:
Clear the cache through the modx admin panel - "Management - Clear
the cache"
Delete the cache folder, which is located in the "core \ cache"
directory
I tried to completely delete the template folder to check if there
would be changes.
Modify the .htaccess file
None of this helped. There are changes in the admin panel, but everything remains as it was on the site
Website on MODX Revolution 2.6.5
In the settings of the server (hosting) it is written that Apache 2. * and PHP 7.1 are used
Here is the .htaccess file
# MODX supports Friendly URLs via this .htaccess file. You must serve web
# pages via Apache with mod_rewrite to use this functionality, and you must
# change the file name from ht.access to .htaccess.
#
# Make sure RewriteBase points to the directory where you installed MODX.
RewriteRule ^.*[-/]v(\d+)-.*$ index\.php?id=$1&%{QUERY_STRING} [L]
RewriteRule ^v(\d+)-.*$ index\.php?id=$1&%{QUERY_STRING} [L]
RewriteRule ^k(\d+)[-/].*[-/]v(\d+)-.*$ index\.php?id=$1-$2&%{QUERY_STRING} [L]
RewriteRule ^k(\d+)[-/]v(\d+)[-/].*$ index\.php?id=$1-$2&%{QUERY_STRING} [L]
RewriteRule ^v(\d+)[-/].*[-/]k(\d+)[-/].*$ index\.php?id=$2-$1&%{QUERY_STRING} [L]
RewriteRule ^v(\d+)[-/]k(\d+)[-/].*$ index\.php?id=$2-$1&%{QUERY_STRING} [L]
RewriteRule ^.*[-/]v(\d+)[-/]k(\d+)[-/].*$ index\.php?id=$2-$1&%{QUERY_STRING} [L]
RewriteRule ^.*[-/]v(\d+)[-/].*[-/]k(\d+)[-/].*$ index\.php?id=$2-$1&%{QUERY_STRING} [L]
RewriteRule ^.*[-/]k(\d+)[-/].*[-/]v(\d+)[-/].*$ index\.php?id=$1-$2&%{QUERY_STRING} [L]
RewriteRule ^.*[-/]k(\d+)[-/]v(\d+)[-/].*$ index\.php?id=$1-$2&%{QUERY_STRING} [L]
RewriteEngine On
RewriteBase /
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
#php_flag register_globals Off
#php_flag zlib.output_compression On
#php_value zlib.output_compression_level 5
#ExpiresActive On
#ExpiresByType image/gif A2592000
#ExpiresByType image/jpeg A2592000
#ExpiresByType image/png A2592000
#BrowserMatch "MSIE" brokenvary=1
#BrowserMatch "Mozilla/4.[0-9]{2}" brokenvary=1
#BrowserMatch "Opera" !brokenvary
#SetEnvIf brokenvary 1 force-no-vary
What to do?
Maybe someone came across a similar one?
What about your browser cache? How do you flush it? Is it the problem with just one chunk or with all of them? How do you call them? I mean like this - [[!...]]? Each resource has "cacheable" setting, what about it?
PS: setting "cacheable" should be on the tab "Settings" with other resource settings, like whether it is a container or not, etc.
https://docs.modx.com/revolution/2.x/making-sites-with-modx/structuring-your-site/resources

.html removal from URL using .htaccess rewrite rules

I have a static website - a bunch of static html pages. I am trying to remove .html part from the URL of my webpages. I have used an .htaccess file with the following code to do that:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
However, I am getting a 404 error. For example:
The requested URL /home/username/public_html/contact.html was not found on this server.
Ideally, it should redirect to /~username/contact.html.
Addition information
When I used "Options -MultiViews" line above the code it is giving the following error:
500 Internal Server Error: The server encountered an internal error or misconfiguration and was unable to complete your request.
Apache/2.2.15 (Red Hat) server is used here.
Why I am facing this problem? Is root is automatically getting changed from public_html/ folder?
EDIT:
Directory Structure:
Username
.gnome2 (there are empty folders inside it)
.mozilla (there are empty folders inside it)
public_html (I have put css, fonts, js, etc folders and .htaccess, contact.html, index.html, etc files here.)
.bash_logout
.bash_profile
.bashrc
Username folder is under universitynameuniverse folder (whose other folders I cannot see).
Try this code in your .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can now link pages inside the HTML document without needing to add the extension of the page.
Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [QSA,L]
</IfModule>
## Results
# ~user/contact => ~user/contact.html (only if file exists)
It only works if the ~user/ directory actually exists on the filesystem.
If you need an external redirect instead, append an R flag to the RewriteRule directive.
The problem with your rewrite rule is that it's appending a .html suffix to the whole %{REQUEST_URI} variable which will probably result in a 404.
Update
Re-reading your question I noted that you want to map the ~user part to somewhere out of the apache webroot. In this case, try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/~(.+)/(.+)$
RewriteCond /home/%1/public_html/%2.html -f
RewriteRule . /home/%1/public_html/%2.html [QSA,L]
</IfModule>
## Results
# ~user/contact => /home/user/public_html/contact.html (only if file exists)

Removing .php extensions from output

I'm developing a small CMS solution with Perch. It's currently running on WampServer on my local development machine.
As Perch doesnt provide friendly URL's out of the box, I wanted to implement this, whilst ensuring the /perch directory remains untouched.
So far, I have the rewriting part working i.e. a request for /blog.php will 301 to /blog, and, /blog will rewrite to /blog.php, using the rules below:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Rewrites domiain.com/file to domain.com/file.php
RewriteCond %{REQUEST_URI} !^/perch
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Redirects domain.com/file.php to domain.com/file
RewriteCond %{REQUEST_URI} !^/perch
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} ^(.+)\.php$
RewriteRule (.*)\.php$ /$1 [R=301,L]
However, I'm still left with .php extensions in the HTML output. I tried adding the following to my .htaccess file:
AddOutputFilterByType SUBSTITUTE text/html
#Replace all .php extensions
Substitute s|.php||ni
#Original blog pattern /blog/post.php?s=2014-11-18-my-first-blog-post
Substitute s|blog/post\?s=(\w+)|blog/$1|i
However, this is applied globally, i.e. even to links within the /perch folder. I couldn't find anyway of adding a condition to apply it to everything except for the /perch folder - is there such a way?
I also looked at the ProxyPass/ProxyReversePass documentation, but this seems like overkill to just replace some HTML on a page.
Any help would be greatly appreciated.
Kind regards,
dotdev
Are you talking about the Perch CMS from www.grabaperch.com?
Everything is here: http://docs.grabaperch.com/video/v/simple-url-rewriting/
However, I'm still left with .php extensions in the HTML output
.htaccess / mod_rewrite does nothing to your HTML output.
Think of the RewriteRules as a postman who delivers mail (URLs) to target mailboxes (actual files).
What you do is you "manually" omit the .php extension in your markup (HTML output):
In perch_pages_navigation(), you need to set hide-extensionsto true
URLs you add manually: just write them without .php
Now you need to instruct the postman to route those addresses to the .php file anyway. That's what these RewriteRules are for. So .htaccess doesn't remove the .php suffix - on the contrary, it adds it.
Here's the basic .htaccess (goes into your public_html directory) for Perch (or any "remove .php" use case) + Perch Blog. I've added some explanations:
# make sure the address we received (e.g. /mypage) is not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# make sure it's not an existing directory either
RewriteCond %{REQUEST_FILENAME} !-d
# make sure there IS an existing .php file corresponding to it
RewriteCond %{REQUEST_FILENAME}.php -f
# if the address starts with "blog/", pick what comes afterwards, put it into the GET Parameter and quit (that's the [L])
RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L]
# if the first conditions are ok, but it wasn't a blog post (else we would have quit), just append .php to it. Ah, and keep other get params (that's the QSA=Query String Append).
RewriteRule ^(.+)$ $1.php [L,QSA]
For more refined possibilities, you can e.g. start here: https://github.com/PerchCMS/perchdemo-swift/blob/master/public_html/.htaccess
This will have no impact at all on the functionality of the CMS in /perch/.

Apache .htaccess: Serving .css files from separate domain?

I'm trying to serve CSS files for my site from a separate domain.
I've got the following entry in my site's .htaccess (in the document root):
RewriteRule ^templates/*\.css$ http://css.mysite.com/templates/$1 [R=301,L]
With the intention of simply matching:
http://www.mysite.com/templates/default/styles/main.css
… and sending an HTTP 301 redirect to:
http://css.mysite.com/templates/default/styles/main.css
But what's actually getting called is:
http://css.mysite.com/templates/.css
(I'd like to duplicate the above for *.js and serve them from js.mysite.com and all png & jpgs and serve them from img.mysite.com.)
Any insights would be fantastic. I've got to admit, htaccess rewrite rules always seem to elude me.
Try this and let me know:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^css\.
RewriteRule ^templates/(.*)\.css$ http://css.mysite.com/templates/$1.css [R=301,L]
or a more "dynamic way"
RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)\.(js|css)$ http://$2.mysite.com/templates/$1.$2 [R=301,L]
RewriteRule ^templates/(.*)\.(jpg|gif|swf|jpeg|png)$ http://imgs.mysite.com/templates/$1.$2 [R=301,L]
This one will check for JS and CSS and use the subdomain based on the file extension: so .js will go to js.mysite.com and .css will go to css.mysite.com and same for images but these goes to imgs.mysite.com instead.
or be even more generic:
RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)$ http://media.mysite.com/templates/$1 [R=301,L]
You redirecting everything that start with templates