LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-12-2015, 05:36 AM   #1
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Rep: Reputation: 33
500 Internal server error when using the mod_rewrite


Hello.

I have such links in a web site

/post.php?id=14
/cat.php?t=y&cid=6666

and i create an .htaccess file with that content inside.

Code:
Options +FollowSymLinks 
RewriteEngine On 
RewriteRule ^([^/]*)$ /post.php?id=$1 [L] 
RewriteRule ^([^/]*)/([^/]*)$ /cat.php?t=$1&cid=$2 [L]
but on refresh, apache returns 500 Internal server error.

I looked on logs and found that :
Quote:
AH00124: 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.
Why that happens ? Mod_rewrite is enabled in httpd.conf.
 
Old 05-12-2015, 08:15 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
I looked on logs and found that :

AH00124: 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.
Why that happens ? Mod_rewrite is enabled in httpd.conf.
This happens because rewrite goes on loop.
you need a condition, so rewrite stops as soon as it meets. E.g:
Code:
RewriteEngine On 
RewriteCond %{REQUEST_URI} !^/post.php
RewriteRule ^([^/]*)$ /post.php?id=$1 [L]
I don't understand what you're trying to do, but I guess you have to define the rewrite based on the original URI (/post.php or /cat.php), e.g.
Code:
RewriteRule ^/post.php([^/]*)$ /post.php?id=$1 [L] 
RewriteRule ^/cat/php([^/]*)/([^/]*)$ /cat.php?t=$1&cid=$2 [L]
 
1 members found this post helpful.
Old 05-13-2015, 02:09 AM   #3
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Original Poster
Rep: Reputation: 33
I want to show the URL in a different way.

The www.site.com/post.php?id=24 to become www.site.com/24

and

The www.site.com/cat.php?t=y&cid=2015 to become www.site.com/y/2015
 
Old 05-13-2015, 04:56 AM   #4
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by netpumber View Post
I want to show the URL in a different way.

The www.site.com/post.php?id=24 to become www.site.com/24

and

The www.site.com/cat.php?t=y&cid=2015 to become www.site.com/y/2015
You can use the %{QUERY_STRING} in the rewrite conditions as follows:
Code:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^/post.php /%1? [L]
RewriteCond %{QUERY_STRING} ^t=(.*)\&cid=(.*)
RewriteRule ^/cat.php /%1/%2 [L]
 
Old 05-14-2015, 02:24 AM   #5
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Original Poster
Rep: Reputation: 33
tried it but doesn't seem to work. It shows URLs as before.

Used this in .htaccess file at root dir.

Code:
Options +FollowSymLinks 
RewriteEngine On 
RewriteCond {QUERY_STRING} ^id=(.*)$
RewriteRule ^/post.php /%1? [L]
RewriteCond %{QUERY_STRING} ^t=(.*)\&cid=(.*)
RewriteRule ^/cat.php /%1/%2 [L]
 
Old 05-14-2015, 03:27 AM   #6
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Used this in .htaccess file at root dir.
In this case you don't need the leading slash:
Code:
Options +FollowSymLinks 
RewriteEngine On 
RewriteCond {QUERY_STRING} ^id=(.*)$
RewriteRule ^post.php /%1? [L]
RewriteCond %{QUERY_STRING} ^t=(.*)\&cid=(.*)
RewriteRule ^cat.php /%1/%2? [L]
 
Old 05-15-2015, 12:47 AM   #7
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Original Poster
Rep: Reputation: 33
Even with this seems not working.
 
Old 05-15-2015, 01:45 AM   #8
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by netpumber View Post
Even with this seems not working.
How exactly is not working? Could you be more specific?
 
Old 05-16-2015, 12:02 PM   #9
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Original Poster
Rep: Reputation: 33
I add those lines in an .htaccess at the root directory, and when i visit the site URLs still have that form :

www.site.com/post.php?id=24
 
Old 05-16-2015, 04:10 PM   #10
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by netpumber View Post
I add those lines in an .htaccess at the root directory, and when i visit the site URLs still have that form :

www.site.com/post.php?id=24
Somewhere between posts 4-5 we missed the % in front of the {QUERY_STRING}. Try to add it and see if it works
Code:
Options +FollowSymLinks 
RewriteEngine On 
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^post.php /%1? [L]
RewriteCond %{QUERY_STRING} ^t=(.*)\&cid=(.*)
RewriteRule ^cat.php /%1/%2? [L]
If it still doesn't work, make sure that apache reads the .htaccess files
 
Old 05-17-2015, 09:31 AM   #11
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Original Poster
Rep: Reputation: 33
It seems that reads .htaccess cause AllowOverride is setted to All. Now with that change, when i'm going to visit the link , server returns 404, Object not found.
 
Old 05-17-2015, 12:03 PM   #12
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by netpumber View Post
It seems that reads .htaccess cause AllowOverride is setted to All. Now with that change, when i'm going to visit the link , server returns 404, Object not found.
Yup it reads it. The 404 error is because the URL /post.php?id=X is rewritten to /X and there is no such thing. So I guess that you just want to replace the actual query_string with a path made from it
If that's the case, assuming that post.php query_string accepts only numeric values, while cat.php accepts as first argument a string and as second a number, try the following
Code:
RewriteCond %{REQUEST_URI} !^/post.php
RewriteRule ^([0-9]+)$ /post.php?id=$1 [L]

RewriteCond %{REQUEST_URI} !^/cat.php
RewriteRule ^([^/]+)/([0-9]+)$ /cat.php?t=$1&cid=$2 [L]
Btw the 2 RewriteCond are not necessary if you meet the above query_string restrictions
 
1 members found this post helpful.
Old 05-18-2015, 01:29 AM   #13
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Original Poster
Rep: Reputation: 33
Tried what you wrote but url remains the same
 
Old 05-18-2015, 04:54 AM   #14
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by netpumber View Post
Tried what you wrote but url remains the same
Isn't that what are you trying to do?
Using the latest rewrites, the client sees http://www.site.com/24 in the address bar, while he actually visits http://www.site.com/post.php?id=2
and same goes for cat.php
If you want to change the URL in address bar to show http://www.site.com/post.php?id=2, add [R] in both the rewriterules, e.g:
Code:
RewriteRule ^([0-9]+)$ /post.php?id=$1 [L,R]
RewriteRule ^([^/]+)/([0-9]+)$ /cat.php?t=$1&cid=$2 [L,R]
 
1 members found this post helpful.
Old 05-19-2015, 02:16 AM   #15
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Original Poster
Rep: Reputation: 33
Oh, ok that was my fault.

post seems now to work but for the cat.php if i visit www.site.com/y/2005

it loads the page but not correctly and without css

Failed to load resource: the server responded with a status of 404 (Not Found) http://www.site.com/y/styles/common.css

Last edited by netpumber; 05-19-2015 at 02:26 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
500 Internal Server Error Nagesh sheregar Linux - General 2 08-14-2009 01:52 AM
Getting 500 Internal Server Error !! Deepaks Linux - Server 0 04-11-2009 06:49 AM
500 Internal Server Error shahz Red Hat 9 03-31-2009 03:05 AM
500 Internal server error fa_khan50 Linux - Newbie 0 01-02-2008 12:13 AM
500 Internal Server Error bship Linux - Software 5 01-19-2007 04:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration