How to replace word in url using htaccess? - .htaccess

i want to replace a word from url. i try many code but. not working
RewriteRule ^(.*)/resetter/(.*)$ $1/reseter-de-puces/$2 [L]
i want to redirect
catalogue-encros/resetter/epson/resetter-pour-epson-ancienne-génération/8345-37457
to
catalogue-encros/reseter-de-puces/epson/resetter-pour-epson-ancienne-génération/8345-37457
i have added www redirect which is work fine
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

You are missing a / before the $1, try:
RewriteRule ^(.*)/resetter/(.*)$ /$1/reseter-de-puces/$2 [L]
Without the / before $1 your would url would have turned into something like:
http://domain.com/www/var/home/domain.com/somedirectory/reseter-de-puces/

Related

Remove index.php keep parameter only

I have this url http://subdomain.domain.com/index.php?page=print-items and I want to remove index.php?page= and only keep what comes after it.
I tried this but it didn't work. it only removes index.php and keeps ?page=print-items
RewriteEngine On
RewriteRule ^([a-z0-9]+) index.php?page=$1
Could you please try following, written and tested as per shown samples.
RewriteEngine ON
RewriteCond %{THE_REQUEST} index\.html\s [NC]
RewriteRule ^(.*/?)/?$ index.html?page=$1 [NC,L]
OR if you are hitting usre friendly URL and you want to redirect in backend into index.html URL with query string then try following.
RewriteEngine ON
RewriteCond %{THE_REQUEST} !index\.html\s [NC]
RewriteRule ^(.*/?)/?$ index.html?page=$1 [NC,L]

Cut off parameter from redirect in htaccess

This silent redirect in htaccess:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]
redirect a url that looks like:
example.com/album_showpage.php?pic_id=1906
to:
example.com/gallery/image_page.php?image_id=1906
that works great. But when the URL has a parameter like:
album_showpage.php?pic_id=1906&mode=prev
or
album_showpage.php?pic_id=1906&mode=next
the redirect wont work.
Question: How to cut of any parameter after pic_id=1906
thank you
You need match against the rest of the query string.
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)(&.*)?$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1%2 [L]
If you don't want the mode=prev stuff to be included in the rule's target, then you can simply remove the $ instead of attempting to match against it:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+) [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]

How to remove a directory from URL permanently

I have my website under directory alpha in WWW.
so my url are like: www.mydomain.com/alpha/*
I want users to see just www.mydomain.com/*
RewriteCond $1 !^alpha/
RewriteRule ^(.*)$ /alpha/$1 [L]
Is working correctly for just www.mydomain.com. But beyond that when I click on any link alpha comes back in the URL.
I want to alpha to be completely hidden from users.
Changing the links is the best solution, but if you cannot do so then you need to add another rule to send requests with /alpha to the URL minus /alpha as below.
RewriteEngine On
RewriteBase /
#if request starts with alpha
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /alpha/([^\ ])* [NC]
#redirect to without alpha
RewriteRule . http://%{HTTP_HOST}/%1 [L,R=301]
RewriteCond $1 !^alpha/
RewriteRule ^(.*)$ /alpha/$1 [L]

How do i do a 301 redirect to a specific location

I want to remove the slash of 1 and only 1 url
this snippet will remove them all
# Remove the trailing slash
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
but i just want to change
example.com/changeme/
to
example.com/changeme
any ideas how to change this htaccess i have to only do it on one
You could replace (.+) with (changeme) in RewriteRule:
RewriteRule ^(changeme)/$ http://www.example.com/$1 [R=301,L]
This will only match 'changeme' and not everything.
In this way you can also match multiple URLs, including e.g. 'changeother' and 'foobar':
RewriteRule ^(changeme|changeother|foobar)/$ http://www.example.com/$1 [R=301,L]
How about this?
RewriteRule ^changeme/$ http://www.example.com/changeme [R=301,L]
All you need is (as long as this rewrite is only applied to example.com):
RewriteRule ^changeme/$ changeme [R=301,L]

Simple htaccess redirect - subdomain to folder

I am trying to redirect http://[random-string].domain.com/ to http://domain.com/folder/[random-string]/
I currently have this:
RewriteCond %{HTTP_HOST} ^.*\.domain\.com
RewriteRule ^.*$ http://domain.com/folder/$1/ [R=301,L]
It currently points to http://domain.com/folder//. (The $1 is missing) How do I fix this?
You need to use parenthesis to grab the value matched, in your case:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/folder/%1/$1 [R=301,L]
assuming you also want to redirect http://[random-string].domain.com/something to http://domain.com/folder/[random-string]/something
how i fix it?
my subdomain not is randomically
have where the replace for $1 ?
cheers
RewriteCond %{HTTP_HOST} ^(.).domain.com
RewriteRule ^(.)$ http://domain.com/folder/%1/$1 [R=301,L]

Resources