0

I have a slight problem. I have a site that uses these formats for viewing certain PHP documents:

domainname.com/server.php?id=14
domainname.com/changeserver.php?id=14
domainname.com/customize.php?id=14

I would like to make these:

domainname.com/server/14
domainname.com/changeserver/14
domainname.com/customize/14

I also have various URLs such as /index.php and /login.php I would like to clean up. If someone were to type the original URL, such as /server.php?id=14, I would like it to redirect to it's clean URL.

If anyone can provide me with a config or help me along the way to making one that can do this, it would be greatly appreciated.

Maz I
  • 3,664
  • 2
  • 23
  • 38
MechaAdsta
  • 27
  • 4
  • possible duplicate of [Redirect all to index.php htaccess](http://stackoverflow.com/questions/18406156/redirect-all-to-index-php-htaccess) – nietonfir Jan 07 '14 at 06:28
  • 1
    I am asking for something different than just redirecting everything to index. Not a duplicate. – MechaAdsta Jan 07 '14 at 16:12

5 Answers5

0

To prevent an infinite loop while externally redirecting the ugly url to the clean url and internally rewriting it back to the ugly url, you can use the THE_REQUEST trick.

#External redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(server|changeserver|customize)\.php\?id=([^&]+)\ HTTP
RewriteRule ^ /%2/%3? [R,L]
#Change above to [R=301,L] once all rules work as expected

#Internal rewrite
RewriteRule ^(server|changeserver|customize)/(.*)$ /$1?id=$2 [L]
Sumurai8
  • 20,333
  • 11
  • 66
  • 100
  • When I use this, it redirects incorrectly. /server.php?id=14 Redirects to: /server/14/ HTTP/1.0?id=14/ Resulting in a 404 error. – MechaAdsta Jan 07 '14 at 16:04
  • While writing it I seem to have deleted the trailing `\ HTTP` I had behind the first condition. I think that should fix the problem. – Sumurai8 Jan 07 '14 at 16:52
  • @Samurai8 The HTTP part is gone, but I am still getting domainname.com/server/14?id=14. Tried messing with the code and nothing is working. – MechaAdsta Jan 08 '14 at 00:42
  • I've added a `?` after the first RewriteRule. This will replace the current query string with this query string, which is empty. Alternativelly, you can use the `QSD` flag (query string discard), like this: `[R,L,QSD]`. – Sumurai8 Jan 08 '14 at 09:21
  • @Samurai8 I added the revised code, and now it is forwarding to /home/minec288/public_html/server.php?id=14... Beginning to think this may be my system. Can anyone else test this to see if it works? – MechaAdsta Jan 08 '14 at 15:07
  • Put the `[L]` flag behind all of your rules and add `RewriteBase /` under your `RewriteEngine On` and above any of your other rules. – Sumurai8 Jan 08 '14 at 15:57
0

You need to rewrite your URLs in .htaccess file.

RewriteEngine On
RewriteRule ^server/(.*)$ server.php?id=$1 [R=301,L]

here is a good guide on URL rewriting

http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

  • With this, if you type in the dirty URL, the dirty URL doesn't go away. If you type in the clean URL, it redirects to /home/minec288/public_html/server.php?id=14, resulting in a 404. Useful article though, but htaccess is still foreign to me. – MechaAdsta Jan 07 '14 at 16:11
0

In this case you have to use a 301 Redirect of URLS. Here is an example:

Redirect

www.example.com/a1/articles.php?id=1&name=abc

to

www.example.com/article/1/abc

For this you have to modify the .htaccess file with the following:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)&name=(.*)$
RewriteRule ^articles\.php$ /article/%1/%2? [R=301]
RewriteRule ^article/([^-]+)/([^-]+)$ /articles.php?article_id=$1&article_name=$2 [L]
Trojan
  • 2,256
  • 28
  • 40
Mukilan R
  • 445
  • 5
  • 17
0
RewriteEngine On
RewriteRule ^server/([A-Za-z0-9-_,()'+]+)?$ server.php?id=$1
RewriteRule ^changeserver/([A-Za-z0-9-_,()'+]+)?$ changeserver.php?id=$1
RewriteRule ^customize/([A-Za-z0-9-_,()'+]+)?$ customize.php?id=$1
Sumurai8
  • 20,333
  • 11
  • 66
  • 100
  • With this, if you type in the dirty URL, the dirty URL doesn't go away. If you type in the clean URL, it redirects to /home/minec288/public_html/server.php?id=14, resulting in a 404. – MechaAdsta Jan 07 '14 at 16:09
0

To adapt to your code. Here is the answer.

Options +FollowSymLinks

RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^server\.php$ /server/%1? [R=301]
RewriteRule ^server/([^-]+)$ /server.php?id=$1 [L]

Check the Above code. I think this will help you.

Mukilan R
  • 445
  • 5
  • 17