URL rewriting and get parameters - php

I am currently building a website where the content of pages come from a database.
To construct my SQL statement, I use the parameters in the URL and until now everything worked fine.
Now I would like to rewrite the URLs to have clean URL, so by default my URLs look like:
lunettes-collection.php?supplier=all&type=vue
I wrote a rule to convert this URL like
lunettes-collection-vue.html
But now when I reach this URL I obviously cannot get the parameter type=vue.
What is the best practice to handle such situation?

With .htaccess you can do that
# Turn Rewrite Engine On
RewriteEngine on
#Rewrite lunettes-collection.php?supplier=all&type=vue to
#lunettes-collection/all/vue
RewriteRule ^lunettes-collection/([a-zA-Z]+)/([a-zA-Z]+) lunettes-collection.php?supplier=$1&type=$2 [NC,L]
Remember that URL will only support letter from a-z and A-Z if you want enable digits add an extra part 0-9

Rewrite would not be Such an outcome.
The $_GET will get normal variable.
Please show your Rewrite rules.
I try the Nginx rules is work normal like this.
rewrite ^/lunettes-collection-vue.html$ /lunettes-collection.php?supplier=all&type=vue last;

Related

url search for file in subdirectory instead from public_html using htaccess

I am working on a website in which I write code for htaccess but the thing which I wanted to do is not happening. I have url which is:
http://www.example.com/demo.php?id=234&title=ask%20me%20a%20question
I converted to below url using htaccess:
http://www.example.com/234/ask%20me%a%question
htaccess code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([0-9]{4})/([a-z]+)/$ demo.php?url=$1&url2=$2
So. the problem is converted url is search for related file in subdirectory instead of server root i.e; public_html. I want to know how could this problem will solve.
Plz help me. Thanks.
The second parameter in your request requires that characters other than a-z be included, but you are limiting it to a-z.
In addition, you are requesting 234 in the URI, but checking for 4 numbers in the first parameter.
As such, change your rule to the following:
RewriteRule ^([0-9]{3,4})/([^/]+)/?$ demo.php?url=$1&url2=$2 [L]
Changes
Allow 3 or 4 numbers in the first parameter. If you want to be more flexible, you can change it to ([0-9]+).
Check for all characters other than / in the second parameter.
Make the trailing slash optional using /?.
Add the L flag to stop rewriting if the rule is matched (always good to have for when you add other rules).

url rewrite for lighttpd - regex

I would like to convert my urls to short ones without get parameters but for the first try I would reduce the regex to only one case:
There are many normal urls that should not be affected. The only case I want to rewrite the urls is the user profile page.
Currently they look like this:
www.dummy.com/index.php?user=USERNAME&id=USERID
Target scheme looks like this:
www.dummy.com/USERNAME/USERID
The usernames can contain mostly any character (without /) not just a-z/A-Z.
I was never really good at regexing and have no clue how to handle this problem. Any suggestions are welcome.
There won't be requests to a folder (www.dummy.com/folder/subfolder) but confirming that the userid is an integer value would be nice but is not necessary.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^(\w+)/(\d+)/?$ index.php?user=$1&id=$2 [L,QSA]
This will match either one of these URLs:
/abcd/123
/abcd/123/
/abcd456/6789
/abcd456/6789/
EDIT:
In LigthHttpd use this equivalent rule:
url.rewrite = ( "^/(\w+)/(\d+)/?(?:\?(.*))?$" => "/index.php?user=$1&id=$2&$3" )

How do I change ugly URLs to pretty URLs using .htaccess? [duplicate]

I need to grab some of my website's old URLs and do a 301 redirect to the new ones, since they are already indexed and we don't want to loose relevance after the change. The old URL is in fact very ugly and for some reason everything I try to do to rewrite it does not work. Here it is:
http://www.mywebsite.com/ExibeCurso.asp?Comando=TreinamentoGeral&codCurso=136&Titulo=Como%20Estruturar%20um%20Sistema%20Gerencial%20de%20Controles%20Organizacionais,13
Basically, I need to translate it into something like:
http://www.mywebsite.com/curso/136
From the old URL I need to check if the user typed "ExibeCurso.asp"; then I know I must send him here: /curso. I must also grab the integer that was in the querystring parameter "codCurso" (136). What is the regular expression I must use for this. I am using ISAPI_Rewrite 3, which basically implements htaccess on IIS, so there should be no difference in terms of syntax. Thanks.
Try this rule:
RewriteCond %{QUERY_STRING} ^([^&]*&)*codCurso=([0-9]+)(&.*)?$
RewriteRule ^/ExibeCurso\.asp$ /curso/%2? [L,R=301]
But I’m not sure whether ISAPI Rewrite requires the pattern to begin with a slash.
Off the top of my head, something like this should work:
RewriteRule ^ExibeCurso.asp(.*)$ http://www.mywebsite.com/curso/$1 [L,R=301]
That would at least send the traffic to /curso/ with all parameters attached. Maybe it's best to process it from there.

Apache Url Rewrite Query String SEO

I have the following rewrite.
RewriteRule ^/news/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /pagebase.php?pbid=3656&nid=$1&title=$2 [QSA,L,I]
http://www.domain.com/news/1/new-event/
So that the above url will be rewritten as:
http://www.domain.com/pagebase.php?pbid=3656&nid=1&title=new-event
This works perfectly. However I want people to be able to type in:
http://www.domain.com/news without any query strings and have it rewrite as this:
http://www.domain.com/pagebase.php?pbid=3656&nid=&title=
However the match fails so I get a 404 error.
Is there anyway I can rewrite my rule to make the last 2 query string options optional. I was able to figure it out using multiple rewrite rules and placing them in the correct order, but I'd like to get it so I can get it working with one rule.
Use two rewrite rules:
RewriteRule ^/news/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /pagebase.php?pbid=3656&nid=$1&title=$2 [QSA,L,I]
RewriteRule ^/news$ /pagebase.php?pbid=3656&nid=&title= [QSA,L,I]

Rewrite URL in PHP

I would like to rewrite the following URL
www.mysite.com/mypage.php?userid=ca49b6ff-9e90-446e-8a92-38804f3405e7&roleid=037a0e55-d10e-4302-951e-a7864f5e563e
to
www.mysite.com/mypage/userid/ca49b6ff-9e90-446e-8a92-38804f3405e7/roleid/037a0e55-d10e-4302-951e-a7864f5e563e
The problem here is that the php file can be anything. Do i have to specify rules for each page on the .htaccess file?
how can i do this using the rewrite engine in php?
To get the rewrite rule to work, you have to add this to your apache configs (in the virtualhost block):
RewriteEngine On
RewriteRule ^([^/]*)/userid/([^/]*)/roleid/(.*)$ /$1.php?userid=$2&roleid=$3 [L,NS]
RewriteRule basically accepts two arguments. The first one is a regex describing what it should match. Here it is looking for the user requesting a url like /<mypage>/<pid>/roleid/<rid>. The second argument is where it should actually go on your server to do the request (in this case, it is your php file that is doing the request). It refers back to the groups in the regex using $1, $2, and $3.
RewriteEngine on
RewriteBase /
RewriteRule ^mypage\/userid\/(([a-z0-9]).+)\/roleid\/(([a-z0-9]).+)$ www.mysite.com/mypage.php?userid=$1&roleid=$2
No you don't need a separate rule for every php file, you can make the filename variable in your regex something like this:
RewriteRule ^(a-z0-9)/userid/([a-z0-9].+)/roleid/([a-z0-9].+)$ $1.php?userid=$2&roleid=$3
If you want to rewrite the latter URL that is entered in the browser TO the first format, you would want to use a .htaccess file.
However, if you want to produce the pretty URLs in PHP (e.g. for use in link tags), then you have two options.
First, you could simply build the URL directly (instead of converting) which in my opinion is preferred.
Second, you could rewrite the first (ugly) URL to the pretty latter URL. You would then need to use preg_replace() in PHP. See http://php.net/manual/en/function.preg-replace.php for more info. Basically, you would want to use something like
$rewrittenurl = preg_replace("#mysite\.com\/mypage.php?userid=(([a-z0-9\-]).+)\&roleid=(([a-z0-9\-]).+)$", "mysite.com/userid/$1/roleid/$2", $firsturl);
Good luck!

Categories