Rewrite URL for Profile Page - php

How do I use .htaccess to rewrite:
mysite.com/profile/[username]
go to:
mysite.com/profile.php?id=[username]
I don't want to have to reload the page just change the URL to make it look good.
Any help is appreciated.

This is the htaccess rule that should get you started:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule profile/(.+) profile.php?id=$1

Related

Simple htaccess rule for rewriting the URL

Im trying to achieve this
http://domain.com?q=page-slug to http://domain.com/page-slug
right now if I access this http://domain.com?q=page-slug my webpage is working, but I want clean URls like http://domain.com/page-slug
I used this code in my htaccess but returning 500 error
RewriteEngine On
RewriteRule ^(.*)$ ?q=$1 [L,QSA]
any help please.
Try this I am assuming you have index.php page which is handling the request.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ index.php?q=$1 [QSA,L]
If not index.php change to actual page you have.

Rewrite url parameter to folder with htaccess

i would like to rewrite all my website's urls from this
https://website.com/page.php?lang=2 to this
https://website.com/en/page
(creating /en/ directory while hiding .php extension)
Can anyone help me with this one?
Thank you in advance
Try it like this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/([\w-]+)$ $1.php?lang=2 [QSA,L]

how can write htaccess file to display user friendly url?

I developed one website. I want to display url for my website as below.
http://example.com/1/post
i am passing the query string as below
<a href="page.php?id=1&url=post>click me</a>
I wrote a below htaccess file to display above user friendly url
RewriteEngine on
RewriteCond %{THE_REQUEST} /page\.php\?id=([0-9]*)&url=(.+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([0-9]*)/(.+)$ /page\.php\?id=$1&url=$2 [L]
But, when clicking Click me link, it load some time and display cannot reach message in browser. I dont no what is wrong in my htaccess code. Kindly help me to display url as i mentioned above. Thanks in advance.
Try this out
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/([^/]+)/?$ page.php?id=$1&url=$2 [L,QSA]

Wrong redirection with .htaccess?

I made an URL Shortener and shortened links looks like this:
http://patison.eu/06uoM. My PHP code checks if this 06uoM is in my database - if yes it will redirect to the correct page, if not it will open index.php file from index_html directory. It's bad because when I write URL to image uploaded on server it will open index.php because URL is not in database...
http://patison.eu/forum/images/off.gif - see?
This is how .htaccess looks like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1
Please help me solve this problem!
It sounds like you want to let 404 errors happen on URLs like
http://patison.eu/forum/images/off.gif
but not on URLs like
http://patison.eu/06uoM
If that is the case, you can change your .htaccess code to this to limit the type of URLs rewritten:
RewriteEngine on
# Limit rewrites to urls like '/06uoM' or '/06uoM/', but not like '/forum/images/off.gif'
RewriteCond %{REQUEST_URI} ^/[0-9a-z]{5}/?$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1

php friendy url using htaccess

I have the following code on my htaccess:
RewriteEngine On
RewriteRule ^course-details/([a-zA-Z0-9]+)/$ course-details.php?id=$1
I think this gave me a URL like this: /cp/course-details/5 where 5 is the id of the course.
But when I go to this address I get 404 not found. Also, our current url is
/cp/course-details.php?course-details.php?name=bla-bla-bla&category_id=1
and we need a friendly url like this
cp/category-name/course-name/
Thanks in advance for you help.
You can try this in your htaccess file if the php file is in the cp dir. Since it appears your are developing in a subfolder(peppers) of your dev box you can try this.
RewriteEngine On
RewriteBase /peppers/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cp/course-details/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
If this is for production then you can use this in the .htaccess file in the root.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cp/course-details/([^/]+)/?$ /cp/course-details.php?name=$1 [NC,L]
RewriteRule ^course-details/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/.*$ course-details.php?id=$1&category_id=$2
With that, you can use URL like course-details/1/5/category-name/course-name
Because URL allow: course-details/category_id/id/what-ever-you-want

Categories