Apache htaccess Rewrite rule to append querystring to all css files - apache

I want to append a static query string to all css files on my site. Like styles.css to styles.css?a=1. Is it possible to achieve this through rewrite rules in .htaccess file. If so what's the rewrite rule to be written. thanks in advance. .

add this to the .htaccess file under the root folder of your website, this will this redirect all css requests to the css?a=34 (query shown in the user browser), is that what you want?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.+)\.css$ $1.css?a=34 [R,L]
</IfModule>

Related

.htaccess Redirect all html files to index.php

I have strange problem with .htaccess file.
The file is placed inside directory www.mydomain.com/t/ and looks like that:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/\.]+).html$ ../index.php [R=301]
I would like to redirect all .html files to my main page www.mydomain.com
Redirection works but I am redirected to www.mydomain.com/server/file/path/t/index.php not to www.mydomain.com (or www.mydomain.com/index.php)
Where did I make a mistake?
Thank you in advance for help.
Add this rule
RewriteRule ^(.*)$ /index.php?$1 [L]

htaccess file ( url rewriting )

Is possibile convert this url:
http://corpovigiligiuratife.it/promo.php
to
http://corpovigiligiuratife.it/promozione-porta-un-amico
with htaccess MOD Rewrite?
I did several searches but I could not figure out how to convert this url.
I have 2 static 2 url and not a dynamic url.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^promozione-porta-un-amico$ promo.php [L]
</IfModule>
Use this as the content of the .htaccess file:
RewriteEngine On
RewriteRule ^promo.php$ promozione-porta-un-amico [R=301]
It will redirect the user from promo.php to promozione-porta-un-amico
The R=301 means that this link is Moved Permanently.

htaccess redirect subdirectory (folder) to single page in same subdirectory

I am having another issue with .htaccess where I can't seem to find the right solution for it. What I am trying to accomplish is to redirect from a subdirectory to a specific html document within that same directory when you copy/paste the url displayed below
For example:
http://example.com/staging/test/ to http://example.com/staging/test/page.html
My (almost) working method:
Options -Indexes -MultiViews
RewriteEngine on
RewriteRule ^test/(.*)$ test/page.html [L]
The problem with this code is that it does basically redirect to the correct page.html but if you want to click on any link on page.html it just redirects to page.html again instead of going to a particular link within subdirectory /test/ let's say /test/page-2.html
Important: This redirect should only affect the subdirectory "staging". All other sub-directories including root can not be affected.
Other info:
This code is also in my .htaccess file to remove the .html extension for the directory "staging"
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
The directory "staging" is not connected to any CMS and only consists of .html files.
I am on a shared hosting environment (Hostgator/cPanel).
Thank you.
Use below rule, you are using only L flag but I am modifying it for redirect as you mentioned.
Options -Indexes -MultiViews
RewriteEngine on
RewriteRule ^test/$ /staging/test/page.html [R=301,L]
You want to redirect only the directoy, but not the files in it. The pattern in the rule says match the directory test/ followed by anything .*, including any files.
To match the directory alone, the pattern must stop at test, which is accomplished by an end of string anchor
RewriteRule ^test/$ /staging/test/page.html [R,L]
Instead of an absolute path, you can keep the relative path and add a RewriteBase directive
RewriteBase /staging
RewriteRule ^test/$ test/page.html [R,L]
If you want to rewrite only, leave out the R flag.

Apache invisible URL Rewrite to subdirectory

I want to use URL Rewrite to rewrite all requests on my domain to a subdirectory. But the visitor hould not see, that it is a subdirectory. I tried a view things, but nothing worked really good. Can you help me? In this subdirectory is also a .htaccess file and this file shouldn't be ignored by the server.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule (.*) sub-directory/$1 [L]

htaccess mod_rewrite URL within subfolder

My question seems to be unanswered on StackOverflow, so here goes:
I want to rewrite the following URL using an htaccess file which is in the root folder.
The URL to rewrite is this:
http://www.domain.com/subfolder/item/12345
to this:
http://www.domain.com/subfolder/item.php?id=12345
However nothing I seem to do works. I can successfully rewrite the item.php URL if it is in the root folder using this:
RewriteRule ^item/(.*)$ item.php?id=$1 [NC,L]
... but not if the item.php file is in a subfolder!
You can use following .htaccess in your subfolder:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subfolder
RewriteRule ^item/(.*)$ item.php?id=$1 [NC,L,QSA]
Your rewritten URL would become: http://domain.com/subfolder/item/123
Here is an example for sending this URL (The one entered in the browser address bar):
http://www.domain.com/subfolder/item/12345 (Can be any number) to this rewrited URL:
http://www.domain.com/subfolder/item.php?id=12345
RewriteEngine on
RewriteRule ^subfolder/item/([0-9]+)/?$ subfolder/item.php?id=$1 [L]
Those directives will work only if there is a subfolder directory and in that directory there is in fact a file item.php