I have a problem with RewriteRule flags. I have a "main" RewriteRule that handles my application, but I also have some urls that need to be handled differently (see them as custom routes).
I've tried many different flags, but it never gives me the result I want. Check the first comment in the code for what I want.
# First check if this pattern is found, and if it's not, continue to the next one and disregard this one
RewriteRule ^test/report/([0-9]+)/?$ index.php?page=report&id=$1 [PT,L]
RewriteRule ^(.*)/?$ index.php?page=$1 [PT,L]
Thanks in advance!
You use the C flag, for chaining, on your first rule, which means that the second rule will be disregarded if the first does not match. Your comment describes what would happen without the C flag. I think you may want an L flag instead if you want to avoid applying the second rule if the first rule matches.
Edit:
Now that you've changed the C flag to L on your first rule, there is another issue. You need to ensure that a rewritten path does not get re-processed by your RewriteRule directives in some way that will change it.
In this case, one way to accomplish that goal is to ensure that the page itself is not already rewritten to index.php:
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^test/report/([0-9]+)/?$ index.php?page=report&id=$1 [PT,L]
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)/?$ index.php?page=$1 [PT,L]
Unfortunately, I do not know of a way to make the same RewriteCond apply to all your rules other than repeating it before each one.
Edit:
Now that I think of it, yes, there is possibly a somewhat better solution. Instead of using the RewriteCond directives, start with:
RewriteRule ^index.php - [PT,NC,L]
Now, anything starting with index.php will match, nothing changes, and no more RewriteRules will be followed.
Related
I have searched in SO but never came across this specific answer. If anyone can help.
This use to be my URL structure /index.php?param1=is1 and I got it to look like this /is1 by using this Rewrite.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
Do not ask my how I got this as it's been a while that this was written. Now the issue is i have a second parameter. So my url that is like this /index.php?param1=is1¶m2=is2 needs to be like this /is1/is2.
Obviously, now it's like this /is1¶m2=is2. So i guess what I am searching for is a way to ask if ¶m2= is seen than hide it ?
I really don't get the logic behind all this URLREWRITE. I am trying to clean my URL structure for SEO purposes and COPY/PASTE logic as URL's should be shared between different users / clients. Obviously, having /this-is-something&this=that is not that bad but having /this-is-something/that is much better.
Just for info, first parameters is BRANDS /this-is-my-brand1 or /this-is-my-brand2 and second parameter is a product as /this-is-my-brand1/product1 before all that I use to have /index.php?brand=brand-one&product=product-one. Changing everything is not an issue, I can start from scratch if there is anything better than what I have.
I am on the learning curve, so I don't mind long explanations.
Any help is appreciated.
If your request parameters name are static (never change) you can use a more specifiq rule
RewriteRule ^(.+)/(.+)$ index.php?brand=$1&product=$2 [L]
I have a cascade problem with my .htaccess rules. Consider the following:
RewriteEngine on
RewriteRule ^product/(.*)$ product.php [L,QSA]
RewriteRule ^(.*)$ index.php [L]
With the above, if I requested a URL like http://example.com/product/product-slug, then I’d expect the request to get routed to product.php. However, it doesn’t; my index.php script is picked the request up.
I would have thought that the first RewriteRule would be matched, and as it has a L (last) flag that no further RewriteRules would be matched, including the “catch-all” one at the bottom.
Why is this not working as expected?
This should sort it:
RewriteRule ^product/(.*)$ product.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !product.php
RewriteRule ^.*$ index.php [L]
The problem is that because the rules were in different sets, i.e. not attached a condition, it only stopped processing the current set of rules (the first one) and jumped onto the second.
Hope that clears it all up :)
Perhaps a typo in your code? You're writing "http://example.com/products/" in your question, but in the code you're targeting ^product$, with no s.
Also, your first rule is too strict. It will only match http://example.com/product/. You need to include a wild card after product to allow it to pick up product-slug. Something like RewriteRule ^products/(.*)$ product.php [L,QSA] should work.
Is it not because of the order you have placed the rules in? The one below will override changes to the one above it. Try changing them around.
Also, do you need to set the RewriteBase or not? Is your project on an actual domain, or locally stored in a sub-directory of the server root?
I hope someone can help me with a mod_rewrite rule, that works apart from adding trailing slashes.
This is the rule
<IfModule rewrite_module>
Options Indexes FollowSymLinks +IncludesNOEXEC
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.org$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.org/$1 [L,R=301]
</IfModule>
The purpose is to rewrite the URL mydomain.org in the form www.mydomain.org
This works. but then www.mydomain.org// shows in the browser address bar. Checking the rewrite log shows that the // is explicitly created by the rule
Questions are:
Do (or could) the two slashes matter?
If the answer to (1) is yes, can I redo the rule so that the trailing slashes are omitted?
Most servers ignore the double slash and treat it as a single. See for example this question (double-slash) https://stackoverflow.com//questions/13027041
To fix your RewriteRule, I believe you just need to change it to
RewriteRule ^/?(.*)$ http://www.mydomain.org/$1 [L,R=301]
The /? part makes it optional (root access), and if it is found, it gets stripped since it is not part of the captured (.*) section.
No, two slashes generally doesn't matter, it doesn't confuse software anyhow. It just means you have to transfer one byte more (which can matter in some situations).
Just change your rewrite rule to RewriteRule ^/?(.*)$ http://www.mydomain.org/$1 [L,R=301]. That /? is what does the trick.
I'm trying to use mod_rewrite to point the blog portion of a site to a blog site.
this is what I have to handle the normal stuff
RewriteRule ^(\w+)/?$ index.php?page=$1
This is what i'm trying to use for the blog site
RewriteRule ^blog/?$ http://url.to.my.blogger.site
but it's not working, when I go to site/blog it directs me to index.php?page=blog is there something I need to do to not do the second rewrite if the first is correct? like an if/else? sorry don't know much about mod_rewrite so any advice would be awesome.
also I noticed that if I try to do something like site/home everything works fine but if I attempt to hit site/home/ it puts all of my urls into the wrong context, for example my css and images don't get loaded correctly.
my full file is this
RewriteEngine on
RewriteRule ^blog/?$ remote/blog/uri/here
RewriteRule ^(\w+)/?$ index.php?page=$1
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php - [F]
and when i hit site/blog it still tries to serve index.php?page=blog, I'm guessing I have to break out of the code at some point? I couldn't find documentation on if/else statements
I needed to add flags to my RewriteRule lines so that the server wouldn't evaluate further. Changing them to be
RewriteRule ^/blog http://url.to.blog [L]
did the trick, the problem was that it was evaluating all the way down, seeing as I wasn't attempting to go to index the last valid rule to evaluate was the general rewrite rule.
I am currenctly facing some htaccess/rewriterule issues. (And I am new to this area)
Let's assume we have an url like this:
http://mypage.at/very/cool
The URL is supposed to look like this (Cause I am using an AJAX-loadedContent which requires this):
http://mypage.at/#ajx/very/cool
So I would like to add the part '#ajx' to every url which does not already contain it.
Which means if an url does already look like: http://mypage.at/#ajx/so/pretty then there is no need for changes.
As I am not sure wheter this creates troubles with the GoogleSearchIndex, I would additionally like to know if there is a way to exclude this rule for searchbots.
Thanks for any help.
Ripei
Since you reported that this does not work (which is probably because your version of Apache doesn't support Perl-style RegEx):
RewriteRule ^(?!#ajx)(.*)$ http://mypage.at/#ajx/$1 [L]
I think this should do it:
RewriteCond %{REQUEST_URI} !^/#ajx
RewriteRule ^(.*)$ http://mypage.at/#ajx/$1 [L]
EDIT: After trying this myself and reading around on the Internet, I'm not sure this is actually possible. A pound sign (#) is not a legal part of a URL. This answer comes close, but I'm going to have to leave this to somebody who knows more to say whether this can even be done the way #Ripei asked for.
Something like this might work:
RewriteCond %{REQUEST_URI} !^/#ajx
RewriteRule ^(.*) http://%{SERVER_NAME}/#ajx$1 [R,L]
Hah finally after lot's of reading and testing - I got it.
RewriteCond %{REQUEST_URI} !^#ajx
RewriteRule . /\#ajx%{REQUEST_URI} [L,R,NE]
This Code works for me... don' ask me why this one is working, to be honest I do not have a clue! But well I am fine with the fact, that it IS working :)