modrewrite RewriteRule problem - apache

I am using Apache 2.2 and mod_rewrite. I would like to take the following urls and map them to another url. The pattern is simple and like this:
http://domain/test/ex1.html -> http://domain/app/index.php/content/?name=ex1
I tried the following in an .htaccess file (place in /test/ directory in docroot):
RewriteEngine On
RewriteBase /app/
RewriteRule (.*)\.html index.php/content/?name=$1
and
RewriteEngine On
RewriteRule (.*)\.html /app/index.php/content/?name=$1
I wasn't sure if the backreference was correct so set to $0 and $2 but never seemed to help.
I also tried setting the RewriteLogLevel to 9.
There is a step where it is almost there:
rewrite 'ex1.html' -> 'index.php/content/?name=ex1'
The last line of the rewrite log is as follows:
[perdir /var/www/domain/htdocs/test/] internal redirect with /app/index.php/content/ [INTERNAL REDIRECT]
How can I get this to rewrite to /app/index.php/content/?name=ex1 ?
EDIT
so using this as an .htacces in the docroot file works for the RedirectMatch:
RewriteEngine On
RewriteBase /
RedirectMatch ^(.*)/(.*)\.html$ http://domain/app/index.php/content/?name=$2
#this doesnt work - adding to docroot allows for it to pick up but still using test/ex1 rather than just ex1
#RewriteRule ^(.*)\.html$ /app/index.php/content/?name=$1
Any help getting the bottom RewriteRule (that is currently commented out) to work would be appreciated.
thanks

In your example you mentioned domain/test/ so I'm going based of rewriting from /test/*.html to /app/index.php/content/?name=
RewriteEngine On
RewriteBase /
RewriteRule ^test/(.*)\.html$ /app/index.php/content/?name=$1
That should work.

Uhm, off the top of my head:
RewriteCond %{REQUEST_URI} ^/app/(.*)\.html$
RewriteRule ^/app/(.*)\.html$ /app/index.php/content/?name=$1
This assumes that the rewrite base is /

Related

Simple modrewrite to remove particular part of path

I'm trying to create a modrewrite rule that will change:
/blah/correct/xyz.htm
to
/correct/xyz.htm
There is not always /blah but when it's there, it always appears at the beginning of the URL. The URL can be any length, with numerous sub-paths. It could even be /blah/myfile.htm (which should just rewrite to /myfile.htm).
What's the best way to do this?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule (?:^|/)blah(/.+)$ /$1 [L,NC]
This will internally forward /blah/foo to /foo or /blah/correct/foo to /correct/foo. If you want external rewrite then use:
RewriteRule (?:^|/)blah(/.+)$ /$1 [L,R=301,NC]

.htaccess redirection from multiple directories to a single page

I have a single blog.php page that should get redirected from all these requests:
www.site.com/blog #goes to blog.php
www.site.com/blog/id-of-an-entry #goes to blog.php?id=id-of-an-entry
Also internationalised versions such for french:
www.site.com/fr/blog *# to blog.php?lang=fr
www.site.com/fr/blog/id-of-entry* #to blog.php?lang=fr&id=id-of-entry
What would be the more eficient and effective cond/rules for .htaccess? I've made many attemps but end walking in circles or with to many specialised rules :-) Thanks for any insights!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^blog/([^/]+)/?$ /blog.php?id=$1 [L,NC,QSA]
RewriteRule ^blog/?$ /blog.php [L,NC,QSA]
RewriteRule ^([^/]+)/blog/([^/]+)/?$ /blog.php?lang=$1&id=$2 [L,NC,QSA]
RewriteRule ^([^/]+)/blog/?$ /blog.php?lang=$1 [L,NC,QSA]
I am not using this regulary so there can be better way but this could help you. Rules works like this. In () are your variables which corresponds to what you want to match. Than you can refer to them by $1 and so on. For example news/local would redirect them to file news-local.php you can use the same system to match you get variables.
RewriteRule ^([a-z]+)/([a-z]+)$ $1-$2.php

Issue with specific 301 redirection in htaccess

I'm having an issue with a format of URL.
I need to send
/en
to /
The problem is that I need only /en (exactly), not /en/foo...
I know how to do it with string that ends with .html, but here I have other URLS that have /en/stuff and they are also matched.
Would really appreciate assistance...
You need to specify end of string in RewriteCond e.g.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/en$
RewriteRule ^(.*)$ / [L,R=301]
Please note that Condition pattern as well as Rewrite Rule pattern are a perl compatible regular expression (with some additions):
For Anchors:
^ - Start-of-line anchor
$ - End-of-line anchor
EDIT
BTW, there is very nice htaccess tester at http://htaccess.madewithlove.be/ which you could use to test the rules.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^en/?$ / [L,R=301,NC]
This will redirect /en or /en/ to / also note that /EN will also be handled because of NC (Ignore Case) flag used here.

What is the modrewrite rule to flatten access to awstats

My current url to access my awstats is
http://my.server.com/awstats/awstats.pl?config=foo
I would like to use apache and mod_rewrite to
http://my.server.com/foo
My attempt is
RewriteRule ^(.*) /awstats/awstats.pl?config=$1 [NC]
but I just get a 404.
The error.log doesn't give me much help.
Try adding a $ after so that the entire string is eaten by the regexp, and then use [L] so the rewrite engine knows to stop processing and apply the rule.
Remember to switch on the rewrite engine and set the rewrite base.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /awstats/awstats.pl?config=$1 [NC,L]

Rewriting a number based URL using .htaccess RewriteRule

How can I rewrite a simple number based URL to a sub folder?
I want http://mysite.com/123 to redirect to http://mysite.com/en/123.
The number could be anything from 1 - 9999. My attempt in htaccess was:
RewriteRule ^([0-9]+)$ /en/$1/ [R]
But that doesn't work.
your syntax is right, I just remove slash in the end of line and it works:
RewriteRule ^([0-9]+)$ /en/$1
Make sure that this 3 lines are writen in your HtAccess File (sorry for mybad english)
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
##
## Now the rewrite
RewriteRule ^([0-9]+)$ ./en/$1