ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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
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
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.
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.
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 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.
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.)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.