Regex rewrite rule for filename with dash - .htaccess

I got a filename like jquery.form.min.3.51.0-2014.06.20.js and want to change the following my rewrite rule to remove the dots, the dash and the digits.
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
</IfModule>
The rewrite rule does it jobs on filenames like scripts.min.4.4.2.js which is forwarded to scripts.min.js. But for the filename at the top, with the dash inside between the digits, the rule won't work.
My regex knowledge is too limited for this case. Could anybody give me a hint please?

You can use:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.[\d-]+\.(js|css|png|jpg|gif)$ $1.$2 [L]
</IfModule>

Try the following:
RewriteRule ^(\D+)[\d.-]+(js|css|png|jpg|gif)$ $1$2 [L]

Related

how to create htaccess to modifies url with 2 parameter

Please someone help me to edit my htaccess.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^supplyer/([0-9]+)/([0-9a-zA-Z_-]+) supplyer.php?page=$1&id=$2 [NC,L]
</IfModule>
What I want:
1. every file .php remove .php
rewrite URL
2. supplyer.php?page=edit&id=120 to supplyer/edit/120
for the fist one it work properly but the second it not work someone help me.
To match the /supplyer/edit/120 uri string , your pattern should be ^supplyer/([0-9a-zA-Z_-]+)/([0-9]+)
Try :
RewriteRule ^supplyer/([0-9a-zA-Z_-]+)/([0-9]+)/?$ supplyer.php?page=$1&id=$2 [NC,L]

URL rewrite via htaccess

I've to change this url : http://localhost:8888/finance/schedule/?location=new_york
to http://localhost:8888/finance/schedule/location/new_york
It's a wordpress site, my htaccess code is :
RewriteRule ^schedule/location/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\$ schedule/?location=$1
But nothing seems to happen, please suggest.
The whole htaccess file code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /finance/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /finance/index.php [L]
RewriteRule ^schedule/location/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\$ schedule/?location=$1
</IfModule>
This line is stopping it from ever reaching your later rule:
RewriteRule . /finance/index.php [L]
The [L] means "last," as in "this is the last rule to run if it matches."
And \$ looks for a literal $, not the end of the string.
You also have two many matching groups in your rule.
You need something like this, instead (totally untested):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /finance/
RewriteRule ^index\.php$ - [L]
RewriteRule ^schedule/location/([a-zA-Z0-9_-]+?)/?$ /wp-content/themes/financeinstitute/page_schedule.php/?location=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /finance/index.php [L,QSA]
</IfModule>

Make url-rewrite in .htaccess file to work both ways

I have the following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlPram=$3 [L,NC]
</IfModule>
when my url format is something like:
mysite.com/project/1/test/vw
everything works fine. But I would also like when the url is typed in the other format, to be rewritten to the first format.
i.e. When I type the url like this:
mysite.com/project.php?id=1&pName=test&urlParam=vw
I wants it to be turned into:
mysite.com/project/1/test/vw
Do I have to write another rule? or is it accomplished in some other way?
The current rewrite rule would not handle both the cases. Add the below rewrite rule as well.
RewriteEngine On
RewriteBase /mysite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlParam=$3&no-redir [L,NC,QSA]
RewriteCond %{QUERY_STRING} !no-redir
RewriteCond %{QUERY_STRING} (^|&)id=([^&]+)&pName=([^&]+)&urlParam=([^&]+)
RewriteRule ^project\.php$ project/%2/%3/%4? [R=301,L,NC]
You need one more external redirect rule. Use this code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} /project\.php\?id=([^&]+)&pName=([^&]+)&urlPram=([^&\s]+) [NC]
RewriteRule ^ project/%1/%2/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlPram=$3 [L,NC,QSA]
</IfModule>

What's wrong with this .htaccess redirect?

I want to redirect (only) mysite/02B /030 etc to theirsite/02B etc.
What's wrong with this? I have the sense that I'm missing something stupidly obvious. :-/
RewriteCond %(REQUEST_URI) ^0(2B|2Y|2C|2Z|2-|30|33)$
RewriteRule ^(.*)$ http://theirsite/$1 [R=301,NC,L]
Thanks!
The leading slash / is included in the REQUEST_URI variable, so you have to include it also in the regular expression to match the variable.
You may try this complete rule-set instead:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/0(2B|2Y|2C|2Z|2-|30|33) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://theirsite/$1 [R=301,NC,L]

Conditional rewriting in htaccess file

I'm rather new to the whole .htaccess thing and I'm using the following right now to use 'pretty url's':
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
</IfModule>
Now i found my website a bit slow and decided to start gzipping my CSS files thru a php script I found somewhere on the web (the website). For this to work I need to rewrite the url to open the correct php file. That would look something like this:
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
But I only want the first to happen when the second doesn't and vice versa. In other words i'd like something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
if request doesn't contain .css do
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
else do
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
</IfModule>
Can anyone help me with the proper code or a place where i can find a way to use some kind of conditional statement in htaccess files?
Thanks in Advance!:)
Try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !.css
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
</IfModule>
Also, you can put the last rule on top

Resources