Apache RewriteRule to subdirectory - mod-rewrite

I need to redirect myhomepage.com/ to myhomepage.com/sub/
When I read the guide at apache.org/docs/1.3/misc/rewriteguide.html I have no clue what they are talking about. Hence I decided to friendly ask one of the experts here. I guess it takes just some seconds to figure that rule out.
Thanks, Carin.

Try this rule:
RewriteRule !^sub/ sub%{REQUEST_URI}
It will redirect any requested URL path that does not start with /sub/ (!^sub/) internally to /sub/ (sub%{REQUEST_URI}).
This rule is for the .htaccess configuration file in your document root. If you want to use it in your httpd.conf, prepend the pattern with a /.
And if you want an external redirect, prepend the substition with a / too and add the [R] flag:
RewriteRule !^sub/ sub%{REQUEST_URI} [R]

If you only want to redirect http://myhomepage.com/ (i.e. you don't need to redirect http://myhomepage.com/example.html to http://myhomepage.com/sub/example.html), the rule is as simple as:
RewriteRule ^$ http://myhomepage.com/sub/ [R=301,L]

Related

Redirect not working for matching a pattern in my url

I am trying to redirect a pattern of urls
http://style.com/style-blog/entry/what-im-looking-for-in-my-next-15-inch-laptop
to
http://style.com/blog/entry/what-im-looking-for-in-my-next-15-inch-laptop
I have tried to match the "style-blog" here
^style-blog/([A-Za-z0-9-]+)$ or ^style-blog/$
I needed to the first version to get ANY remaining part of the url to append to the new url here
RewriteRule ^style-blog/([A-Za-z0-9-]+)$ http://style.com/you-blog/$1 [NC,L]
Thanks for pointers on what I am doing wrong.
I think you may just be missing a /:
RewriteRule ^/style-(blog/[A-Za-z0-9-]+)$ http://style-review.com/$2
Using wildcard:
RewriteRule ^/style-(blog/.*)$ http://style-review.com/$2
Do you even need the full URL?
RewriteRule ^/style-(blog/.*)$ /$2
Are the ^ and $ required? Wouldn't just this work?
RewriteRule style-blog/ blog/
RewriteRule ^style-blog/(.*)/(.*)$ http://%{HTTP_HOST}/you-blog/$1/$2 [R=301,L]
This appears to be working but thanks to you guys you got me on the right track and probably you didn't have enough information to solve it. Its not fully tested yet and might not cope with any extra 'folders' (bits between slashes)

replace character in rewrite rule

Here are my current rules:
RewriteCond %{QUERY_STRING} ^to=(one|seventeen|thirty\+four)
RewriteRule ^/folder/page.php$ http://www.site.com/folder/category/%1? [L]
RewriteRule ^folder/category/(.+)\+(.+)$ http://www.site.com/folder/category/$1-$2 [L]
The first rule works fine, it redirects perfectly if the word is in the query string, but I can't get thirty+four to become thirty-four when redirected.
Any help would be greatly appreciated.
For starters, RewriteRule ^/folder/page.php$ will never match anything. The URI's get the prefix (the leading slash) removed if the rules are in an .htaccess file instead of server config.
Secondly, since you've included http://www.site.com/ in your targets, that means the browser will get redirected instead of internally rewritten. You need to remove http://www.site.com/ from your first rule so that the second one can be applied.
Here's what should work:
RewriteCond %{QUERY_STRING} ^to=(one|seventeen|thirty\+four)
RewriteRule ^folder/page.php$ folder/category/%1 [NC,QSA,L]
RewriteRule ^folder/category/(.+)\+(.+)$ folder/category/$1-$2 [NC,QSA,L]
And now three hints:
1)
Please make sure you've read everything here before asking:
Here's the wiki of serverfault.com
The howto's htaccess official guide
The official mod_rewrite guide
2)
Please try to use the RewriteLog directive: it helps you to track down problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
3)
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
You use this tool when you want to check the URL and see if they're valid or not.

Need help removing part of filename from url with mod_rewrite

I'm trying to reformat my url to be a bit shorter. Right now the links end up as this: website.com/image?id=name.jpg
What I want to have the link come out as is m.website.com/name, without the file exension or image.php file in the url. I figure mod_rewrite is the way to do it, so any help will be greatly appreciated.
In order to make it so someone accessing the URL http://m.website.com/name gets served the content for http://website.com/image?id=name.jpg, you first need to check the hostname for m.website.com, then match the name part of the URI. Using that match, you can proxy the request (using a [P]) or, if both website.com and m.website.com are hosted on the same server, just simply internally rewrite. Try putting this in your .htaccess file in your document root:
RewriteEngine on
# check the host (NC = no case)
RewriteCond %{HTTP_HOST} ^m\.website\.com$ [NC]
# don't rewrite /image
RewriteCond %{REQUEST_URI} !^/image
# Match the first non-slash word and rewrite
RewriteRule ^([^/]+)$ /image?id=$1 [L]
This will rewrite http://m.website.com/name to /image?id=name.jpg, but it will not rewrite http://m.website.com/path/name. If you want paths (and everything else) to be included in the id parameter, change the ([^/]+) to (.*) in the RewriteRule.

Problem redirect CodeIgniter URLs to WWW

I have a CI application that uses .htaccess for URL routing. My basic setup is as follow:
RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]
These rules are pretty standard for CI apps. They rewrite all URLs (except for those in the exception list) to the index.php front controller. The lines above also hide index.php, as it would normally appear as part of every URL.
So far, so good. Everything works just fine. Now, for the sake of SEO I would like to force all traffic to www. So I extended the rules as follow:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]
rewritecond %{http_host} ^jungledragon.com [nc]
rewriterule ^(.*)$ http://www.jungledragon.com/$1 [r=301,nc]
These last two lines rewrite http://jungledragon.com/anything URLs to http://www.jungledragon.com/anything URLs. This kind of works, but it brings back the index.php part back: http://jungledragon.com/anything becomes http://www.jungledragon.com/index.php/anything.
How exactly do I combine these rules so that they do not interfere with each other? I tried doing the WWW rewrite before the CI rules. That shows an Apache 301 page with an error, rather than doing the actual redirect.
Additionally, I would like to also include rules to get rid of trailing slashes, but for now let's keep the question simple. Note that I did find useful post here and elsewhere yet for some reason I still can't find the correct exact syntax for my situation.
Edit: Thanks for the help. This works:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^jungledragon.com [nc]
rewriterule ^(.*)$ http://www.jungledragon.com/$1 [r=301,nc,L]
RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]
mod_rewrite processes rules in a linear fashion. Rules at the top of the file are processed first.
The [nc] and [L] at the end of the rules are the options for how to process rules.
nc - nocase: case insensative
L - last: last rule in the execution (if you hit this, stop processing)
You need to put your www redirect rules above your CI rules so it will first add the www, THEN apply the CI rules to the newly re-written url. **And also use either the C or N flag with your www redirect rule so it will parse the next rule.
http://mysite.com/blah ==becomes==> http://www.mysite.com/blah ==becomes==> http://www.mysite.com/index.php/blah (Executed, not redirected)
What's happening currently is:
http://mysite.com/blah ==becomes==> http://mysite.com/index.php/blah (STOP)
Browser goes to http://mysite.com/index.php/blah and a second re-write pass is done since your exceptions stop /index.php urls from being processed
http://mysite.com/index.php/blah ==becomes==> http://www.mysite.com/index.php/blah (Redirected)
As Suggested, here is a link to mod_rewrite's documentation if you want to look further.
#LazyOne: Brainfart, sorry.
Here's an excerpt from the docs outlining the flags you'll probably need:
'chain|C' (chained with next rule)
This flag chains the current rule with the next rule (which itself can be chained with the following rule, and so on). This has the following effect: if a rule matches, then processing continues as usual - the flag has no effect. If the rule does not match, then all following chained rules are skipped. For instance, it can be used to remove the .www'' part, inside a per-directory rule set, when you let an external redirect happen (where the.www'' part should not occur!).
'next|N' (next round)
Re-run the rewriting process (starting again with the first rewriting rule). This time, the URL to match is no longer the original URL, but rather the URL returned by the last rewriting rule. This corresponds to the Perl next command or the continue command in C. Use this flag to restart the rewriting process - to immediately go to the top of the loop.
Be careful not to create an infinite loop!
'nocase|NC' (no case)
This makes the Pattern case-insensitive, ignoring difference between 'A-Z' and 'a-z' when Pattern is matched against the current URL.
'noescape|NE' (no URI escaping of output)
This flag prevents mod_rewrite from applying the usual URI escaping rules to the result of a rewrite. Ordinarily, special characters (such as '%', '$', ';', and so on) will be escaped into their hexcode equivalents ('%25', '%24', and '%3B', respectively); this flag prevents this from happening. This allows percent symbols to appear in the output, as in

What's wrong with my RewriteRule?

My .htaccess file currently looks like this
AddType x-mapp-php5 .php
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^account$ /account/orders.php [L]
When I go to http://mywebsite.com/account it properly shows the page at http://mywebsite.com/account/orders.php. But when I change the RewriteRule to
RewriteRule ^account/orders$ /account/orders.php [L]
and then I go to http://mywebsite.com/account/orders, I get Error 404 Page Not Found. What did I do wrong?
******Additional Details**
I finally diagnosed the problem. But I don't understand why my solution works. Consider the scenario where account/orders.php exists.
The following rule will not work
RewriteRule ^account/orders$ account/orders.php [L]
The following rule will work
RewriteRule ^account/order$ account/orders.php [L]
Ie., the rewrite rule will fail if the Pattern evaluates to an existing file. So when the pattern is the same as the substitution, but minus the extension, the rewrite rule will fail. If I add a file called account/order.php, then both rules will fail.
Why does this happen?
I don't see how your first example would work, because I believe that intial slashes are also passed on.
RewriteRule ^/account/orders$ /account/orders.php [L]
Have you tried a relative path?
RewriteRule ^account/orders$ account/orders.php [L]
Edit   You should also make sure to have MultiViews disabled. This causes that Apache does some additional vague file matching to find similar named files and thus /account/orders would be mapped to /account/orders.php before it’s passed to mod_rewrite.
Strange, it seems ok to me...
If you have access to Apache Configuration try to enable RewriteLog and RewriteLogLevel for some debug...
Also take a look in the site's log files (always if you have access)
I would at first try to add a redirect to my rules, so I can see in the browser what is happening on the server.
RewriteRule ^account$ /account/orders.php [L,R]
Also make sure that there are no other rules (previous ones) interfering, just in case you are not showing all of your .htaccess file here.
I'm answering my own question because I finally diagnosed the problem. But I don't understand why my solution works. Consider the scenario where account/orders.php exists.
The following rule will not work
RewriteRule ^account/orders$ account/orders.php [L]
The following rule will work
RewriteRule ^account/order$ account/orders.php [L]
Ie., the rewrite rule will fail if the Pattern evaluates to an existing file. So when the pattern is the same as the substitution, but minus the extension, the rewrite rule will fail. If I add a file called account/order.php, then both rules will fail.
Why does this happen?

Resources