PHP .htaccess mod_rewrite - php

So I want to use mod_rewrite to transform:
random_name.com/main.php?user=$user
into
random_name.com/$user
(the $user is a php variable added onto the url, so it can be any name such as andy or rebecca, names like that)
and my code for the .htaccess is:
RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/?$ main.php?id=$1 [NC,L]
But this doesn't seem to work for some reason. I've read up on the tutorials but they're really complicated and it seems like this would do the trick, but it doesn't. I'll appreciate it if anyone who has experience with mod_rewrite would give me a few pointers.

You cannot use mod rewrite to transform random_name.com/main.php?user=$user into random_name.com/$user.
You have to do it manually in all the links on your site.
After that you may use mod rewrite for the reverse transformation, which will make /main.php?user=$user out of /user request

Try this:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)$ main.php?user=$1 [NC,L]

Related

Rewrite Rule for htcaccess

I think this has been discussed over here in past but i don't know exactly what i have to search for so please if you could help just give me a hand.
Well i want to create a url rewrite with multiple options. I cannot explain here with few so i would rather give a simple example.
A url:
example.com/info.php?id=1
example.com/info.php?id=1&edit
I want to rewrite to like this
example.com/info/1
example.com/info/1/edit
I think you want the opposite: example.com/info/1 -> example.com/info.php?id=1
RewriteEngine on
RewriteRule ^info/([^/]+)/edit/?$ info.php?id=$1&edit [L]
RewriteRule ^info/([^/]+)/?$ info.php?id=$1 [L]

Multiple vanity URLs for different php files?

I am trying to build a site which has the following pages which needs vanity urls,
account.php
questions.php
profile.php
I need to display
www.mysite.com/account?req=settings
www.mysite.com/account?req=questions
www.mysite.com/account?req=answers
as following
www.mysite.com/account/settings
www.mysite.com/account/questions
www.mysite.com/account/answers
And for the other files I need the following,
etc:-
www.mysite.com/questions?id=0484684
As
www.mysite.com/questions/0484684
And
www.mysite.com/profile?id=123456
as
www.mysite.com/profile/123456
Would anyone help me because I really need to make this possible.
I would like to remove the .php extention from the url too.
I like to say that I have no experience in htaccess files.
Thanks for the help, it worked !!!
In your .htaccess file, add these rules:
RewriteEngine on
RewriteBase /
RewriteRule ^account/([^/\.]+)/?$ account.php?req=$1 [L,NC,QSA]
RewriteRule ^questions/([^/\.]+)/?$ questions.php?id=$1 [L,NC,QSA]
RewriteRule ^profile/([^/\.]+)/?$ profile.php?id=$1 [L,NC,QSA]
You should be able to add new rules based on how these work if you need them in the future.
You'll want to use a .htaccess file combined with PHP for complex manipulations like that, I wrote an article on re-writing URLs after asking a similar question on SO:
http://tomsbigbox.com/elegant-url-rewriting/

.htaccess rewriterule and fake directories

I am trying to do some URL rewriting with Apache and PHP. This is for SEO reasons so that foo.com/product-category/product-title becomes foo.com/product.php?id=999. I would of course want the URL not to change and be the SEO friendly version.
I have a current site which has a working setup but it's very much a hack rather than a maintainable solution, so I am trying to do things right!
At the moment I have:-
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) /url_redirect.php?page=$1 [QSA,L]
Which works perfectly, http://foo.com/random-url?foo=1&bar=2 ends up at /url_redirect.php with $_GET['page'] = 'random-url' and $_GET['foo'] = 1 etc etc..
I want the exactly the same thing to happen if you one level deeper
(eg http://foo.com/non-existing-directory/random-url?foo=1&bar=2)
I could make the non-existing-directoy a real directory and then .htaccess from there but there has to be a easer way.. I would prefer me not to have to hardcode any directory names (which is sort of how my current solution works).
I think the issue is probably something not right with the regular expression, they always defeat me.
I just tried this, might not be the best way to achieve it though (I'm no rewrite guru)
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/([a-zA-Z0-9_\ \-\%]+)?($) url_redirect.php?page=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) url_redirect.php?page=$1 [QSA,L]
Sorry that was for my local setup here's how I think you would want it!
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/([a-zA-Z0-9_\ \-\%]+)?($) /url_redirect.php?page=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) /url_redirect.php?page=$1 [QSA,L]
Notice the / before url_redirect.php

PHP Mod_rewrite

i'm new to mod_rewrite, and i'm trying to convert my web address from:
website.com/profile.php?user=andy
to the following:
website.com/user/andy
This is my following code:
RewriteEngine On
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?user=$1 [NC,L]
I researched extensively and this does seem to be the correct way to do it, but it doesn't redirect to where i want it to, it just redirects to this:
http://website.com/profile.php?user=andy
which means i must doing something wrong...
Can anyone help me out here? I would appreciate any tips.
If you want
http://website.com/profile.php?user=andy ->301-> http://website.com/user/andy
http://website.com/user/andy means http://website.com/profile.php?user=andy
They are 2 different things, you'll need 2 rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9]+)
RewriteRule ^profile.php /user/%1? [R=301,L]
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?a=b&user=$1 [L]
The first will 301 (moved permanently) redirect to the pretty url.
The second will allow your application to understand the pretty url.
Whenever you change the url scheme for a site you should take care of existing links. As such, that first rule is required/a good idea. You should not, however, need the first rule when using your own application. If your own application is generating links to profile.php?user=me - change your application code.
You have to change your URLs when outputting them in your HTML to be in the format you want (/user/andy).
mod_rewrite will rewrite /user/andy to main.php?... not the other way around.
What do you mean by my result?
mod_rewrite won't change existing links in your source code. Navigate to website.com/user/andy and you should see it work.

Custom URL with PHP

I have a small question to ask. Is it possible, via php or htaccess, to change a url like: miodominio.com/users.php?idu=x into something like miodominio.com/username ?
I want it to be Facebook style...Where "username" is the username chosen by idu = x.
There are multiple ways to solve this problem, but here's one that always suits my needs.
Guide all your URL requests through the index.php first and resolve the request in your PHP code second.
1) Use an .htaccess file to direct all URL's through index.php. You'll find one way here by the CodeIgniter framework and a more advanced explanation here. I recommend the CodeIgniter .htaccess guide first if you're inexperienced with .htaccess.
2) Second, use the $_SERVER variable in PHP to extract the URL. Probably with the help of the $_SERVER['REQUEST_URI'], you'll find '/username/' which you can then use to extract the user's data and serve it to them.
Good luck and beware of URL injections using this method.
You need to use apache's mod_rewrite for this. It can translate miodominio.com/username to miodominio.com/users.php?idu=x. There are some good guides about this which are easy to find with Google.
You can try to use this mod_rewrite pattern (add it to the .htaccess):
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?idu=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?idu=$1
you have to write a clean URL in your .htaccess file like :
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ users.php?idu=$1
Put the following in your .htaccess
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)/?$ /users.php?idu=$1 [NC]
The [NC] will make it case-insensitive, if you accept only lowercase username, remove the [NC] from the last.

Categories