LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-22-2011, 06:05 AM   #1
SkyerSK
Member
 
Registered: Oct 2010
Location: Europe
Distribution: Gentoo
Posts: 206

Rep: Reputation: 10
mod_rewrite problem with RewriteCond


Hello,
recently I have started playing with mod_rewrite, and now I am trying to accomplish following:

I'd like to use redirection for SEO friendly URLs:
Code:
RewriteRule ^userstats/?$ page.php?id=3
Also, to avoid users trying to get pages by themselves:
Code:
RewriteCond %{QUERY_STRING} id=[0-9]+
RewriteRule ^[a-zA-Z\.]+$ index.php
I understand that first rule should be placed below this one, as it would rewrite all pages,
but it does not work at all. Also, I tried adding L flag, to both rules, but it does not work.

Could anyone please point me to right solution, or some information about application of RewriteRules/Conditions? I don't know what's wrong there.

Thanks in advance
 
Old 05-22-2011, 09:53 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi,

Maybe dumb question but did you enable the rewrite engine with:
Code:
RewriteEngine On
as the first line? Without enabling the rewrite engine nothing gets processed.
I'd also make the URL rewrite conditions case insensitive with NC ([NC]) and the last rule always needs [L] as termination to indicate the end of the rewrite conditions and rules.

Kind regards,

Eric
 
Old 05-22-2011, 10:39 AM   #3
SkyerSK
Member
 
Registered: Oct 2010
Location: Europe
Distribution: Gentoo
Posts: 206

Original Poster
Rep: Reputation: 10
Thanks for your answer,
yes, RewriteEngine is turned on, also I have RewriteBase set. Rewriting itself works without any problems, I'm just getting some misbehavior (caused by my lack of knowledge) with rules stated in my first post.
 
Old 05-22-2011, 11:33 AM   #4
SkyerSK
Member
 
Registered: Oct 2010
Location: Europe
Distribution: Gentoo
Posts: 206

Original Poster
Rep: Reputation: 10
Still can't get satisfying results, rules are:
Code:
RewriteEngine on
RewriteBase /

RewriteCond %{QUERY_STRING} id=[0-9]+
RewriteRule [a-z0-9]+ index.php?q=redirected_by_condition [NC,R]

RewriteRule ^userstats$ index.php?id=2 [L]
As far as I understand RewriteRules, processing should start like this:
If query_string contains "id=" and some number, proceed with next rule. There anything is rewritten to index.php?q=redirected_by_condition.
If not, continue with next rule. Then, if URI Request is userstats, requested URL will be index.php?id=2 and there will be no further rewriting.

Why does it continue rewriting?

Thanks

Last edited by SkyerSK; 05-22-2011 at 11:34 AM.
 
Old 05-22-2011, 05:36 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by SkyerSK View Post
Code:
RewriteCond %{QUERY_STRING} id=[0-9]+
RewriteRule [a-z0-9]+ index.php?q=redirected_by_condition [NC,R]
Do you really want to replace the first alphanumeric sequence in the URL by index.php?q=redirected_by_condition? Assuming you wish to redirect any URLs with ID queries to the index.php in the same directory, I'd use
Code:
RewriteCond %{QUERY_STRING} ^id=([0-9]+) [OR]
RewriteCond %{QUERY_STRING} &id=([0-9]+)
RewriteRule /+[^/]*\.[^/]*$ /index.php?q=redir&oldid=%1 [R=301,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+) [OR]
RewriteCond %{QUERY_STRING} &id=([0-9]+)
RewriteRule /+([^\./]*)/*$ /$1/index.php?q=redir&oldid=%1 [R=301,L]
The first triplet replaces everything that contains a full dot after the last slash (including the slash), if the query contains an id parameter with a value that begins with a number. The second triplet does the same for URLs that end with a (dotless) directory name.

The R=301 flag tells mod_rewrite to do a permanent redirection (HTTP 301), and the L flag will tell mod_rewrite to not apply any further rules.

Quote:
Originally Posted by SkyerSK View Post
Why does it continue rewriting?
Because you did not tell it to stop; you didn't use the L flag. Also, the RewriteCond only applies to the very next RewriteRule.
 
1 members found this post helpful.
Old 05-23-2011, 11:24 AM   #6
SkyerSK
Member
 
Registered: Oct 2010
Location: Europe
Distribution: Gentoo
Posts: 206

Original Poster
Rep: Reputation: 10
Thanks much for your answer,
the code I posted was just as an example, what I really want to do is: (as I stated in first post)
1. Make URLs SEO friendly, by redirecting from easy-to-remember text urls:
Code:
RewriteRule ^userstats/?$ page.php?id=3
2. If user tries to write id explicitly, redirect him to index.
Code:
RewriteCond %{QUERY_STRING} id=[0-9]+
RewriteRule ^[a-zA-Z\.]+$ index.php
So the whole .htaccess looks like this:
Code:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} id=[0-9]+
RewriteRule [a-z0-9]+ index.php?q=redirected_by_condition [NC,R]

RewriteRule ^calendar$ index.php?id=2 [L]
The problem is that it does not work - I did not know, that "RewriteRule ^calendar$ index.php?id=2 [L]" generates absolutely new request (probably, looks like that), and that way the URL is being rewritten, just as when id parameter was stated explicitely. Is there any way to avoid this?

Thanks, I am newbie with mod_rewrite.
 
Old 05-23-2011, 01:30 PM   #7
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by SkyerSK View Post
Thanks much for your answer,
the code I posted was just as an example, what I really want to do is: (as I stated in first post)
1. Make URLs SEO friendly, by redirecting from easy-to-remember text urls:
Code:
RewriteRule ^userstats/?$ page.php?id=3
2. If user tries to write id explicitly, redirect him to index.
Code:
RewriteCond %{QUERY_STRING} id=[0-9]+
RewriteRule ^[a-zA-Z\.]+$ index.php
Ah, now I understand your problem.

You need to do proxy requests, and skip the rewrite rules for subrequests. Try this in a .htaccess file:

Code:
RewriteBase /baseURL
RewriteRule ^/*userstats/*$ /page.php?id=3 [P,NC]
RewriteCond &%{QUERY_STRING} &id=([0-9]+)
RewriteRule ^/*[^/]+$ /index.php?oldid=%1 [NS,R=302,L]
You also need to make sure mod_proxy and mod_proxy_http are loaded in your Apache configuration. (I recommend explicitly setting ProxyRequests Off and not using any ProxyPass directives; that is, you don't need to enable any of the proxy features, just make sure the modules are loaded.)
 
1 members found this post helpful.
Old 05-24-2011, 01:20 PM   #8
SkyerSK
Member
 
Registered: Oct 2010
Location: Europe
Distribution: Gentoo
Posts: 206

Original Poster
Rep: Reputation: 10
Thanks for your post,
I have my hands off the computer where web sites are being hosted, I should get to it by tomorrow. Sorry for keeping you waiting.

Last edited by SkyerSK; 05-28-2011 at 11:14 AM.
 
Old 05-27-2011, 01:39 PM   #9
SkyerSK
Member
 
Registered: Oct 2010
Location: Europe
Distribution: Gentoo
Posts: 206

Original Poster
Rep: Reputation: 10
Alright, thanks,
it seems to be working now (I'm about to play with it to see how exactly it works). Problem solved.

And sorry for keeping you waiting again.

Last edited by SkyerSK; 05-28-2011 at 11:16 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
Apache rule loop - make RewriteCond explicit timmywo Linux - Software 7 04-18-2010 08:50 AM
Doubt about RewriteCond? your_shadow03 Linux - Newbie 8 12-18-2009 03:29 PM
Correct Apache RewriteCond to stop proxying ... curtisa Linux - Security 1 07-21-2009 10:38 AM
RewriteCond & Rewrite Rule Problemo sea-bass Linux - Server 0 04-07-2008 10:44 AM
mod_rewrite problem jfall Programming 1 02-22-2005 07:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 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