Mod rewrite is creating duplicate links - .htaccess

I have URL on my site like http://costcut.in/view-store.php?id=2 (Link 1)
I want to change it to http://costcut.in/2 (Link 2)
I have used the following lines in my .htaccess file
RewriteEngine on
RewriteBase /
RewriteRule ^/?([0-9]+)$ view-store.php?id=$1 [L]
Now I am able to access both Link1 and Link2 But I want the url Link 1 to be redirected to Link 2
How can I change the links from homepage and other pages to point to Link 2 instead of Link 1?

You'd need to work on raw request for that to work.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /view-store\.php\?id=(\d+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^(\d+)$ view-store.php?id=$1 [L]

Related

how to hide id = from url using htaccess file

I need the url
from
localhost/project/category?c=electronics
to
localhost/project/category/electronics
I have tried
RewriteRule ^category/([^/\.]+)?$ /category.php?c=$1 [L]
RewriteRule ^category/+?$ /category.php?c=$1 [NC,L]
With your shown samples and attempts please try following htaccess rules. Please do clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
##External redirect to url change in browser.
RewriteCond %{THE_REQUEST} \s/(project/category)\.php\?c=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite to category.php in backend.
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ %{DOCUMENT_ROOT}/$1/$2.php?c=$3 [QSA,L]
RewriteEngine on
RewriteBase /
RewriteRule ^project/category/([0-9a-z]+)$ /project/category?c=$1 [L]
Why is "project/" missing in your original try ?
You have to specify the full path.
You can try this simple rewriteRule wich should works.

php - Htaccess as fake directories

I am making a mini blog that could make it's url looks like this:
From: http://127.0.0.1/index.php?post=the-story-of-us
To: http://127.0.0.1/view/the-story-of-us
I have tried this but i'm getting 404 not found.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?post=([^&]+)
RewriteRule ^ /view/%2/? [L,R=301]
Your current rule only handles the case: Redirect old url to new url.
(By the way, +1 for using THE_REQUEST to avoid a redirect loop)
You also need to handle the case: Rewrite (internally) new url to old url.
Here is how your htaccess should look like
RewriteEngine On
# Redirect /index.php?post=XXX to /view/XXX
RewriteCond %{THE_REQUEST} \s/index\.php\?post=([^&\s]+)\s [NC]
RewriteRule ^ /view/%1? [L,R=301]
# Internally rewrite back /view/XXX to /index.php?post=XXX
RewriteRule ^view/([^/]+)$ /index.php?post=$1 [L]
I do not udnerstand your RewriteCondition, but the RewriteRule should look like this:
RewriteEngine on
RewriteBase /
RewriteRule ^view/(.*)/? ./index.php?post=$1 [L,R=301]

.htaccess url rewrite not working

here i am trying to rewrite url but its not working
> Options +FollowSymlinks RewriteEngine on rewriterule
> ^FAQ.php?view=why_best(.*)$ http://www.bestsports.ca/FAQ/WhyBest$1
> [r=301,nc]
old url is http://www.bestsports.ca/FAQ.php?view=why_best
New Url http://www.bestsports.ca/FAQ/WhyBest should be like this
after editing htacces fiel its opening the same old url please help
thanks
i am trying this code now
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+FAQ\.php\?view=why_best [NC]
RewriteRule ^ /FAQ/WhyBest/? [R=301,L]
RewriteRule ^FAQ/WhyBest/?$ /FAQ.php?view=why_best [QSA,NC,L]
RewriteRule ^ /FAQ/SizeChart/? [R=301,L]
RewriteRule ^FAQ/SizeChart/?$ /FAQ.php?view=size_chart [QSA,NC,L]
but its showing error
The page isn't redirecting properly
and if i click same link again for FAQ.php?view=why_best its
redirected to
http://www.bestsports.ca/FAQ/WhyBest/FAQ.php?view=why_best
as you helped me i completed the above seo url rewrite problem for my all other website pages
i used the third party cart system from ecwid when i click on any proudct its show this link or url
http://www.bestsports.ca/product.php#!/~/product/category=9414048&id=11484861
how can i rewrite this url insted of showing category id and i want to show name
i already tried your above instruction but its not working can you please explain is it possible to make this url seo friendly url
thank you
You cannot match query string in RewriteRule. Use RewriteCond instead:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+FAQ\.php\?view=why_best [NC]
RewriteRule ^ /FAQ/WhyBest? [R=301,L]
RewriteRule ^FAQ/WhyBest/?$ /FAQ.php?view=why_best [QSA,NC,L]

Making URL different from orginal

My site is: http://binodgiri.com.np, when you click on link you are redirecte to: http://www.binodgiri.com/what.php?id=1
I want to display above url as
http://www.binodgiri.com.np/change-url/
I tried following on .htaccess file
RewriteEngine On
RewriteRule ^change-url/(.*)$ what.php?id=1 [L]
(I don't have much knowledge on this stuff.)
Try this, it should redirect http://www.binodgiri.com/what.php?id=1 to http://www.binodgiri.com.np/change-url/
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /what\.php\?id=1\ HTTP
RewriteRule ^ /change-url\? [R,L]

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

Resources