How to remove md5 from the filename in the requested URL for the file name without md5 using apache mod_rewrite? - apache

Example:
This => URL request ./any_folder/main.d25a1b054a0b0fbeb3def5a0ff50d01e.min.js
For this => URL request ./any_folder/main.min.js
My htaccess:
RewriteEngine On
# Remove md5 from asset files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+).([a-fA-F0-9]{32}).(.+)?(js|css)$ $1.$3 [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The above configuration does not work.

Have your htaccess rules file following manner. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Remove md5 from asset files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##RewriteRule ^([^.]*)\.(?:[[:alnum:]]{32})\.([^.]*)\.(js|css)/?$ $1.$2.$3 [L]
##Since following has worked for OP, so adding it here, commenting above.
RewriteRule ^(.+).([a-fA-F0-9]{32}).(.+)?(js|css)$ $1.$4 [L]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Related

pretty url with folder name and two variables

ugly url - example.com/folder_name/art.php?id=9&s=something
want to be - example.com/folder_name/id/s
here is my try
# external
RewriteCond %{THE_REQUEST} \s/+art\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ art.php?id=$1&s=$2 [L]
doesn't work
entering ugly url - address bar is unchanged
entering pretty url - getting error 404
pls help
You rules will have to allow folder_name as well so use:
RewriteEngine On
# external
RewriteCond %{THE_REQUEST} \s/+(folder_name)/art\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(folder_name)/([\w-]+)/([\w-]+)/?$ $1/art.php?id=$2&s=$3 [L,QSA,NC]
This assumes there is no other .htaccess in folder_name/.
In case there is a .htaccess inside that subfolder then use this .htaccess:
RewriteEngine On
RewriteBase /folder_name/
# external
RewriteCond %{THE_REQUEST} /art\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ %1/%2? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ art.php?id=$1&s=$1 [L,QSA]

I need to remove language code from URL using htaccess

I have two language /ru and /uk. /uk is the default.
I need to remove /uk from the URL.
For example:
www.example.com.ua/uk/category
to
www.example.com.ua/category
But any URL with /ru must not change. ie. www.example.com.ua/ru/category.
htaccess look like
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^init.php$ - [F,L,NC]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(favicon|apple-touch-icon-|homescreen-|firefox-icon-|coast-icon-|mstile-).*\.(png|ico)$ - [R=404,NC,L]
RewriteCond %{REQUEST_URI} ^api/(.*)$ [or]
RewriteCond %{REQUEST_URI} .*/api/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*api/(.*)$ api.php?_d=$1 [L,QSA]
RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml)$ [NC,or]
RewriteCond %{REQUEST_URI} store_closed.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/(.*)$ $2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
In order to remove the /uk path segment from the start of the URL-path then you would need to do something like the following at the top of your .htaccess file:
RewriteRule ^uk/(.*) /$1 [R=302,L]
This does assume that you have already modified all your internal links and canonical tags to remove the /uk path segment.
If this is intended to be permanent then change the 302 (temporary) redirect to 301 (permanent) only once you have confirmed that everything works as intended, so as to avoid potential caching issues.

.htaccess: Redirect Trailing Slashes

I have a rewrite rule below in my .htaccess file in the root directory:
RewriteEngine On
# Exclude .css / .js / ...
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
# Rewrite Rule
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&product=$2
So the URL http://example.com/data1/data2 give me category=data1 and product=data2.
The problem is that the below URLs are not working:
http://example.com/data1 # Not Working (Page Not Found)
http://example.com/data1/data2/ # Not Working (Page Not Found)
But these URLs are working:
http://example.com/data1/ # Works -> category=data1
http://example.com/data1/data2 # Works -> category=data1 & product=data2
How can I redirect the first two URLs to the second one?
OR/AND
Do something that all URLs are redirect to the non-trailing slash one. So the URLs below:
http://example.com/data1/
http://example.com/data1/data2/
are redirect to these:
http://example.com/data1
http://example.com/data1/data2
To avoid trailing slashes alltogether, do a redirect
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]
To match a URL with just one element, you might use a second rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1&product= [L]
or
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1 [L]
Putting all together gives
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]
RewriteRule ^(.+?)/(.+)$ /index.php?category=$1&product=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1 [L]

htaccess: redirect of index.php

Sorry for not-very-clear title.
I'm using FatFreeFramework with his .htaccess:
RewriteCond %{REQUEST_URI} \.ini$
RewriteRule \.ini$ - [R=404]
#RewriteCond %{REQUEST_URI} \.html?$
#RewriteRule \.html?$ - [R=404]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Now, restyling an old site, I have in Google some url codified like
www.example.com/index.php?ID=12
I would strip away that and redirect them to home page.
I don't know how write the rule, I did try to edit in this mode but without luck:
RewriteCond %{REQUEST_FILENAME} !-d
Redirect 301 /index.php?ID=$1 index.php
RewriteRule .* index.php [L,QSA,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Right above this rules:
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Add this:
RewriteCond %{THE_REQUEST} \ /+index\.php\?ID=[0-9]
RewriteRule ^ / [L,R=301]
You can't match against the query string using mod_alias's Redirect directive.

Ignore files in non-existent paths using .htaccess

I am having a problem with rewriting.
Here is my .htaccess-file
RewriteEngine On
RewriteBase /
#ignore non-existent utility-files
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* - [L]
#load existent utility files and don't rewrite them to index.php
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$
RewriteRule ^(.*) $1 [L]
#load other files if they exist
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
#load index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
The last part is working but I want to avoid that my index.php is loaded when the code requests a non-exisiting image (as: www.mysite.test/images/foobar.png)
The first RewriteCond-Line should check if the request matches a file of image, JavaScript, and the like and the second RewriteCond line should check if the file does NOT exist.
The third one should check if the folder exists.
If the match succeeds NOTHING should be loaded.
It works but only if the folder exists. If the folder if the image doesn't exist it doesn't work.
You have many redundant rules. You can simplify the to:
RewriteEngine On
RewriteBase /
#ignore non-existent utility-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$ [NC]
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Ok there was still an issue with existing files, as Member anubhava said.
My final solution is:
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteBase /
#load files if they exist
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
#ignore non-existent utility-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt|php)$ - [L]
#load index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
This solution includes php-files in the second rewrite-rule. Because I have some php-files that are actually css-files and loaded by the browser.