Question

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

Était-ce utile?

La solution

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:

  1. Extract the sue part
  2. Check that /document_root/sue.php actually exists
  3. rewrite /sue/blah/ to /sue.php?r=blah

Same thing applies to 2 word URI's

Something like /kevin/foo/bar/:

  1. Extract the kevin part
  2. 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top