Unable to properly redirect with .htaccess - mod-rewrite

I'm tring to redirect the following paths on our old site:
/apps
/apps/about
/apps/everything_else
/site
To a different subdomain using the following RewriteRule:
RewriteRule ^site/?$ http://site.newsite.com [R=301,NC,L]
but nothing happens, is anything wrong with this rule? Also, how do I add the apps to the same rule?
Will the following work:
RewriteRule ^site/?|apps(/.)?$ http://site.newsite.com [R=301,NC,L]

You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(apps|site).* [NC]
RewriteRule .* http://site.newsite.com/ [R=301,L]
Maps silently:
http://site.oldsite.com/apps or
http://site.oldsite.com/apps/anything or
http://site.oldsite.com/site or
http://site.oldsite.com/site/anything
To:
http://site.newsite.com/
Nothing is passed to the substitution URL as the OP does not mention that requirement in the question.
Strings apps and site are assumed to be fixed.
For silent mapping, replace [R=301,L] with [L]

Related

How do you remove index.php from a url using mod_write, while still allowing php to see the path with index.php in it?

For the past 3 days I have been playing around with Apache's mod_rewrite trying to get it to remove index.php from my url, while php still needs to see it in the path.
Essentially PHP needs to see this
http://example.com/index.php/Page/Param1/Param2
While the user needs to see this
http://example.com/Page/Param1/Param2
What I have right now is the following in an htaccess file
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Which was taken from another page, and is close to what I need. However this seems to be cutting off everything after the http://example.com/ part. How can I get mod_rewrite to show the user one thing and have php see something else?
This is the modified code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^index\.php)^(.+)$ /index.php/$1 [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1%2 [R=302,L]
Change R=302 to R=301 once you're satisfied that it is working fine for you.
This rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Needs to look like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /index\.php(.*)\ [NC]
RewriteRule ^ /%1 [R=301,L]
Also note that RewriteRule ^(.*)$ index.php?$1 [L,QSA] doesn't create a URI that looks like this /index.php/Page/Param1/Param2, it creates a query string that looks like this: /index.php?Page/Param1/Param2. Which isn't at all what you said PHP needs to see.

How to redirect an url with parameters?

I'm at a total loss trying to integrate a mod_rewrite in my existing page. Currently, I am building a new site from scratch, and there i have some nice clean url's such as:
http://www.example.nl/nl/example
The old site, running Cms made simple, has some not-rewritten url's that would need to be redirected to the new pages. Those url's look like this:
http://www.example.nl/index.php?page=cake-and-pie&hl=nl_NL
But shorter versions of that like:
http://www.example.nl/index.php?page=cake-and-pie
also work.
It took me a while to figure out that url's with parameters cannot simply be redirected with "Redirect 301", like i'd normaly do. So i tried some online mod_rewrite generators like this and this, but the rules outputted by those result only in 404 errors, (the redirect doesn't work at all).
My .htaccess file current looks like this:
RewriteEngine On
RewriteBase /
# remove .php;
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
The old pages would seize to exist.
How do i redirect the old pages to the new ones?
Thanks.
EDIT
RewriteCond %{QUERY_STRING} =page=pie
RewriteRule ^index\.php$ /nl/? [L,R=301]
RewriteCond %{QUERY_STRING} =page=pie&hl=nl_NL
RewriteRule ^index\.php$ /nl/? [L,R=301]
Seems to do the trick. This is of course manual for every url, but i only have a few.
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)/([^/]+)/?$ index.php?page=$1&hl=$2
RewriteRule ^([^/]+)/?$ index.php?page=$1 [QSA]

Question about url rewriting using apache mod_rewrite

I have a doubt about url rewriting using apache mod_rewrite. I am a newbie in mod_rewrite and I don't have any experience in regex.
What I want to do is to:
Rewrite / To /web/content/public/
Rewrite /clients/ To /web/content/clients/
How can I achieve above things.
I tried:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/clients/$ web/content/clients/ [L]
RewriteRule ^(.*)$ web/content/public/$1 [L]
</IfModule>
But it doesn't work. What can I do?
^(.*)$ includes the slash. So don't include the slash in the rewritten pattern.
But include a root slash at the head of your rewritten pattern.
RewriteRule ^/clients(.*)$ /web/content/clients$1 [L]
RewriteRule ^(.*)$ /web/content/public$1 [L]
Check the apache access log and error log to see what kind of request URL comes back.
Please check the documentation, especially the list labeled "Here are all possible substitution combinations and their meanings:"
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^/clients/$ web/content/clients/ [L]
RewriteRule ^/(.*)$ web/content/public/$1 [L]
And if you want to use that rules in a .htaccess file, remove the leading slash from the patterns.

Mod rewrite most requests to index.php

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
##RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.(js|ico|gif|jpg|png|css)$ [NC]
RewriteCond %{REQUEST_FILENAME} !^index.php [NC]
RewriteCond %{REQUEST_FILENAME} !^$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
## RewriteRule ^/([a-zA-Z_+-]+).php$ index.php?p=%1 [R=301]
## RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?p=$1 [R=301,L]
## RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php?b={REQUEST_FILENAME}
RewriteRule * index.php?p={REQUEST_FILENAME} [R=301,L]
Above you can see my attempts to redirect any request that is not an existing directory, is not index.php and is not a static resource to redirect to index.php?p=resourcename
I am not having any luck. Basically the purpose of this is to redirect static and old urls to new ones as I have just rewritten an old site.
The PHP will handle the redirect logic.
At the moment this code causes an internal server error, I assume because it is caught in a redirect loop. What have I done wrong? My brain is fried after a long day.
Untested, but worth a try :
RewriteRule .* /index.php?p={REQUEST_FILENAME} [R=301,L]
The ".*" part means you want to match 1 or more characters (any of them). And the "/" in front of the "index.php" is probably not mandatory but makes things clearer even if you have the RewriteBase option set to "/" already.
You may also want to add the parameter "QS" between the brackets, to be sure to get the querystring that may be passed with the queries (would be [QS,R=301,L]). Hope this works, and this helps :)
Edit: There's also the "%" in front of "{REQUEST_FILENAME}", as stated by Gumbo.
This code eventually solved my problem.
Thanks for the help, though.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?([-a-zA-Z0-9_+]+).php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?p=$1 [L]
You forgot the % in front of the variable (%{REQUEST_FILENAME}) and an expression that should be repeated zero or more times (* is just a quantifier):
RewriteRule .* index.php?p=%{REQUEST_FILENAME} [R=301,L]

How do I write a custom mod_rewrite .htaccess file for CakePHP routing?

I've rewritten my web app using CakePHP, but now I need to have my old formatted urls redirect to my new url format. I can't seem to add my own custom mod rewrite rule. I've added it above the main cakephp rewrite rule, but I'm getting an infinite redirect loop. I just want http://mysite.com/index.php?action=showstream&nickname=user to redirect to http://mysite.com/user before the cakephp rewrite happens.
EDIT: Ok, so now when the condition is met it's redirecting but it's appending the original query string to the end. I'm assuming that's due to the QSA flag in CakePHP rewrite rules, but I was under the impression the "L" in my rule would stop that from executing...
RewriteEngine On
RewriteCond %{QUERY_STRING} ^action\=showstream&nickname\=(.*)$
RewriteRule ^.*$ http://mysite.com/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
When you do a capture inside the RewriteCond line instead of the RewriteRule, you have to reference the capture with %N instead of $N. That is, your RewriteRule line should be:
RewriteRule ^index.php$ /%1 [R=301,L]
Try to test the request line (THE_REQUEST) to see what URI originally has been requested:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php
RewriteCond %{QUERY_STRING} ^action=showstream&nickname=([^&]*)$
RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]
But maybe it would be easier to do this with PHP.

Resources