Htaccess global RewriteRule - php

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 .

Related

Problem with .htaccess - RewriteRule is appending new URL to old URL

I'm very new to htaccess and mod_rewrite things, so please forgive me the possibly stupid question :-)
I have a Links like that on my menu
teams/senioren/herren-1
teams/senioren/damen-1
etc.
and I want to redirect it to
team.php?team=herren-1
team.php?team=damen-1
My htaccess looks like that:
RewriteEngine On
RewriteRule ^teams/([^/]*)/([^/]*)$ team.php?team=$2 [L,QSA]
That works on localhost when I click the link one time. The URL then gets https://localhost/teams/senioren/herren-1. But when I click a second time I get https://localhost/teams/senioren/teams/senioren/herren-1. So it looks like it appends the new URL to the old one.
Can anyone help me?
Thank you
Solution: Use absoluth Path like arkascha mentioned in the comments.
So in my case use /teams/senioren/herren-1 instead of teams/senioren/herren-1

Changing link apperance with .htaccess

I'm trying to do simple trick with .htaccess file, but with without success.
Generally I have PHP script that makes dynamically generated signatures and link looks like this:
example.com/signature/generate.php?name=%SomeUserName%
where %SomeUserName% is simply username e.g. Patison
and I'm trying to get:
example.com/signature/%SomeUserName% or (if necessary)
example.com/signature/generate/%SomeUserName%
Last code that I'm tried with no success:
RewriteEngine On
RewriteRule ^/signature/generate.php?name=(*) /signature/$1 [L,NC]
.htaccess is hard to understand for me.
So I have one more question. When someone use this link on another site he will render an image or it will work only on mine?
This is the sort of approach you need:
RewriteEngine on
RewriteRule ^signature/([^/\.]+)?$ signature/generate.php?name=$1 [L,NC,QSA]
So anybody going to signature/levi will see signature/generate.php?name=levi

How do i convert my URL's like www.example.com/profile.php?id=xyz into www.example.com/xyz?

I have got different pages in my website(portal) which may take one or more parameters from the URL.
So the URL www.example.com/xyz has to be solved into or like www.example.com/profile.php?id=xyz and the value id should be able to get using GET method.
I'm working on PHP. Any answers would be highly appreciated. (I'm new to PHP and have only basic+ knowledge in it. Please explain briefly.)
To go from: www.example.com/profile.php?id=xyz into www.example.com/xyz you can use the following rule in your .htaccess file:
RewriteEngine On
RewriteRule ^([^/]*)$ /profile.php?id=$1 [L]
Just make sure you clear your cache before testing this.

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.

How do I strip part of a url?

How do I strip a part of the url? I do not know much about htaccess or apache.
I would like to strip www.mysite.com/page=services to www.mysite.com/services for example.
What exactly do I need to put in the .htaccess file in order to achieve this, and would that work for other pages as well?
Thanks.
I tried this for one of my sites lately and came up with this and for me it works fine,
this goes into the .htaccess file:
RewriteEngine On
RewriteRule ^([A-Za-z]+)/?$ index.php?page=$1 [NC,L]
then if you would write yoursite.com/pagename it would send yoursite.com/index.php?page=pagename to your php.
You will link to the page like so: yoursite.com/pagename
it wont change yoursite.com/index.php?page=pagename to yoursite.com/pagename in the adress bar after you send it.(if that makes sense :) )
I hope this is what you are looking for...
I think you mean your original URL to be www.mysite.com/index.php?page=services and not www.mysite.com/page=services
Also, you probably mean the opposite, you should switch www.mysite.com/services to www.mysite.com/index.php?page=service
Anyway, to change www.mysite.com/services to www.mysite.com/index.php?page=services then you need .htaccess, and the rule for that would be RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
As suggested earlier, you should read about .htaccess, regex, and rewrite rules. Best resource is the apache documentation here: http://httpd.apache.org/docs/2.2/howto/htaccess.html
I read your comment earlier that you need the opposite, I am not sure why you need that, because the whole idea of URL shortining is to make easy-to-remember URLs in addition to some security concerns that can be resolved. The URL is the first thing that is sent to load your webpage, then .htaccess changes it to some form undrstandable by PHP then PHP deals with get parameters for example.

Categories