Nazeer Ahamed
Nazeer Ahamed

Reputation: 38

shorten url using htaccess file?

I am completely beginner to htaccess, I am trying to shorten my following url..

From

 http://website.com/index.php?student-name=john
 http://website.com/index.php?teacher-name=amy
 http://website.com/index.php?class=xxx

To

 http://website.com/john
 http://website.com/amy
 http://website.com/xxx

I have tried following .htaccess code,

 Options +FollowSymlinks
 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^([^/]+)/?$ /index.php?student-name=$1 [L]
 RewriteRule ^([^/]+)/?$ /index.php?teacher-name=$2 [L]
 RewriteRule ^([^/]+)/?$ /index.php?class=$3 [L]

but, i shows "500-Internal servor error"....

EDIT :

when i am trying to use one rewrite_rule, its works fine. Like

 RewriteRule ^([^/]+)/?$ /index.php?student-name=$1 [L]              

(or)

 RewriteRule ^student/([^/]+)/?$ /index.php?student-name=$1 [L]

when i am trying to use two or more rewrite_rule, its shows "500-Internal servor error". Like

RewriteRule ^([^/]+)/?$ /index.php?student-name=$1 [L]              
RewriteRule ^teacher/([^/]+)/?$ /index.php?teacher-name=$1 [L]

(or)

RewriteRule ^student/([^/]+)/?$ /index.php?student-name=$1 [L]
RewriteRule ^teacher/([^/]+)/?$ /index.php?teacher-name=$1 [L]

Its shows error log is: "Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace."

what's that mean?

Upvotes: 0

Views: 2189

Answers (1)

Sumurai8
Sumurai8

Reputation: 20745

This is a bad idea. Why? How does Apache know if the string in the first path segment is a student, teacher or class? It doesn't, so it will always rewrite to a student-name.

Instead use urls like:

http://example.com/student/john
http://example.com/teacher/amy
http://example.com/class/xxx

Now the rewrite is trivial, since each group has a common prefix.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^student/([^/]+)/?$ /index.php?student-name=$1 [L]
RewriteRule ^teacher/([^/]+)/?$ /index.php?teacher-name=$1 [L]
RewriteRule ^class/([^/]+)/?$ /index.php?class=$1 [L]

As for the 500 internal server error you need to check your Apache error log. Make sure that mod_rewrite is enabled, and that you restarted your Apache after you did this. Besides the rules, I changed the lowercase l in FollowSymLinks into an uppercase L, but I am unsure if this can cause any problems.

Upvotes: 1

Related Questions