Replacing space from special urls using htaccess - .htaccess

I'm able to replace all %20 with - from url by using :
.htaccess :
RewriteRule "^(\S*)\s+(\S*)$" /$1-$2 [L,NE,R=302]
RewriteRule "^(\S*)\s+(\S*\s+.*)$" $1-$2 [L]
# remove multiple hyphens
RewriteRule ^(.*)-{2,}(.*)$ /$1-$2 [L,R=302]
Now i want to do this work for only desired urls Not all , url's like like :
Example.com/blog/example%title => example.com/blog/example-title
Example.com/product/example%product => example.com/product/example-product
How i can do this using .htaccess ?
EDIT :
Main Problem is that when i upload images to server with % in name like image 1.jpg url will redirect me to a name like image-1.jpg and server can't find image to show
EDIT 2
my .htaccess have this codes beforehand:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

You can place known prefixes in your rules:
RewriteRule "^((?:blog|product)/\S*)\s+(\S*)$" /$1-$2 [L,NE,R=302,NC]
RewriteRule "^((?:blog|product)\S*)\s+(\S*\s+.*)$" $1-$2 [L,NC]
# remove multiple hyphens
RewriteRule ^((?:blog|product)/.*)-{2,}(.*)$ /$1-$2 [L,R=302,NE,NC]

Related

Redirect rule to change url parameters with slashes / in .htaccess file

I need to change url parameters with slashes.
My current working url is
https://www.example.com/Blog-Details.php?blog=test-title
I need to change it like below
https://www.example.com/Blog-Details.php/blog/test-title
My htaccess file is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#RewriteRule ^([a-z_]+).html $1.php
RewriteRule ^([A-Za-z_-]+).html $1.php
RewriteRule ^([a-z]+)/([a-z]+)/(.*)\.html /market_material.php?ind=$3
#RewriteRule ^([A-Z]{2})/([\+a-z\ ]+)\.html$ /reports_content.php?
report_id=$1
#RewriteRule ^report/([0-9]+) /reports_content.php?report_id=$1
RewriteRule ^([0-9]+)/(.*)\.html /reports_content.php?report_id=$1
#RewriteRule ^blog/([0-9]+) /Blog-Details.php?blog=$1
#RewriteRule ^Blog-Details.html/([^/]+)?$ $1.php?blog=$2 [L]
RewriteRule ^([^.]+?)/?$ /Blog-Details.html?p=$1 [NC,L]
</IfModule>
I added this line to convert parameters with slashes
RewriteRule ^([^.]+?)/?$ /Blog-Details.html?p=$1 [NC,L]
Try using the following RewriteRule in your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} blog=(.+) [NC]
RewriteRule ^(.*)$ http://www.example.com/Blog-Details.php/%1? [R=301, NC, L]
This is grabbing your variable blog= using a condition and then adding it onto the end of your URL using %1. You'll notice I've also added a ? onto the end of the RewriteRule, this is to stop the original query string from appearing on the end of the URL.
The rewrite works by using 301 redirection, you might want to change this to 302 to make it a temporary redirect while testing.
Make sure you clear your cache before testing this.

mod_rewrite force trailing slash except if url had .html extension

i have a problem here..
i have a htaccess like this
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# Rewrite cuiy
RewriteEngine On
# Ensure all URLs have a trailing slash.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://store.kiosban.com/$1/ [L,R=301]
RewriteRule ^produk/([^/]*)/([^/]*)\.html$ /template/single-product.php?produk=$1&slug=$2 [L] #single produk
RewriteRule ^produk/([^/]*)$ /template/product.php?produk=$1 [L] #archive produk
RewriteRule ^produk/$ http://www.store.kiosban.com/produk/all [R=301,L]
RewriteRule ^produk/([^/]*)/([^/]*)$ /template/product.php?produk=$1&page=$2 [L] #archive produk
RewriteRule ^filter/([^/]*)/ukuran/([^/]*)$ /template/search.php?filter=$1&ukuran=$2 [L] #search by size
RewriteRule ^filter/([^/]*)/ukuran/([^/]*)/([^/]*)$ /template/search.php?filter=$1&ukuran=$2&page=$3 [L] #search by size
RewriteRule ^filter/([^/]*)/oem/([^/]*)$ /template/search.php?filter=$1&oem=$2 [L] #search by oem
RewriteRule ^filter/([^/]*)/oem/([^/]*)/([^/]*)$ /template/search.php?filter=$1&oem=$2&page=$3
RewriteRule ^checkout/$ /template/checkout.php
RewriteRule ^checkout/2/ /template/product.php
RewriteRule ^login/$ /template/mlebu.php
RewriteRule ^daftar/$ /template/register.php
It works if i insert a url like this
http://store.kiosban.com/produk
It will become like this
http://store.kiosban.com/produk/
BUt the problem is when the url is like this
http://store.kiosban.com/produk/accesories/accesories.html
It will become like this too
http://store.kiosban.com/produk/accesories/accesories.html/
My question is.. how to add the trailing slash when the url is not a .html file...
http://store.kiosban.com/produk
Become
http://store.kiosban.com/produk/
And
http://store.kiosban.com/produk/accesories/accesories.html
Still being
http://store.kiosban.com/produk/accesories/accesories.html
any suggestion??
You'll need to add this to your condition-set:
RewriteCond %{REQUEST_URI} !\.html$
That should prevent the rules from running if the URL ends in .html.

How to replace "-" with "_" and remove index.php from the address bar

I have developed my website in codeigniter. I have a couple of things to accomplish in .htaccess file.
For any page/resource other than index.php, img, js, css i want it to add index.php before the actual resource in the url.
I have so many static pages having unserscore "_" in them, e.g, contact_us.php, our_services.php etc. I want when user gives the url like www.mywebsite.com/our-services it should open up the original page, that is, www.mywebsite.com/our_services. There are 1,2 upto 7 underscores for different pages, e.g, mywebsite.com/speech_writing_services
Here is my .htaccess file which i could build up so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/StyleAdmin|/images|/StyleAdmin/images|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5_$6 [R=301,L]
The problem here is that when i give the url like www.mywebsite.com/contact_us it works just fine and shows the same url in the address bar. But when i give this url www.mywebsite.com/contact-us it does show the page but the url shown in the address bar of the browser becomes www.mywebsite.com/index.php/contact_us whereas i want it to be like www.mywebsite.com/contact_us with index.php removed.
You need to do the routing after the redirecting.
RewriteEngine on
RewriteRule ^([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5_$6 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/StyleAdmin|/images|/StyleAdmin/images|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]

.htaccess redirect from 1 url (no matter what file/folder) to another url

I have a website http://rochesterwaterskishow.com which they've recently changed their name so they want to update their url to http://skidox.com. I'm trying to redirect any page from rochesterwaterskishow.com to skidox.com/site/index.
I have this line of code which redirects http://rochesterwaterskishow.com to http://skidox.com, but if I go to something like http://rochesterwaterskishow.com/test, it doesn't redirect to http://skidox.com.
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
How can I make it a catch all so anything rochesterwaterskishow.com/* gets redirected to skidox.com/site/index?
UPDATE: Full .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
That's because the search pattern ^$ will only match a URI path of "/". You need to pick up the request in a match variable, for example:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/site/index/$0 [R=301,L]
I am assuming that you are using SEO optimised-style URIs for the new site. If you want to simply redirect everything to the index page without any context, then you still need a pattern that matches:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^ http://skidox.com/site/index [R=301,L]
Update following post of full htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/$0 [R=301,L]
RewriteCond $0 ^(index\.php$|robots\.txt$|resources)
RewriteRule ^.* - [S=1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index/$1 [R=301,L]

%2520 (Double space) in URL for URL with spaces

My URL structure is like
http://www.example.com/folder/index.php?dir=dir1
To be able to access it from
http://www.example.com/folder/dir1
and at the same time redirect the 1st URL to 2nd one, my htaccess (in 'folder') is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]
The trouble is that if any directory name in URL contains a 'space', the URL shows %2520 instead of the 'space'.Please advice in modifying the htaccess so it shows %20 or preferrably a simple 'space'?
try adding a NE on the redirect ie
RewriteRule ^ %1? [L,R=301,NE]
EDIT
Since you've read my htaccess, do you see any possiblity of shortening it further
Below are a couple of comments
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]
#this looks redundant with last rule, and could be deleted?
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
#this says if not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
#and this says if it IS an existing directory
#Is this what you wanted, or should it be not an existing directory i.e
# RewriteCond %{REQUEST_FILENAME} !-d instead
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]

Resources