RewriteRule doesn't do anything - mod-rewrite

I am using the current set of re-write rules in my .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule lessons/(.*)$ page.php?url=$1
It works fine. Now I have some old pages which I would like to redirect to new pages. I do the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule lessons/(.*)$ page.php?url=$1
RewriteRule old/this_url/(.*)$ lessons/to_this_url/and_this_url
But when I go to www.mywebsite.com/old/this_url/another_path it simply doesn't do anything?
I tested the rule in a re-write test program and it worked. The rule was recognised. So not sure, what I am doing wrong on the live website?

Answering my own question. In fact I realized a bit late that what I was trying to do was redirecting the content of an old website to a new website. They live under the same domain name but don't live under the same root. One lives under / (the current one) and the old one lives under /old/. So the rules needed to be added to the .htaccess of the old website. In:
www.root.com/old/.htaccess (THIS IS CORRECT)
While I was trying to edit them in:
www.root.com/.htaccess (NO)
That solves my problem.

Related

.htaccess MAMP difference to environment

I'm building my site on MAMP, after a couple of days I finally got an .htaccess file to work after my will. But now when I upload to live environment it doesn't behave the same! Is there different versions? Is it because of error in my file that MAMP handles automatic?
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 404.php [QSA,L]
Check if mod_rewrite is enabled in your live environment.

ExpressionEngine: mod_rewrite directory to subdomain, while removing /index.php/

In ExpressionEngine, what’s the best way to mod_rewrite a directory to a subdomain, while keeping index.php out of the picture?
For example:
http://www.domain.com/index.php/group/template -> group.domain.com/template
I’ve seen variations that take ANY group and rewrite them to subdomains, but I only need one.
I’ve been tasked with porting over a subsite from a different server (that was also running EE). Normally, I’d just redirect group.domain.com to domain.com/group (index.php removal was already working), but that’s been deemed an unacceptable solution. And of course, this is time-sensitive.
I’ve been diving into Google and the EE docs/wiki for going on twelve hours and I’m starting to go cross-eyed. Can anyone give me a hand?
Thanks in advance.
Here's how I would craft your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(group|group/.*)$
RewriteRule ^(.*)$ http://group.domain.com/template/$1 [L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
This example uses the "File and Directory Check" Method of removing index.php from the URL and uses a RewriteCond Directive to instruct Apache to handle the requests for the "group" directory and all its sub-directories differently.
Any links to domain.com/group/template will be redirected to group.domain.com/template/.
If you care about letting crawlers know your content has moved and want to make the transition as seamless as possible, you can add a 301 Redirect to your RewriteRule:
RewriteCond %{REQUEST_URI} ^/(group|group/.*)$
RewriteRule ^(.*)$ http://group.domain.com/template/$1 [R=301,L]
This will ensure that users and search engines are directed to the correct page.

How to do this via mod_rewrite

http://mydomain.com/bubba
goes to
http://mydomain.com/myscript.php?name=bubba
But does not match anything with an extension on it (.php, .html, etc).
I've been working on this for the last several hours and I cannot see how to do it. Every piece of documentation I find specifically doesn't work.
I'm doing this on a shared host (1and1.com) with .htaccess
Rewrites every non-existant file to myscript.php?name=requested:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ myscript.php?name=$1 [QSA,L]
Maybe you want RewriteCond -f, to check whether the file exists.
To only match things without a period:
^([^.]+)$ myscript.php?name=$1 [QSA,L]

Rewrite URLs for static content

I have problems with static files url rewriting in current .htaccess setup on apache2.
My app structure is:
/siteroot
/siteroot/app
/siteroot/lib
/siteroot/...
/siteroot/public <- all the static files (images, js, etc.) stored here
/siteroot/index.php
/siteroot/.htaccess
So, i need to rewrite url like /css/style.css to /public/css/style.css. I did that in really simple way, but when the file is not found it causing 10 internal redirects, which is bad. I need somehow to return 404 code if file not found, or just pass it to the next rule. And i dont have any access to site configuration file. Only .htaccess.
The reason why i`m asking this question is that the site was running on nginx and i need to rebuild the same configuration on apache.
Here is my .htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Test if a redirect is reasonable:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/public/$0 -f
RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]
If the number of prefixes is limited, you could add another group to your regex:
RewriteRule ^(css|js|images|etc)/.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]

xampp redirects

Does anyone has an example how to do a rule in .httpaccess in xampp?
I'm trying to redirect from localhost/test/company.php?name=Abc to localhost/test/company/Abc and I cant seem to find the solution. I followed some examples that I found on the web but none seems to work. I'm putting the .htacces file in the same folder where I have the company.php file. And I have the urlrewrite turned on.
Put the .htaccess file in your application root.
The .htaccess file should have something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^company/([a-zA-Z0-9\-]+)$ /company.php?name=$1 [L,NC]
Turn on mod_rewrite and restart apache.
Then it should work.

Resources