Jump to content

PHP & Friendly URLs help


vadim88

Recommended Posts

Hi,

 

I was wondering how could i convert my long dirty links into nice friendly urls

 

i have the following:

 

http://www.127.0.0.1/php/?act=articles&alias=MY-ARTICLE

http://www.127.0.0.1/php/index.php?act=articles&alias=MY-ARTICLE

http://www.127.0.0.1/?act=articles&alias=MY-ARTICLE

http://www.127.0.0.1/index.php?act=articles&alias=MY-ARTICLE

 

http://www.127.0.0.1/php/?act=articles&id=7

http://www.127.0.0.1/php/index.php?act=articles&id=7

http://www.127.0.0.1/?act=articles&id=7

http://www.127.0.0.1/index.php?act=articles&id=7

 

http://127.0.0.1/php/?act=articles&alias=MY-ARTICLE

http://127.0.0.1/php/index.php?act=articles&alias=MY-ARTICLE

http://127.0.0.1/?act=articles&alias=MY-ARTICLE

http://127.0.0.1/index.php?act=articles&alias=MY-ARTICLE

 

Notice the changes, that the top ones use www and the lower ones don't. one uses the /php directory others don't, Some uses the index.php file and some don't, Some has numbers in the 'id' key and some text in the 'alias' key.

 

How can i write rewrite rules that first it will do the same if the url has www or don't, Second it will work regardless if the index.php file exists in the url or not, it will know if the 'id' key is a number it will go to id={id} and if the alias is a string it will use alias='{alias}' and the end result will be:

 

http://www.127.0.0.1/articles/MY-ARTICLE.htm

http://www.127.0.0.1/articles/MY-ARTICLE.htm

http://www.127.0.0.1/articles/MY-ARTICLE.htm

http://www.127.0.0.1/articles/MY-ARTICLE.htm

 

http://www.127.0.0.1/articles/7.htm

http://www.127.0.0.1/articles/7.htm

http://www.127.0.0.1/articles/7.htm

http://www.127.0.0.1/articles/7.htm

 

http://127.0.0.1/articles/MY-ARTICLE.htm

http://127.0.0.1/articles/MY-ARTICLE.htm

http://127.0.0.1/articles/MY-ARTICLE.htm

http://127.0.0.1/articles/MY-ARTICLE.htm

 

 

Would appreciate any help given (Willing to pay 25$ for a full rules that works)

 

Link to comment
Share on other sites

I know, I have access and can use it, And i do already i just need someone to help Or write the right rewrite rules that will produce what i wrote above, I can google myself so that post wasn't very helpful. Like i said i can pay someone to help out or write it down via paypal.

 

Thanks.

Link to comment
Share on other sites

If it's easier and someone knows then an alternative solution would be converting each _GET key into it's own directory so if i have this:

 

index.php?act=articles&code=test&alias=MY-ARTICLE

 

then it will be

 

index/act/articles/code/test/alias/MY-ARTICLE

 

That's a good alternative as well.

 

Thanks.

Link to comment
Share on other sites

And instead of allowing any string for the 'alias', it would be better to allow only certain chars (at least disallow a forward slash, as that seems confusing). So instead of (.+) in the code, e.g. ([^/]+) - anything not a slash.

 

To answer your post: It's pretty difficult to try the www. thingy out on 127.0.0.1 (I've come to find). But this code below should remove www. from all requests (the way I like to do it):

 

RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://example.com/$1 [L,R]

 

Just put the three lines right after the RewriteBase line.

Link to comment
Share on other sites

First i appreciate the help if you want i can transfer some cash via paypal to your account.

How about i show you what i have done and if someone finds something wrong let me know please.

### REWRITES ###
Options +FollowSymLinks
RewriteEngine on

### Global Acts ###

# Rewrite act/?/.htm|html into index.php?act=$1 anything but forwarding slash for the next rule to exists
RewriteRule act/([^/]+)\.(htm|html)$ index.php?act=$1


# rewrite index/act/?/?/?.htm|html into index.php?act=$1&$2=$3 
# rewirtes stuff like index/act/login/code/00.htm into index.php?act=login&code=00
RewriteRule act/(.*)/(.*)/(.*)\.(htm|html)$ index.php?act=$1&$2=$3

### Articles ###

# rewrite articles/{number}.htm|html into index.php?act=articles&code=article-by-id&id=$1
RewriteRule articles/([0-9]+)\.(htm|html)$ index.php?act=articles&code=article-by-id&id=$1

# rewrite articles/{string&numbers}.htm|html into index.php?act=articles&code=article-by-alias&alias=$1
RewriteRule articles/([A-Za-z0-9]+)\.(htm|html)$ index.php?act=articles&code=article-by-alias&alias=$1

 

And i want the www to exists if there is no www then to use www i dont want it to be 127.0.0.1 but www.127.0.0.1

 

 

Link to comment
Share on other sites

Your code seems fine.

 

If it's possible to add www. to 127.0.0.1 (I can't get it to work, but may be my settings), the code below will work (from the official Apache URL rewriting guide):

 

RewriteCond %{HTTP_HOST}   !^www\.127\.0\.0\.1 [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www.127.0.0.1/$1 [L,R]

 

it should be added as the first (Cond)/Rule in the .htaccess file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.