Apache Redirect does not work… - apache

Once in a while I have to fuddle around with mod_rewrite or rather Apache's redirect. Now I have a simple task, that drives me nuts, cause I can't get it working:
Everything that was under /journal/(.*) is now under /blog/(.*).
This is what I have now:
RewriteEngine on
RewriteBase /
RewriteRule ^/journal/([^/]+)$ /blog/$1
If it matters: this is a drupal installation.

Drupal has existing rewrite rules in .htaccess. If you put your rules at the top of the file, Drupal's rules may override them.
I don't think you want the starting / in your rule.
If you're expecting the rule to redirect folks who use the old /blog/ URL, you're mistaken. If that's what you're trying to do, you'll need to perform a redirection, like this:
.
RewriteEngine on
RewriteBase /
RewriteRule ^journal/(.+)$ http://example.com/blog/$1 [R=301,L]

since your rewrite base is / I think you can leave out the / in front of journal. so try
RewriteRule ^journal/([^/]+)$ /blog/$1
Also if you have any other rules happening you can append [L] to the end of the statement to make sure no other rewrites happen.

Related

Apache htaccess rewrite (Pretty URLs)

I have a sneaking suspicion this is not possible, but figured I would ask regardless.
Is it at all possible to take a URL passed to a server in the form of:
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
And rewrite it to appear as:
http://domain.com/Controller/Action/1/2/3
I am trying to clean up an borderline ancient project to support "Pretty URLs" and I would really like to make the URLs display a bit nicer. I know I could setup a 301 header redirect to the new URL, but I would prefer to avoid that overhead if at all possible.
Any thoughts?
Thanks!
To get
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
To appear as
http://domain.com/Controller/Action/1/2/3
You will need to use %{QUERY_STRING} to capture the query string data. Your .htaccess file will look like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Action=Controller/Action&one=(\d+)&two=(\d+)&three=(\d+)
RewriteRule ^.+ /Controller/Action/%1/%2/%3 [R=301,L]
This will set up a permanent redirect to the new page. You can play around and test .htaccess rewrite rules here: htaccess.madewithlove.be
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?Action=$1/$2&one=$3&two=$4&three=$5 [L,QSA]

Using mod_rewrite correctly on Debian server

the good old mod_rewrite. I can't seem to get it right.
Typical scenario: A user types in "http://domain.com/page"
I want that the user is being redirected to "http://domain.com/page/page2"
My htaccess file looks as follows:
RewriteEngine on
RewriteBase /var/www/
RewriteRule ^/page/$ page/page2
RewriteRule ^/bla/$ page/page2/bla
The first rewrite rule works, the second on the other hand doesn't seem to have any effect. Any idea? Maybe a better way to do this?
And another question:
As I said the first rewrite works just fine, but the url is not pretty. "http://domain.com/page" changes to "http://domain.com/page/page2". Is there a way to keep the typed in url but still forward the user to the actual link?
I presume the .htaccess is in your DocumentRoot.
How does your /bla containing look like? This should not rewrite the URL in the browser.
Use this:
RewriteEngine on
RewriteBase /
RewriteRule ^(/?)page/?$ $1page/page2 [L]
RewriteRule ^(/?)bla/?$ $1page/page2/bla [L]

Mod_Rewrite assistance for a noob please

I want to rewrite all URLs of the form:
http://www.site.com/push20/dir1/dir2/afile.html
to
http://www.site.com/dir1/dir2/afile.html
I.e. lose the push20 bit
I've tried with:
RewriteEngine On
RewriteRule ^push20/(.*) /$1 [R]
but its not happening.
Any suggestion?
Won't you need a leading / before push20?
i.e:
RewriteRule ^/push20/(.*) /$1 [R]
Unless I'm missing something, you can do this with a standard Redirect directive, something along these lines:
Redirect /push20 /
That should redirect everything under /push20 to the root of your site -- basically it strips out push20 and redirects.

Rewrite rules doesn't work apache 1.3

I'm using a couple of rewrite directives that always works before on apache2 but now trying a new shared hosting and the rewrite rules do not seem to get applied.
I've reduced the .htaccess files to the following essential rules:
RewriteEngine On
Rewritebase /demo/
RewriteRule ^(.*)$ index.php/$1 [L]
As you can see, i want to rewrite every request to my index.php file in the demo folder from root.
So everything like http://www.example.com/demo/albums/show/1 should be processed by http://www.example.com/demo/index.php for a standard MVC setup. (I'm using CodeIgniter btw)
The directives above results in a 500 error, so i thought maybe because of some possible syntax differences between 1.3 and 2.x.
After some trail and error editing, i've found the rewrite rule itself to be at fault but i really don't understand why.
Any ideas to why my rewrite rule doesn't work? it did before on lots of different servers.
Suggestions how to fix it?
Note: mod_rewrite does work, i've written a small test to be sure.
In your position, I'd probably look in the Apache error log first, then would try to eliminate one moving part by doing
RewriteEngine On
RewriteRule ^/demo/index.php$ /demo/index.php [L]
RewriteRule ^/demo/(.*)$ /demo/index.php/$1 [L]
If that worked, I'd try reintroducing RewriteBase.
Did you set:
Options +FollowSymLinks
… before the rewrite rules? If FollowSymLinks is disabled mod_rewrite won’t work.
It may be that you’re running into an infinite recursion since index.php/… is also matched by ^(.*)$. So try to exclude your target:
RewriteCond $1 !^index\.php/
RewriteRule ^(.*)$ index.php/$1 [L]

Trouble with advanced .htaccess redirect

I'm migrating a custom coded blog over to Wordpress, and have to set up a redirect that will handle all of the blog posts.
I need to redirect from this:
/oldblogdirectory/Old_Blog_Posts_Looked_Like_This.htm
to:
/newblogdirectory/new-blog-posts-look-like-this/
Any ideas on the regex for a redirect like this?
Gumbo's approach is certainly the way to do it. I made two test directories:
oldblogdir/archives/blog_posts_look_like_this.htm
newblogdir/archives/blog-posts-look-like-this
And the following RewriteRules redirect successfully. They are only slightly changed to Gumbo's proposal:
RewriteEngine on
RewriteBase /
RewriteRule ^(oldblogdir/archives/[^_]*)_(.*) $1-$2 [N]
RewriteRule ^oldblogdir/archives/(.*?)\.htm$ newblogdir/archives/$1 [R,NC,L]
Note that the [N] causes the .htaccess file to be re-evaluated until the RegEx no longer matches. Therefore you should put it at the very top of the file.
Try this mod_rewrite rules:
RewriteEngine on
RewriteRule ^(oldblogdirectory/[^_]*)_([^_]*)_(.*) /$1-$2-$3 [N]
RewriteRule ^(oldblogdirectory/[^_]*)_(.*) /$1-$2
RewriteRule ^oldblogdirectory/(.+)\.htm$ /newdirectory/$1/ [L,R=301]
But for the uppercase to lowercase conversion you’ll either need a mapping like the internal tolower function or you use PHP for both.