htaccess rewriting issue - php

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.

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]

How to make query string look better in the URL with PHP?

Now, this is how my URL looks:
onlineproject/search-category-products.php?cat_id=3
I want to show somewhat like this:
onlineproject/search-category-products.php/vegetables
Is there any way to achieve it?
Add or modify the ".htaccess" in your server so it looks like
Rewrite On
RewriteRule onlineproject/search-category-products.php/^(.*)$ onlineproject/search-category-products.php/?cat_id=$1 [L]
That way your url would look like
onlineproject/search-category-products.php/vegetables
But the request would be redirected to
onlineproject/search-category-products.php/?cat_id=vegetables
This way it looks prettier, but the mapping with the categories and their ids must be handled by the server.

Change a url with parameters using htaccess

Like on the title, I'm looking for a way to change a url with parameters (from this www.animevid.net/player/?anime=d/dmc to this www.animevid.net/player/anime/d/dmc) using the .htaccess file.
I've found many similar post but I've only got errors, loop redirect, or...nothing. The nearest thing i've got is this code :
RewriteRule ^player/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+) player/index.html?anime=$1&t=$2 [NC,L]
Also please note the "d/dmc" on "?anime=d/dmc", is a variable, another example is "c/codegeass", "s/sao" etc...
As mentioned above the final, rewritten url you give as an example is invalid. You should escape the last /:
https://www.animevid.net/player/anime/d%2Fdmc
and rewrite to:
https://www.animevid.net/player/?anime=d%2Fdmc
Then you have to change your regex strategy for this to work. Try something like that:
RewriteEngine On
RewriteRule ^player/([^/]+)/([^/]+) player/index.html?anime=$1&t=$2 [NC,L]

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 .

htaccess URL rewrite

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

Categories