Yet another .htaccess question - .htaccess

I have a .htaccess file on a WAMP server. The .htaccess Rewrites work fine...for some easy url rewrites, but I can't get it working on a simple test.
My .htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^Homepage/$ index.php
RewriteRule ^Test/(.*)/$ test.php?a=$1
On my test.php I'm dumping out $_GET.
My Homepage works fine, i.e. http://www.mysite.com/Homepage/ is redirecting just fine. But my Test page is only partially working in that it goes to the test.php but nothing is getting dumped from my $_GET dump - it's just an empty array.
Ideally I need to do this for multiple folders, e.g.
RewriteRule ^Test/(.*)/(.*)/(.*)/$ test.php?a=$1&b=$2&c=$3
...but I can't even get my simple test working.
BTW - this same .htaccess works perfectly in my Linux environment (dev server). I now need to move this to a production Windows box and it's simply not working.
If it helps this .htaccess file is sitting in a subfolder e.g. C:\wamp\www\mywebsite\.htaccess
What am I doing wrong? Any ideas? Thanks in advance.
UPDATE:
Turned on Rewrite Log (substitude real website name with mywebsite) and http://www.mywebsite.com/Test/ABC/ returns this:
217.42.222.22 - - [31/Mar/2011:23:08:16 +0100] [www.mywebsite.com/sid#65f3f0][rid#1a13a68/subreq] (1) [perdir C:/wamp/www/mywebsite.com/] pass through C:/wamp/www/mywebsite.com/test.php
217.42.222.22 - - [31/Mar/2011:23:08:16 +0100] [www.mywebsite.com/sid#65f3f0][rid#1a2ea88/initial] (1) [perdir C:/wamp/www/mywebsite.com/] pass through C:/wamp/www/mywebsite.com/test.php
217.42.222.22 - - [31/Mar/2011:23:08:16 +0100] [www.mywebsite.com/sid#65f3f0][rid#1a24af0/subreq] (1) [perdir C:/wamp/www/mywebsite.com/] pass through C:/wamp/www/mywebsite.com/ABC
I'm still none the wiser ;-)

Two suggestions:
post the url that you're using for the Test page.
turn on the rewrite log and see what it says. Just be sure to turn it off again later!

Have your rewrite rule like this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^Homepage/$ /index.php [R=301,L]
RewriteRule ^Test/(.*)/$ /test.php?a=$1 [R=301,L]
target of rewrite rules should start with a /

Related

.htaccess on a subdomain

long story short:
This is my (sub-) domain and file:
https://sub.domain.com/test.php
And I would love to have it like:
https://sub.domain.com/test
My .htaccess file looks like this:
RewriteEngine On
RewriteBase /
RewriteRule ^test/?$ test.php [NC,L]
But I always get a 404 not found.
The strange part: When I change ^test/?$ to ^test/test/?$ and try to get to https://sub.domain.com/test/test.php everything works fine.
I have no idea what to do, I searched like a maniac -- maybe someone has a solution?
(btw the subdomain points to a different directory than domain.com; so there are no nested folders like sub.domain.com -> domain.com/sub)
Thanks :)
Edit: Server is running Apache/2.4.25 (Debian)

Rewrite rules for localhost AND live environment

I want to add rewriterules that works in local environment (localhost) and on my liveserver.
Why?
I don't want to change rules when I test my project locally and upload it to the liveserver.
Adding Rules
Here an example
(ANY-URL)index.php?page=somepage
change to
(ANY-URL)/somepage
So I used a rewritemod generator and pasted this into it:
index.php?page=somepage
The rewriterule I got, looks like this: (of course my .htacces starts with RewriteEngine On)
RewriteRule ^([^/]*)$ /?page=$1 [L]
When I try to get to (http:)
//localhost/myproject/development/index.php?page=login it sends me to the root directory of my local development envirment. But the URL in the adressline doesn't change.
Testing
Of course I tried some other Rules by pasting the whole URL into the generator just to test if the rewrite thing works.
Also here the URL doesn't change to short-url but the server cant find stylesheets and javascripts anymore. I got redirected to the index.php
Possible solutions?
Maybe it has something todo with that "RewriteBase"?
Do i have to set a basepath?
My .htacces is located here:
//localhost/myproject/development/.htaccess
Later I also want to change paths that look like this:
(ANY-URL)index.php?page=somepage&second=hello&third=world&fourth=cool
Also here a I'm looking for a solution that works on both environments.
Since the htaccess is located inside a sub-directory, use RewriteBase:
RewriteEngine On
RewriteBase /myproject/development/
Once that is done, you use the base element in your php/html files. This will resolve the addresses for the scripts/stylesheets in your pages.
<base href="/myproject/development/" />
Now, the rewrites in htaccess would be:
RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page=$1 [L]

Htaccess - Detecting the URL

For my family members I was giving each person their own subdomain
(sister1.mydomain.com, sister2.mydomain.com, etc...)
I was using PHP to detect the domain, and then I'd load information related to the subdomain dynamically.
I'd like to get rid of the subdomains and use the power of .htaccess
My goal is to give the same URL:
www.mydomain.com/sister1
www.mydomain.com/sister2
www.mydomain.com/mommy
www.mydomain.com/daddyo
Obviously, I don't plan to have literal working directories for each person.
I'd pass the "sister1" portion to a process.php script that takes care of the rest.
I've figure out how to do it by manually typing each RewriteRule in my htaccess file:
Options +FollowSymLinks
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteRule ^/?sister1$ process.php?entity=sister1 [L]
RewriteRule ^/?sister2$ process.php?entity=sister2[L]
RewriteRule ^/?mommy$ process.php?entity=mommy[L]
RewriteRule ^/?daddyo$ process.php?entity=daddyo[L]
I feel this is the long way of doing it.
Is there a more universal way of extracting the text after the first "/" forwardslash, and passing it to process.php?entity=$1 ?
I tried it this way:
RewriteRule ^/([A-Za-z0-9-]+)/?$ process.php?entity=$1 [NC,L]
I'm getting the apache 404 error: "Not Found".
It is because you have a mandatory / in the beginning of your rule, i.e., you are always looking for something like /sibling in the URL. Your first examples have that first forward slash as optional due to the question mark after it.
You do not need a beginning forward slash - normally the rewrite rule picks up stuff after the domain name
www.example.com/string/mod/rewrite/gets/is.here
So just remove the starting slash and it should work.

block some pages with htaccess

I used to have php site with some keywords in file name like this
domain.com/?keyword-one,23
domain.com/?another-keyword,43
domain.com/?one-more-keyword,23
and so on
the problem is that I have an index.html file now and the above pages are still works because of that ? after the first / because the server is attaching those files as domain.com/index.html?keyword-one,23
is there any way to show error 410 gone using htaccess?
I used RewriteRule ^index.html$ - [R=404,L,NC] and it is not working.
Nevermind, I got it to work.
RewriteRule ^/?$ - [R=410,L,NC]

htaccess rewrite working offline on WAMP but not online on linux host

I'm just going to explain my problem here :
http://mysite.com/movie/20000
should be rewritten to
http://mysite.com/movie.php?id=20000
my htaccess file :
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie/([0-9]+)$ movie.php?id=$1
On my localhost WAMP installation this works fine, but when I put it online on my linux host it doesn't completely work. It does go to the movie.php page, but it seems it gives no GET parameter id.
Edit :
if I use
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^movie([0-9]+)$ movie.php?id=$1
then
http://mysite.com/movie20000
Goes to the correct page, but I would like /movie/20000 and not just /movie20000
It also seems that my host automatically rewrites a visit to mysite.com/movie to mysite.com/movie.php
After searching for a long time, and pulling some of my lovely hair out I found the solution.
I just added
Options -MultiViews
to my htaccess file and that fixed it.
Why? Because .php was being added to urls without an extension, which I really did not need.
This should work.
RewriteRule ^movie/([0-9]+)$ http://mysite.com/movie.php?id=$1 [NC,L]
Don't forget the [NC, L] it means Case insensitive, last rule... If you don't, it will continue to process through your htaccess rules, maybe triggering other ones.
Still, the stuff below is good advice.... :)
Check to see if the Rewrite module is loading with apache. Look for this line in the httpd.conf file:
LoadModule rewrite_module modules/mod_rewrite.so
Check to see if your apache config allows for .htaccess files for your system or in the virtual host definition.
Also, test with a simpler rewrite catch all and test that alone to see if it's working at all like this (get rid of everything else in your htaccess file to limit the test):
RewriteEngine On
RewriteRule ^(.*)$ http://www.google.com [L,R=301]
Any request to your site should go to google if the configuration for apache is correctly set.

Resources