I'm trying to do sorting passing get value in htaccess to php:
I have sth like:
RewriteRule ^pubs$ pubs.php
RewriteRule ^pubs/beer$ pubs.php?sort=beer
Above code is working fine. Now Im trying to do the same thing for subpages (cities):
RewriteRule ^pubs/(.*)$ district.php?name=$1
RewriteRule ^pubs/(.*)/beer$ district.php?name=$1&sort=beer
The links look like:
www.domain.com/pubs/beer - working ok
www.domain.com/pubs/new-york/beer - not working
What I'm doing wrong? If i delete line RewriteRule ^pubs/(.*)$ district.php?name=$1 then the link www.domain.com/pubs/new-york/beer is working. I spend hours to find solution with no luck. I will be grateful for help.
If i delete line RewriteRule ^pubs/(.*)$ district.php?name=$1 then the
link www.domain.com/pubs/new-york/beer is working.
because www.domain.com/pubs/new-york/beer is matching by
RewriteRule ^pubs/(.*)$ district.php?name=$1
resulting in district.php?name=newyork/beer instead of matching
RewriteRule ^pubs/(.*)/beer$ district.php?name=$1&sort=beer
one way you could fix this would be:
RewriteRule ^pubs/([^/]*)$ district.php?name=$1
so if there is an additional slash anywhere after your city the rule will be ignored and proceed to the 2nd rule... just be sure to put it after
RewriteRule ^pubs/beer$ pubs.php?sort=beer
or it will match beer as if it were a city name like district.php?name=beer
Related
I have a webpage which has ugly urls like this
DOMAINNAME/gallery.php#filter=.filtername
I want them to look like this
DOMAINNAME/artwork/filtername/
I've tried this in my .htaccess file
RewriteRule ^artwork/([^.]+)/ gallery.php#filter=.$1 [NE]
But this doesn't do the trick. It just goes to DOMAINNAME/artwork/
If you are adding # in URL then you must do a full redirect since # part is only interpreted in browser:
RewriteRule ^artwork/([^./]+)/?$ /gallery.php#filter=.$1 [L,NE,NC,R=302]
I just tried this in my environment and it seems, Apache swallows everything after and including #, when it does an internal rewrite.This happens no matter, whether you use flag NE or not.
So the only solution seems to be using a regular query string, e.g.
RewriteRule ^artwork/([^.]+)/ gallery.php?filter=.$1 [L]
or separate the filter with a slash
RewriteRule ^artwork/([^.]+)/ gallery.php/filter=.$1 [L]
This is how I would like to get my url to look at it any other way than before.
The problem is such that I must have forum and after forum content unique number that is in the database and after the url of the content.
I could imagine that it looked like this:
/forum/1/hello-world-stackoverflow-danmark/
now on my .htaccess file:
RewriteRule ^forum/([^/.]*)/([^/.]*)/?$ /forum-s.php?id=$1&url=$2 [L]
and now it:
www.hello-world.com/forum/velkommen-til-traenigsmakker---sjaelland/
I will have its to
www.hello-world.com/forum/1/velkommen-til-traenigsmakker---sjaelland/
Be sure to have RewriteEngine On
The code you have should work, you can optimize it like this to ensure the numbers.
RewriteRule ^forum/([0-9]+)/([^/.]*)/?$ /forum-s.php?id=$1&url=$2 [L]
I have been reading about .htaccess files for a couple of hours now and I think I'm starting to get the idea but I still need some help. I found various answers around SO but still unsure how to do this.
As far as I understand you write a rule for each page extension you want to 'prettify', so if you have something.php , anotherpage.php, thispage.php etc and they are expecting(will receive??) arguments, each needs its own rule. Is this correct?
The site I want to change has urls like this,
maindomain.com/sue.php?r=word1%20word2
and at least one page with two arguments
maindomain.com/kevin.php?r=place%20name&c=person%20name
So what I would like to make is
maindomain.com/sue/word1-word2/
maindomain.com/kevin/place-name/person-name/
Keeping this .php page and making it look like the directory. Most of the tutorials I have read deal with how to remove the .php page to which the argument is passed. But I want to keep it.
the problem I am forseeing is that all of the .php?r=parts of the url are the same ie sue.php?r=, kevin.php?r= and the .htaccess decides which URL to change based on the filename and then omits it. If I want to keep the file name will I have to change the ?r=
so that it is individual? I hope this make sense. So far I have this, but I'm sure it won't work.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1&c=$1
And I think I have to add ([^-]*) this in some part or some way so that it detects the %20 part of the URL, but then how do I convert it to -. Also, how are my $_GET functions going to work??
I hope my question makes sense
You're missing a space somewhere in those rules, but I think you've got the right idea in making 2 separate rules. The harder problem is converting all the - to spaces. Let's start with the conversion to GET variables:
# check that the "sue.php" actually exists:
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/?$ /$1.php?r=$2 [L,QSA]
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$ /$1.php?r=$2&c=$3 [L,QSA]
Those will take a URI that looks like /sue/blah/ and:
Extract the sue part
Check that /document_root/sue.php actually exists
rewrite /sue/blah/ to /sue.php?r=blah
Same thing applies to 2 word URI's
Something like /kevin/foo/bar/:
Extract the kevin part
Check that /document_root/kevin.php actually exists
3 rewrite /kevin/foo/bar/ to /kevin.php?r=foo&c=bar
Now, to get rid of the "-" and change them to spaces:
RewriteCond %{QUERY_STRING} ^(.*)(c|r)=([^&]+)-(.*)$
RewriteRule ^(.*)$ /$1?%1%2=%3\ %4 [L]
This looks a little messy but the condition matches the query string, looks for a c= or r= in the query string, matches against a - in the value of a c= or r=, then rewrites the query string to replace the - with a (note that the space gets encoded as a %20). This will remove all the - instances in the values of the GET parameters c and r and replace them with a space.
I have got a problem that seems to me something simple but i'm new to htaccess and can't find the solution.
i've got the following line in my htaccess (root map)
RewriteRule ^page1/([a-zA-Z0-9_-]+)/$ page1.php?name=$1
RewriteRule ^page1/([a-zA-Z0-9_-]+)$ page1.php?name=$1
When i enter the the following url it works without a problem
www.myexample.com/page1/variable
the strange thing happens when I add a / at the end. Then the page can't get the GET value out of the URL.
Thanks for your time and help!
Get rid of the ending /$ sign on the first rule
RewriteRule ^page1/([a-zA-Z0-9_-]+) page1.php?name=$1
Or you can continue to capture data
RewriteRule ^page1/([a-zA-Z0-9_-]+)/(.*)$ page1.php?name=$1
Ultimately if you want to keep capturing more data with "/" as a delimiter, I suggest doing
RewriteRule ^page1/(.*)$ page1.php?url=$1
Then use the server side script to determine what to do.
I am trying to get the follow urls to work
www.urlname.com/team/1-Test
www.urlname.com/team/1-Test/members
RewriteRule ^team/([^-]+)-([^&]+)$ index.php?p=teamprofile&team_name=$2&team_id=$1
RewriteRule ^team/([^-]+)-([^&]+)/members$ index.php?p=teammembers&team_name=$1&team_id=$2
but when i try the link with /members init it goes to the other page?
can someone help me please
Thanks
[^-] and [^&] includes the / so /members is included with that. you could either add / to your negation character groups like [^-/] and [^&/] so it doesn't match / or move the bottom one up and add [L] after it to tell apache this is the [L]ast rule to check if it matches.
The trouble is, your second rule is being satisfied by the first rule. You could simply switch them around and it will work:
RewriteEngine On
RewriteRule ^team/([^-]+)-([^&]+)/members$ index.php?p=teammembers&team_name=$1&team_id=$2
RewriteRule ^team/([^-]+)-([^&]+)$ index.php?p=teamprofile&team_name=$2&team_id=$1
Although, a slight change in the first rule will also address the problem:
RewriteEngine On
RewriteRule ^team/([^-]+)-([^/]+)[/]?$ index.php?p=teamprofile&team_name=$1&team_id=$2 [L]
RewriteRule ^team/([^-]+)-([^/]+)/members[/]?$ index.php?p=teammembers&team_name=$1&team_id=$2 [L]
Note, I changed the match string in the first rule from ([^&]+) to ([^/]+) - that way the forward slash isn't included in the match in cases like mydomain.com/team/1-2/. The [/]? rule at the end is an optional match for that trailing forward slash. I've likewise added one to the end of the members rule as well, now it works like this:
mydomain.com/team/1-2/ - goes to index.php?p=teamprofile&team_name=1&team_id=2
mydomain.com/team/1-2 - goes to index.php?p=teamprofile&team_name=1&team_id=2
mydomain.com/team/1-2/members - goes to index.php?p=teammembers&team_name=1&team_id=2
mydomain.com/team/1-2/members/ - goes to index.php?p=teammembers&team_name=1&team_id=2