Rewriting Url With .htaccess for Muliple Parameters - php

I Want to rewrite the url using .htaccess
Plz Read the code, and you well Get to know What I mean
My URL :
article.php?id=1&title=example
Using this in .htaccess
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$
article.php?id=$1&title=$2 [NC,L]
I get
article/1/example
What i need is
article/example

So something like this:
RewriteRule ^article/([0-9a-zA-Z_-]+)$ article.php?title=$1 [NC,L]

Related

URL Rewriting with php htaccess?

I have a URL that looks like:
url.com/pages/login.php url.com/pages/NewClient/index.php
url.com/pages/NewClient/page1.php
url.com/pages/NewClient/page2.php etc.
How would I go about converting that URL to:
url.com/login/ url.com/panel/
url.com/page1/
url.com/page2
How do I go about making friendly URLs in PHP?
For handling it in PHP, you can add the below line in the .htaccess file
FallbackResource route.php
This way all the URLs will get landed to route.php first.
This file will have all the logic for routing and mapping.
You can have the logic to look for the $_SERVER['REQUEST_URI'] and render content accordingly using basic condition statements.
For more information on FallbackResource directive, refer doc
Or for something as plain as your requirement, you can simply handle this directly in the .htaccess file by adding the below rules.
RewriteEngine On
RewriteRule ^login$ pages/login.php [NC,L]
RewriteRule ^panel$ pages/NewClient/index.php [NC,L]
RewriteRule ^page1$ pages/NewClient/page1.php [NC,L]
RewriteRule ^page2$ pages/NewClient/page2.php [NC,L]

How to rewrite specific URL using htaccess

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]

URL Rewriting friendly URL with htaccess

I'm interested in rewriting a url which is already "friendly" in order to clean up what is already in the path.
At the moment, I have a URI which points to http://example.com/page/index/home, but ideally id like to have the remove page/index completely, and just have http://example.com/home.
Something like the following...
RewriteEngine On
RewriteRule ^page/index/home$ /home [L]
If you know the specific page you are matching
To achieve what you want, you can just do this:
RewriteEngine On
RewriteRule ^/home$ /index.php [QSA,L]
QSA stands for Query String Append. It will append the query string (your GET parameters) to the end of your URL automatically if there is one.
You can use this rule in .htaccess file:
RewriteEngine On
RewriteRule ^home$ page/index/home [NC]

Rewrite URL using htaccess php

I want to rewrite the url in my project.
For example:
http://www.example.com/dashboard/test/ to http://dashboard.example.com/index.php
Also I want to do it for:
http://www.example.com/dashboard/test2/ to http://dashboard.example.com/index.php
Can anyone tell me the idea to rewrite the url?
First you create a .htaccess file in your root.
Than, just put the redirect commands in it.
How to compose a .htaccess and how to create rewrite rules is explained here: http://net.tutsplus.com/tutorials/other/the-ultimate-guide-to-htaccess-files/
You will need something like:
RewriteRule ^(.*)$ http://dashboard.example.com/$1 [L,QSA]
try this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^test.*$ http://dashboard.example.com/index.php [R=301,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]

Categories