htaccess rewriteRule not displaying new url - php

My question is how to get the URL in the RewriteRule to display when I click the link (domain.com/?brandID=1&name=Beretta).
My tests make me believe it might have something to do with index.php, but maybe not. My script is triggered by the GET variable being set, and queries the DB and includes the page that displays the results (and the "ugly" url).
Here's the .htaccess code:
# Rewrite for ?brandID=1&name=Name
RewriteRule ^pistol-brand/([0-9]+)/([0-9a-zA-Z_-]+) ?brandID=$1&name=$2 [NC,L]
I want to replace domain.com/?brandID=1&name=Beretta with domain.com/pistol-brand/1/beretta
The code above displays the "ugly" url, but when I change the url to what the RewriteRule states (domain.com/pistol-brand/1/beretta), the page works.
How do I get the "new" url to display when the link is clicked?
Thanks for any assistance.

You need one more to redirect the ugly url to sef format
##1)Redirect /?brandID=foo&name=bar to //pistol-brand/foo/bar##
RewriteCond %{THE_REQUEST} /\?brandID=([^&]+)&name=([^\s&]+) [NC]
RewriteRule ^ /pistol-brand/%1/%2? [L,R]
##2)Rewrite /pistol-brand/foo/bar to /?brandID=foo&name=bar##
RewriteRule ^pistol-brand/([0-9]+)/([0-9a-zA-Z_-]+) ?brandID=$1&name=$2 [NC,L]

Related

Rewrite Mod Still Returns Original URL

Using WamServer , I used Generate It! to over write a URL like
The original URL:
http://localhost/CMS/foo.php?id=2&slug=eyeglasses
The rewritten URL:
http://localhost/2/eyeglasses.php
The rule which site gave me is
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /CMS/foo.php?id=$1&slug=$2 [L]
but this is still returning original URL
http://localhost/CMS/foo.php?id=2&slug=eyeglasses
Can you please let me know what I am doing wrong?
Update
To be more clarify! I have an index.php which contains a link like
echo ''.$theName.'';
now we user landed in foo.php the URL looks like
http://localhost/CMS/foo.php?id=1&slug=eyeglassess
correct? what I would like to do is displaying the
http://localhost/2/eyeglasses.php
instead of original URL
You need a redirect rule to redirect old URL to pretty URL. This code should be in /CMS/.htaccess
RewriteEngine On
RewriteBase /CMS/
# redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /CMS/+foo\.php\?id=([^\s&]+)&slug=([^\s&]+) [NC]
RewriteRule ^ %1/%2.php? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)\.php$ foo.php?id=$1&slug=$2 [L,NC,QSA]
However it is better to change your link code to new pretty link scheme.

Specificity when redirecting dynamic PHP urls via htaccess

I'm changing a site layout from example.com/product.php?id=1 to example.com/new.php
I need to leave a rule for 301 redirect for this redirection from id=1 to new.php.
What I have, that WORKS currently
RewriteCond %{query_string} id=1
RewriteRule (.*) https://example.com/new.php? [R=301,L]
Problem Needs Solving:
There is also a page called example.com/other-products.php?id=1
All the products on this OTHER page are ALSO being redirected to 'new.php'.
Final Question:
How can I apply specificity to this redirect such that only dynamic urls that are preceded by products.php are redirected and all other dynamic urls on the site with identical id number (e.g. id=1) stay put?
Thanks!
You need to use correct URI pattern in RewriteRule:
RewriteCond %{query_string} ^id=1$ [NC]
RewriteRule ^product\.php$ https://example.com/new.php? [R=301,L,NC]
Take note of RewriteRule ^product\.php$, which will only affect /product.php?id=1 URL but won't affect /product-abc.php?id=1 OR /product.php?id=123

How to write an htaccess 301 Redirection rule to redirect from php to html

I have a problem (it's probably a simple one but I've never had the need to write regex)
A SEO specialist told me to make pretty URLs so I did with the .htaccess file the CMS provides.
But now he requires me to redirect the old URLs to new ones.
This doesn't work
RewriteRule ^index.php?page=kontakt$ /kontakt.html [R=301,L]
and also this (wich was supposed to redirect to the main page from the index.php file)
RewriteRule ^index.php$ / [R=301,L]
has resulted in sitename.com/?page=kontakt, so now I also have to redirect this.
How do I fix this?
RewriteRule only matches the base URL without the query string. You need an additional RewriteCond for it to work.
RewriteCond %{QUERY_STRING} ^page=kontakt$
RewriteRule ^index.php$ /kontakt.html [R=301,L]
EDIT:
Apparently query string gets preserved in this case, so you're probably getting /kontakt.html?page=kontakt
To discard original query string you need to put ? after URL.
RewriteRule ^index.php$ /kontakt.html? [R=301,L]

How to remove Index.php on Select set of URLs Matching Pagination and Component Parameter

Though index.php is required in every set of url. However, there are 2 sections in which our original url in pagination pages were linked with non index.php
However, due to upgradation of cms, the url set has become with index.php
This only needs to be done on pages having page tag and component only as com_news / com_toyschemes.
Original Correct URL
http://www.abc.com/?page=5&&option=com_latestnews&view=list&Itemid=4
http://www.abc.com/?page=2&&option=com_toyschemes&view=list&Itemid=3
Incorrect URL - which needs to be 301 redirect to Original URL
http://www.abc.com/index.php?page=5&&option=com_latestnews&view=list&Itemid=4
http://www.abc.com/index.php?page=2&&option=com_toyschemes&view=list&Itemid=3
Can someone help on this.
Edit - Above url are for illustration. There are 100 of pages with pagination like this in above.
Add this rule to your .htaccess that I'm assuming is in your web root / directory
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteCond %{QUERY_STRING} (^|&)page=\d+(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)option=com_(latestnews|toyschemes)(&|$) [NC]
RewriteRule ^ /?%{QUERY_STRING} [R=301,L]

htaccess Redirect to same file, but display different URL without having to change internal links?

I currently have links on my website in the form of:- http://example.com/products.php?cat=X
I want that once taken to the destination of the link, the url displays something like:- http://example.com/new.
The file on my server is the same file, but only the url should appear neater without me having to change any of the links.
I tried the following, but found it only useful if I explicitly changed the internal links:-
RewriteRule ^new$ products.php?cat=10
RewriteCond %{QUERY_STRING} =cat=10
RewriteRule ^products.php$ new [R,L]
RewriteRule ^new$ products.php?cat=10 [L]
Check the original requested URL in the request line:
RewriteCond %{THE_REQUEST} ^GET /products\.php\?([^&]*&+)*cat=X
RewriteRule ^products\.php$ /new? [L,R=301]
But it will probably be easier if you do that with PHP (see $_SERVER['REQUEST_URI']).

Categories