htaccess URL rewrite - php

Having a bit of trouble with htaccess, at the moment I have a rewrite rule that replaces the query string into a folder structure
RewriteRule profile/id/(.*) u_profile.php?id=$1
This spits out /profile/id/1/ which is perfect, although, I'd really like to add another variable in there to also get the users name too.
At the moment I have a $_SESSION['username']; set for everyone who's logged in, I want to really pass it to the query string like so,
/profile/john/id/1/
/profile/sarah/id/1/
etc

I think this is what you're looking for:
RewriteRule profile/(.*)/id/(.*) u_profile.php?person=$1&id=$2
Then you can access the name through $_GET['person'] and compare it to $_SESSION.

To remove the id part, you just can remove /id
It would look like this:
RewriteRule profile/(.*)/(.*) u_profile.php?person=$1&id=$2

Related

.htaccess re-write use a get variable to replace the filename

This one is a bit tricky for me, I want to make this URL as minimal as possible. Ideally I would like to change this URL:
http://www.example.co.uk/profile/profile.asp?profile_id=1&top=1&abt=2&ft=3&school=Something%20School
to:
http://www.example.co.uk/something-school/
Using .htaccess file.
This means we would be using the school get variable to replace the .asp file name, getting rid of /profile/ as well as the other get variables.
Is this possible? If so how? If not could you potentially give me an alternative?
Any help would be greatly appreciated!
EDIT
A user Badhorsie has wrote a rewrite rule for me which does the exact conversion. Unfortunately the webpage fails to load as these get variables are unfortunately necessary for the page to load.
I am guessing it is not possible to hide the get variables. In which case would it be possible to retain the get variables but to keep them clean? Looks like the directory is also necessary?
Perhaps something like: www.example.co.uk/profile/something-school/1/1/2/3
We can get rid of the school get variable as it is not needed (only needed to replace the profile/profile.asp section.
Try this:
RewriteCond %{QUERY_STRING} (.+(?=&school))school=([\w%-]+)
RewriteRule ^profile/profile.asp /%2?%1 [L,NE,R=301]
Did you tried this RewriteRule. Try adding this code in your .htaccess file.
RewriteRule ^http://www.example.co.uk/profile/profile.asp?profile_id=1&top=1&abt=2&ft=3&school=Something%20School?$ http://www.example.co.uk/something-school/ [L]

php query without var=

I'd like the end user of my website to be able to do something along the lines of: example.com/user/user_name to show the statistics of that user on a different website. In short, I'd like to be able to make queries in php without having the user do: example.com/user/username=user_name I also have no way of knowing all of the possible users. I guess this means I'd like to always redirect to the index, while preserving the querystring that doesn't have "var=" in it. How would I do this?
you cna use the explode function in php. use it on the url slashes "/" and you get valid data to query. Remember to make it secure from sql injections.
EDIT
here you go:
$url = explode('/',$_SERVER['REQUEST_URI']);
echo 'user = '.$url[count($url)-1].'<br />
username = '.$url[count($url)-2];
EDIT 2
yeah you need to use .htaccess
unless you wanna get away with the questionmark
http://example.com?/user/username
EDIT 3
if you use apache you can make all traffic go through index.php with following .htaccess file:
RewriteEngine On
# always run through the indexfile
RewriteRule .$ index.php
Then if you want some folder to not go through to index.php, you add a htaccess file in that folder with:
RewriteEngine Off
The easiest way to do this is to still have the script be somepage.php?username=user_name and then have in your .httaccess file:
RewriteEngine On
RewriteRule ^/user/(.*) somepage.php?username=$1
This way you can still get the variable with $_GET['username'] but it looks nice to the user.

htaccess rewriting issue

Working on a website, and having some problems with .htaccess.
I do have a category link, which is like this:
sharing/index.php?cf=category&id=$1
I did a rewrite on it to:
sharing/cat-([^/]+)$
which works very well, then I added a pagination to the script, and pagination in the link is like this:
sharing/cat-1?next=1
I would like to make it like this:
sharing/cat-1/page/1
I have tried my best, but it didn't work out. Please note cat-1 is for the category ID meaning in another category it may be cat-3.
The error comes from the fact that you haven't escaped the forward-slash.
So you should try this for the RewriteRule
RewriteRule ^sharing\/cat-([^\/]+)\/page\/([0-9]*)$ sharing/index.php?cf=category&id=$1&next=$2
And the link for the pagination, should be like this:
sharing/cat-1/page/1
not the one you mention (sharing/cat-1?next=1)
Remember that if your categories are only numbers, it might be better to change ([^\/]+) by ([0-9]*)
Try the following example, where I'm passing two variables:
RewriteRule ^player/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ players/view.php?player=$1&page=$2 [NC,L]
For extending it, you just need to add ([A-Za-z0-9-]+)/ to the URL, and pass another parameter in the other side.

Htaccess global RewriteRule

I'm not sure how to explain this problem so the title is kind of vague!
Well here I go, I'm working on a picture/album page on my website and everything is working great. But I want to add a next/previous picture feature like any good picture website has. I want to passe the different options for sorting out the album by variables.
For now the url on a picture is http://localhost/photo/174/picture-name/, I would like to add on this some parameters so that the url then looks like http://localhost/photo/174/picture-name/album:5/sort:name/.
With the help of .htaccess I would like to extract the variables album and sort`.`But the little catch is that I would still want to be able to get to the page with only this urlhttp://localhost/photo/174/picture-name/``
For now my .htaccess file looks like this :
RewriteEngine on
RewriteRule ^photo/([A-Za-z0-9]+)/(.*)/$ photo.php?pic_id=$1
I tried adding this line in it but it did not work out.
RewriteRule ^(.*)/album:([A-Za-z0-9]+)/sort:([A-Za-z0-9]+)/$ &album=$2&sort=$3
I hope someone has an answer for me,
Have a good day
Joris
Did you mean something like this?
RewriteRule ^photo/([A-Za-z0-9]+)/[^/]+/album:([A-Za-z0-9]+)/sort:([A-Za-z0-9]+)/$ photo.php?pic_id=$1&album=$2&sort=$3
You also need to change your original rule:
RewriteRule ^photo/([A-Za-z0-9]+)/([^/]*)/?$ photo.php?pic_id=$1
Because what you have will match against the URI with the parameters
I guess you are talking about this, and your example wasn't correct:
RewriteRule ^photo/(.*)/.*/album:(.*)/sort:(.*)/$ photo.php?pic_id=$1&album=$2&sort=$3
(for simplicity, I replaced some of the expressions).
Here, http://mydomain.com/photo/174/picture-name/album:5/sort:name/ get rewritten to http://mydomain.com/photo.php?pic_id=174&album=5&sort=name .

Not sure how to get mod_rewrite to write to a certain format

I'm trying to understand how to properly use mod_rewrite. I have posts in my database that have a title and date. Right now I'm using mysite.com/post.php?id=3 to retrieve my post data and populate the page. I would like to have it like this: mysite.com/2011/03/27/my-title-like-so/. I'm guessing I'll have to query my database for something other than the id, but i dont know. Can anyone help? Perhaps I'm over looking something very simple. So far I have it showing up like this: mysite.com/post/2/ , but that doesn't help very much :P
Thanks!
A common practice is to include both the ID and the title/date in the URL. That way it looks nice to search engines and you can still retrieve it efficiently by ID. As an example, look at the URL for this stackoverflow question.
http://stackoverflow.com/questions/5451267/not-sure-how-to-get-mod-rewrite-to-write-to-a-certain-format
Just a guess but that number in the URL is probably a generated ID. So you use the ID and just ignore the rest of the URL, e.g.:
RewriteRule /questions/(\d+) question.php?id=$1
If you do not wish to include the ID in the URL, then I would suggest the following approach.
In your "posts" table, include a "url" (or "friendly" or something) field that contains the url you wish your post to be accessed by. When you insert the post, simply strip all non-word characters and replace them with a '-'.
Then, when the URL comes into your system, you can just map this url directly to the "url" field.
So, using your example, your URL would be http://mysite.com/2011/03/27/my-title-like-so/
The "url" field would contain 'my-title-like-so'.
You could use a simple rewrite rule such as the following:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f # if requesting file that doesn't exist, use the rule below
RewriteCond %{REQUEST_FILENAME} !-d # if requesting a directory that doesn't exist, use the rule below
RewriteRule ^(.*)$ index.php?myvar=$1 [L,QSA]
This would put the contents of the URL after the domain:
http://mysite.com/[catches everything here]
And would store it in the $_GET global as 'myvar'
Then you can do any processing on that url in php:
$url_string = my_sanitising_code($_GET['myvar']);
$url_array = explode('/', $url_string);
Now you have an array of all the parts of the URL and you can do whatever you wish with them.
You must capture the values that are passed to the engine to extract the data you want. So:
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ fetch.php?year=$1&month=$2&day=$3&title=$4 [L]
should match your format.
Then the http://mysite.com/fetch.php can use the $_GETs values to fetch the database and display the good things.

Categories