How do I rewrite an id to a name via htaccess - .htaccess

I am trying to rewrite the title ID through htaccess but it does not work. I used several different codes in htaccess but it does not work.
When I went to the url by name it did not show me any post, but with ID it works very well.
I am using this code in htaccess:
RewriteRule ^([0-9a-z]+)/?$ news-details.php?nid=$1 [NC,L,QSA]
ID for posts/categories:
127.0.0.1/new/news-details.php?nid=89
127.0.0.1/new/category.php?catid=9

Yes it works through the id "/new/89" you mentioned but not with url "/new/89/name-news"
Then change the rule (the regex) to allow /<id>/<slug> (no trailing slash), for example:
RewriteRule ^(\d+/[\w-]+)$ news-details.php?nid=$1 [NC,L,QSA]
<id>/<slug> is then past as the value of the nid URL parameter.

Related

I changed the structure of my site to reach index cards

Excuse me for my english.
I make a brands directory web site.
Before to acces to the brands pages I use requests like this :
mydomain.com/fiche.php?id=115
where id is the id of the brand in my directory
I change the structure of the brands pages and now use this request:
mydomain.com/annuaire.php?type=fiche&id_marq=115
where id has become id_marq
I try to use a rewritebrule like this:
RewriteRule ^fiche.php$ http://www.annuaire-sites-officiels.com/annuaire.php?detail=fiche&id_marq=$1 [L,QSA,R=301]
to redirect the old links to the new pages but result dont pass the id_marq value and the url is:
http://www.annuaire-sites-officiels.com/annuaire.php?detail=fiche&id_marq=&id=115
&id= is too.
What am I doing wrong?
Your rule is not evaluating query string and that's why its not capturing id query parameter.
Change your code to:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC]
RewriteRule ^fiche\.php$ /annuaire.php?detail=fiche&id_marq=%1 [R=302,L,QSA,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Check out Regex Back Reference Availability:
You have to capture the query string. [QSA] passes it forward unaltered, so unless you're using id for anything you don't need that bit of code. Your 301 redirect is correct since this is a permanent redirect. Remember if you add a failed redirect your browser may cache that redirect so it might not look like it's working.
In this string match I'm only catching numbers to prevent someone from passing something like an asterisk * and XSS exploiting your site.
I've not included and [NC] matches in my code because when you allow multiple cases they can seem like different URLs to search engines (bad for SEO).
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^fiche.php$ http://%{HTTP_HOST}/annuaire.php?detail=fiche&id_marq=%1 [R=301,L]

301 redirect based on text to new url

I'm having a little problem with an URL. I hope you can help me out!
I have this url:
http://www.domain.com/search.html?page=shop.product_details&flypage=vmj_softy.tpl&product_id=1408&category_id=1028
This needs to be redirected to:
http://www.domain.com/search.html?page=shop.product_details&flypage=vmj_color_plus.tpl&product_id=1408&category_id=1028
The only thing that needs to be changed is the flypage=vmj_softy to flypage=vmj_color_plus
The product ID and Category ID has to be a string (at has to change on all products)
I Think I need to use apaches mod_rewrite for this, but i've tried everything but I'm not able to get this to work!
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=shop.product_details&flypage=vmj_softy.tpl(.*)$
RewriteRule ^/?search.html$ /search.html?page=shop.product_details&flypage=vmj_color_plus.tpl%1 [L,R=301]

Mod_Rewrite for htaccess

The URL structure for Joomla 1.5 changed in version 2.5.
Before it was:
http://example.com/index.php?option=com_content&task=view&id=587&Itemid=73
Now it's:
http://example.com/index.php?option=com_content&view=article&id=587&Itemid=114
*Note the id and itemid numbers change based on page and while all pages have an id not all have the Itemid in the url.
While we updated all links in the database lots of people still have the old link structure so we want to edit the htaccess file so if someone enters the old structure it will forward to the right structure.
Simply put any URL entered at example.com with task=view in the url should be replaced with view=article.
Does anyone know a simply way to do this in htaccess, maybe with replace query string method and 301 redirect?
You can match against the query string using mod_rewrite's RewriteCond and the %{QUERY_STRING} variable. Then use the % backreferences in a RewriteRule
Try something like this:
RewriteCond %{QUERY_STRING} ^(.*)&task=view&(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1&view=article&%2 [R=301,L]

.htaccess: rewrite URL with query strings

I need to rewrite a URL using an .htaccess and the URL has two query strings. Here is my raw URL:
http://domain.com/channel-partners/en/index.php?location=phoenix-az&name=company
How do I get that to be written as:
http://cp.domain.com/phoenix-az/company
RewriteEngine On
RewriteBase /
RewriteRule ^partners/([\w-]+)/([\w-]+)/?$ channel-partners/en/index.php?location=$1&name=$2 [L]
This will redirect URLs in the format partners/location/name with an optional trailing slash to the raw URL you provided. location and name can be alphanumeric, underscore, or dash characters.
Note: I have prefixed the URL with the literal partners. Without it your RewriteRule would be too loose and match nearly all requests. Feel free to change it.
UPDATE
To redirect from your sub-domain, the above needs to be in the .htaccess file for cp.domain.com. You'll need to modify your RewriteRule to the following:
RewriteRule ^partners/([\w-]+)/([\w-]+)/?$ http://domain.com/channel-partners/en/index.php?location=$1&name=$2 [L]
However, I really don't recommend this. Link management is going to be a nightmare. You should really try to have all these links under the same domain. 301 Redirect from the sub-domain if you have to.

htaccess redirect if url contains specific string

We've installed a new shop software and I've set up redirects for all of the old pages, but there's one that I just can't get to work. If the url includes
cp_tpl=productlist.html
then I want to redirect to the main website. So for example the old url could be
http://www.gocellular.ca/index.php?cp_tpl=productlist.html&cp_cat=803&cp_sid=1xxxxxx
and I want to redirect to
www.gocellular.ca
The 'cp_tpl=productlist.html' can be anywhere in the url - basically I just want to check if the string 'cp_tpl=productlist.html' is included anywhere in the url and then redirect. I've tried about 100 different .htaccess re-writes but just can't get this to work! I'd be very grateful for any ideas.... THANK YOU!
In order to catch that string in the query string as you have shown it there you have to use a RewriteCond, as the regular expression doesn't get checked against the query string in a redirect rule. Something like this should do the trick:
RewriteCond %{QUERY_STRING} cp_tpl=productlist.html
RewriteRule .* / [R,L]
The above will keep the query string intact. If yow want to remove the query string, just add a ? after /, i.e.:
RewriteRule .* /? [R,L]

Resources