Mod_rewrite rule doesent work - mod-rewrite

I need to rewrite some url on my site
http://wwww.example.com/poem.php?s=to-sally
to
http://wwww.example.com/poems/to-sally
I wrote this rule but doesnt work
RewriteRule ^poems/([a-zA-Z0-9-]+)$ poem.php?s=$1

A couple things:
Make sure you have RewirteEngine On, and actually have the mod_rewrite module loaded and running
I believe the paths being matched against start with /, but your rule dictates that the first character is p. Try replacing your match expression with ^/poems/([a-zA-Z0-9-]+)$ and see if that does the trick.

Related

Is mod_rewrite rewrite rule match depending on position in substitution?

I try to echo current state of a URL being rewrited in .htaccess if query string contains DEBUG phrase:
RewriteCond %{QUERY_STRING} DEBUG
RewriteRule .+ echo.php?ip=%{REMOTE_ADDR}&url=$0&query=%{QUERY_STRING} [L]
and my echo.php script echoes expected URL.
But strangely when I change order of parameters in substitution to:
RewriteRule .+ echo.php?url=$0&ip=%{REMOTE_ADDR}&query=%{QUERY_STRING} [L]
echoed url is "echo.php" itself.
Is this expected behavior, and if so why?
When I test, I don't get the different behaviour you are stating. When I use the rule either way round, I get echo.php as the URL, when visiting example?DEBUG. And this is because rules in .htaccess will keep looping (because the whole directory processing starts again after the rewrite, including the rewrite rules) until the URL does not change. So since you are matching .+ you will always end up with echo.php unless some prior rule gets in the way and ends processing of that iteration before reaching your rule.
You can prove this to yourself by adding the [END] flag to the rule which stops any further mod_rewrite processing, and then you will get the behaviour you are expecting. The difference you are seeing must be due to your prior rules in some way.
As mentioned in the comments, a better way to debug mod_rewrite is to make use of the LogLevel rewrite:traceX option, with X being a number between 1 and 8. 3 is a good place to start, and increase from there if not enough info. There is a lot of info as you increase towards 8. You can only enable it in the main config. See the documentation. Make sure to switch it off again as all that logging affects performance. It does not interfere with any earlier LogLevel directives.

IIS7.5 URL Rewrite, with or without trailing slash

I'm using IIS7.5 and have a rule for rewriting a URL...
The rule is:
^trade/new_products/page([^/]+)/?.*$
But, this is only working when I don't include a slash - e.g.
http://www.website.com/trade/new_products/page1
I'd like it to also work with
http://www.toppstiles.co.uk/trade/new_products/page1/
Can I do this or do I need a separate rule?
This group ([^/]+) is catching everything but slash. If your page is only numeric you can try this one:
^trade/new_products/page([0-9]+)/?.*$
If not, you can use the non greedy modifier on the multiplier(+?):
^trade/new_products/page([^/]+?)/?.*$
Sorry - the rule was working as expected - it was another issue that was causing my problem!

Apache htaccess rule priority

I know .htaccess rules are parsed top to bottom but what if my URL matches two rules which one will be used and why?
I have simple rules like
^(.*)$ index.php?pag=cms&title=$1
^store/(.*)$ index.php?pag=store&id=$1
Basically any URL will match the first rule so what happens with other ones?
If the URL matches two rules, it's the first one that rewrites. This is not to say that the second rule doesn't fire. It does but it fails to match because subsequent rules fire on the output of the rule preceding it.
If somehow you don't want the rewrite to fall-through and stop at the first matching rule you can mark the rule as last by using the [L] flag.
^(.*)$ index.php?pag=cms&title=$1 [L]
^store/(.*)$ index.php?pag=store&id=$1 # won't fire now

Creating a simple rule to rewrite a friendly URL to the physical URL

I cannot wrap my head around URL rewriting. What I want to do seems very simple but I am having problems getting the results I want.
I would like allow users to type www.mysite.com/search/real with an optional / at the end. This would take them to www.mysite.com/content/search_real_property.asp
That's it. Here is the rule I have right now. The problem with this is it will keep stacking.
RewriteRule ^(search) content/search_real_property.asp
So this would work /search/real but so would search/real/search/real/search/real/
and others.
Assuming there are no other issues, you've turned the rewrite engine on (RewriteEngine On) and that you're either adding the rewrite in httpd-vhosts.conf or an .htaccess file in the root of the web tree (so that any path issues are resolved)... then the issue is merely one of Regular Expression pattern matching. Though I'm a bit perplexed by ASP running on what appears to be an Apache server (assuming this IS mod rewrite we're talking about).
So, all you really want is to terminate the match - something like:
RewriteEngine On
RewriteRule ^search/real/?$ /content/search_real_property.asp
That will fix it to /search/real (with or without a trailing slash, the ? means match the preceding character 0 or 1 times) to /content/search_real_property.asp. As the $ sign denotes the line terminator (EOL effectively) there must be nothing after "real" (except perhaps that 1 forward slash).
For greater flexibility you might want to look at what you can actually do with regular expressions, for instance...
RewriteEngine On
RewriteRule ^search/([^/]*)/?$ /content/search_real_property.asp?query=$1
Which would allow you to take any string and pass it in the address bar as a variable called query (Request.QueryString('query') IIRC).
Try: http://www.regular-expressions.info/ for more info.

url-rewriting and rss feed (feedburner) issue

I am using below rule to read the URL like
URL:
http://www.example.com/blog/sampe-post-title/10004/
RULE:
RewriteRule (.*)/(.*)/([0-9]+)/$ $1/details.asp?mod_id=$3 [NS,I]
Everything was fine untill I discovered that links coming via feedburner are not working anymore. Because feedburner adds some extra parameter to URL for stats/tracking etc.
For example www.example.com/blog/sampe-post-title/10004/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed:+somesite+(my+feed)
My rewrite URL doesn't recognizes the above URL anymore. Any idea how to deal with it?
Try adding a rule for the feedburner URLs:
RewriteRule (.*)/(.*)/([0-9]+)/\?(.*)$ $1/details.asp?mod_id=$3&$4 [NS,I]
I added an extra RegEx group at the end to capture everything after the question mark and place it after mod_id. You could probably combine this with your other URL if you only wanted to have one rule for some reason, but you might as well just have two.
Try the QSA flag to get the original requested URL query automatically appended to the new one.
I managed to figure out the answer on my own: just replace the ending $ with .* That's it!
Here is the modified rule:
RewriteRule (.*)/(.*)/([0-9]+)/.* $1/details.asp?mod_id=$3 [NS,I]

Resources