How to properly rewrite a friendly URL? - php

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]

Related

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]

Rewriting Url With .htaccess for Muliple Parameters

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]

Mod rewrite format for blog URL with post and name in title

understand how to make e.g. blog.php rewrite to just blog.
But I can't understand what I need to do when rewriting a dynamic URL.
The URL normally looks like so:
<a href='newspost.php?id=$postID&$blogtitleURL'></a>
This works fine as it should, but i want it to look more friendly.
E.g. newspost/5/Blog_title.
Is this possible? Also how if it is, do I go about making a variable using $_GET?
I normally use this for the rewrite:
RewriteRule ^apage apage.php [L]
What should I be doing for the URL provided?
Thanks!
This should work for you:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /newspost\.php\?id=(.*)&title=(.*)\ HTTP
RewriteRule ^ /newspost/%2/%3\? [R=302,L]
RewriteRule ^newspost/(.*)/(.*)$ /newspost.php?id=$1&title=$2 [L]

Add $_GET-vars to a rewritten URL

I have several URLs which are dealt with by rewrite rules. Basically I want to add some $_GET-vars to a rewritten URL.
Say I have:
http://www.domain.com/search/the-netherlands/overijssel/
I would like to add some extra data to the URL like:
http://www.domain.com/search/the-netherlands/overijssel/?custom_var=1
My RewriteRule looks like this:
RewriteEngine On
RewriteRule ^search/(.*)/(.*)/$ /index.php?page=Search&Country=$1&Region=$2 [L]
When I var_dump the contents of $_GET I don't see custom_var anywhere. Can this be done, using the existing rewrite rule?
Since your query already has some parameters, append [QSA] to the end of the RewriteRule.
RewriteRule ^search/(.*)/(.*)/$ /index.php?page=Search&Country=$1&Region=$2 [QSA,L]

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]

Categories