how to set link /profile/username in .htaccess - php

i'm working on a script and I want to make the links such as www.mysite.com/signup. this link for registration. www.mysite.com/user/username and this link for user's profile. the links without any php extension. also when someone request link such as www.mysite.com/signup.php should redirect him to www.mysite.com/signup
.my .htaccess
RewriteEngine On
RewriteRule ^/?([a-z]+)$ $1.php [L]
with this, all links are working fine, but when I access the user's profile such as
www.mystie.com/profile/username
I get this error
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
my question: what code to add to let that link works fine?
thank you

Your routing requests blindly to php files, this doesn't have anything to do with your issue:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?([a-z]+)$ $1.php [L]
Now you need to add a rule to route profiles, since you don't have a rule for it, your profile links aren't going to work. So something that looks like:
RewriteRule ^/?profile/(.*)$ /profile.php?username=$1 [L]
Replacing profile.php with whatever script generates profiles and the username= with whatever _GET param the script looks for for the username.

Ok I solved the problem
big thanks to all who tried to help me and especially Jon Lin :)
this is the code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/(.*)$ profile.php?username=$1 [L]
my profile.php
$username = $_GET['username'];
$info = fetchUserInfo($username);
echo $info['username'];
echo $info['userid'];
hope also help some people having same problem

You want to read in the manual for mod_rewrite about the two directives 'RewriteLog' and 'RewriteLogLevel'. Using those you can learn to understand how the rewriting engine processes the requests by seeing all steps in a log file.

Related

PHP identifying the URL and displaying page

Currently, a user's timeline can be accessed on my site using localhost/dateapp/timeline?profile=user1571502747. Here user can later manually change their username from user1571502747 to anything they like for easy access. Now what I want is if someone opens localhost/dateapp/user1571502747 (or mysite.com/user1571502747 when live), it automatically redirects and opens the timeline of user user1571502747 or say it redirects to localhost/dateapp/timeline?profile=user1571502747. But I am not being able to figure out a way to do so. Currently, when trying to access that displays no page with page not found browser error. Of course I have haven't put anything for it yet so its natural to get this error. Can anybody guide me on doing this? Facebook does this, for reference. Thanks in advance.
UPDATE: If anything needs to be done in .htaccess file then my current .htaccess has the following.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You can use .htaccess for this.
Try adding this to your .htaccess file in your root directory
RewriteEngine On
RewriteRule ^([^/]*)$ /timeline?profile=$1 [L]

.htaccess redirect to another site

hello all i am building a url shortener site where we create smart short urls for any long url the short url looks like this reurl.in/anji89jd. whenever anyone clicks on this short url the user is directed to corrosponding long url which is stored in database and it is extracted by sql query on the index page everything is working good but i dont know how to direct the user to the long urls by htaccess
look if the short link is reurl.in/abcd then the index page is designed like this
reurl.in/index.php?r=abcd and then it directs the page to new site if i type this url everyhing works well
but reurl.in/abcd dosent works
please help me i know this can be done by .htaccess
i have tried like
RewriteEngine on
RewriteRule ^index/([0-9]+)/?$ index.php?r=$1 [L,QSA]
Try:
Options -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?r=$1 [L,QSA]

URL rewrite more than one php file

I got an problem with url rewriteing.
First of all i am starting to think that i am making mistake in the structure of my webpage.
I made more than one php file. I think that this is mistake because in all tutorials and samples urls they are using in .htaccess only index.php.
Info
1. I got in navigation tab index.php section1.php section2.php section3.php section4.php
2. In index.php i only have an basic information that won't change much.
3. In each section.php file i am echoing out list of articles.
4. each aricle got id and i am using it to echo out information when user clicks on article link.
5. link to article "http://www.example.com/section1.php?id=607575"
6. In each section.php file i got more than one page.
7. For now my links to section page looks like this "http://www.example.com/section1.php?page=1".
Questions
Qestions
Should i better echo out all information on index.php page by getting information from url?
If now my link looks like this <a href="http://www.example.com/section1.php?page=1>section1</a> do i need to manually rewrite them OR it dose not matter, .htaccess will do the job?
if i need to rewrite url will it looks like this? section1
How to rewrite urls so they look like this www.example.com/section1/page/1 and www.example.com/section1/article/607575 (i know it's bad to use only numbers, i just need to understand how it works,then i will figure it out,how to replace it with article name).
If I understand correctly, this is how you would rewrite:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ /path/to/section1.php?page=$ [QSA,L]
I'm not very used to mod_rewrite but that should work. You might have to rewrite rule for ?article too.
If this was not answered correctly, please refer to http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
From: http://www.example.com/section1/page/1
To: http://www.example.com/section1.php?page=1
RewriteRule ^section(\d+)/page/(\d+)$ /path/to/section$1.php?page=$2
From: http://www.example.com/section1/article/607575
To: http://www.example.com/section1.php?id=607575
RewriteRule ^section(\d+)/article/(\d+)$ /path/to/section$1.php?id=$2

How to create a directory with a single file in it?

I'll be signing businesses up to advertise on my website, and I want them to have a direct URL for their customers to go to.
Like, instead of www.website.com/page.php?id=324234234,
I want to have www.website.com/businessname
Is there a simple way to do this? I've searched and seen a whole bunch of different things people are trying to do but I haven't seen anything that's the same as what I want to do.
I'm using a VPS, and I want to make sure that I don't open up permissions so that anyone can get in there and mess things up.
Also, these users will not be signing themselves up. I will be doing that.
The simplest way to get my end result is what I'm looking for. Thanks!
Basic URL rewriting could work.
Add to your .htaccess file
RewriteEngine on
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
Then use PHP to rewrite the businessname to the ID of the company / find the data.
Of course .htaccess rewrite rules is a complete science if you need more complex rewriting...
Re-iterating what jtheman said with a little more explanation:
Create a file named .htaccess with the contents:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
You need, of course, the ability to have directory level .htaccess enabled - you're using a VPS so you should be able to do this if it is not already enabled.
So let me explain what each line will do.
RewriteEngine on
Turns on the ability to URL re-write
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Tells Apache not to re-direct files that exist in the directory already
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
This is where the magic happens.
^(.*)$ this part is like a regular expression match. It will tell Apache to collect any URLs that have any characters within them and redirect them to page.php?businessname=(.*)
So, if you post:
www.website.com/stackover
It will really be sending: www.website.com/page.php?businessname=stackover
Then you can just use $_GET[businessname] to dynamically update the page.
Hope this helps!

URL Routing method for core php

I have web site in core php and I want to make my SEF URL.
In my web site for transferring control from one page to other i have used something like
header("Location: edit.php?id=12");
I read one article but couldn't get it how to implement it.
Link of article
So how i can make url to read like "mysite.com/usr/edit"
I read lot on google i got the idea that i need to do something in .htaccess but i dont know what needs to do with url in php so i dont have to change major code in site.
Thanks in advance.
You can do it with the .htaccess code given below:
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
after creating .htaccess file with above code. You will have all the stuff after the file name ex. "/usr/edit" in the GET parameter $GET['url']. After that you can do anything.
Here is the working example of my MVC framework: https://bitbucket.org/ManthanB/my-framework/
You can contact me for further information.

Categories