How to rewrite specific URL using htaccess - php

I've been breaking my head on this for quite some time and I don't see the solution.
I want to rewrite a URL with a GET language parameter to a more clean URL.
For instance:
http://www.example.com?lang=en
Needs to be:
http://www.example.com/en
The above works fine with this rewrite rule:
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L]
But I can't get it to work on URLs like these:
http://www.example.com/contact.php?lang=en
http://www.example.com/about.php?lang=en
That need to be:
http://www.example.com/en/contact.php
http://www.example.com/en/about.php
Anyone have an idea what I'm missing in my rewrite rule to make this work?

You will need an additional rewrite rule for handling /en/about.php:
RewriteEngine On
RewriteRule ^(en|nl|fr|de)/([\w-]+\.php)$ $2?lang=$1 [L,QSA]
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L,QSA]

Related

PHP/Apache rewrite rules with multiple parameters?

I got the difficult when rewrite rules with multiple parameter,to modify a URL to SEO-friendly.
My URL:
http://domain/cat.php?alias=canon&sort=price&page=3
I want to have a rewrite rule so that the following:
http://domain/c/canon?sort=price&page=3
Heres my current rule:
RewriteEngine On
RewriteRule ^c/([a-z,0-9-]+)$ cat.php?alias=$1 [L]
RewriteRule ^c/([a-z,0-9-]+)?sort=([a-z]+)$ cat.php?alias=$1&sort=$2 [QSA]
RewriteRule ^c/([a-z,0-9-]+)?sort=([a-z]+)&page=([0-9]+)$ cat.php?alias=$1&sort=$2&page=$3 [QSA]
I try to get the params but it doesn't work. Anyone have any ideas on which rewrite rules to use?
Thank you!
--hatxi
RewriteRule ^c/([a-z,0-9-]+) cat.php?alias=$1 [L,QSA]
Should be enough. The QSA flag will take care of passing the sort and page parameters.
Your rules don't work because of the [L] flag on the first one, it just discards the rest because it always matches first.

Not able to make seo friendly URL from php using htaccess

I have a problem in creating SEO friendly url actually I have two url rewritten one is working and other one is not. I dont know why? please help
My url is this
http://localhost/quotesnew/author.php?authID=1
and I want it to be
http://localhost/author.html
here is my code in .htaccess file
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /quotesnew/index.php?authchar=$1 [L]
RewriteRule ^author\.html$ /quotesnew/author.php?authID=1 [L]
You can try
RewriteEngine On
RewriteRule ^author/([^/]*)\.html$ /quotesnew/author.php?authID=$1 [L]
Now your url would be
http://localhost/author/1.html
The [L] flag stops rewriting for this cycle. It therefore never reaches the second rule.

create SEO friendly URL using rewriterule

I try to rewrite my SEO URLs to some real GET requests, to handle in my PHP file.
I want to have these 2 cases to work:
mysite.com/company-profile -> index.php?action=company-profile
mysite.com/faq/howcanijoin -> index.php?action=faq&anchor=howcanijoin
I got the first case to work using the rule:
RewriteRule ^([A-Za-z0-9-]+)$ index.php?action=$1
For the second I tried also. I put this rule before the previous one:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2
But it's not working. Any suggestions? If I understand correctly inside each parenthesis goes variables $1, $2 etc?
Do this
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)$ /index.php?action=$1&anchor=$2
This?
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2 [QSA,L]

How to properly rewrite a friendly URL?

I have a URL like index.php?url_id=u5531e3aas01fe5c2 and I also want to make it work like /u5531e3aas01fe5c2, that it to pass a parameter to my PHP, but something is wrong with my code.
RewriteEngine On
RewriteBase /try/
RewriteRule /u[a-z0-9]{17} /index.php?url_id=$1 [L,QSA]
P.S.
And also is it possible to rewrite URL that if appears index.php?url_id=u5531e3aas01fe5c2, it would be rewritten to /u5531e3aas01fe5c2
Try this:
RewriteRule ^u([a-z0-9]{16}) /index.php?url_id=$1 [L,QSA]

Rewrite Rules in Htaccess

New to htaccess: how can i write rewrite Rules for this,
instead of http://www.x.com/directory.php?state=TX have www.x.com/texas-fishing
instead of http://www.x.com/directory.php?state=WA have www.x.com/washington-fishing
Actually $1 indicates the dynamic part of the Url like(TX,WA..), but here i need complete title of the page. So do i need to modify in script or .htaccess is enough to manage.
If it is enough then how can we manage..
RewriteRule ^texas-fishing$ directory.php?state=TX [NC,L]
RewriteRule ^washington-fishing$ directory.php?state=WA [NC,L]

Categories