How to create mode_rewrite rule on Apache Server FreeBSD 8.1 - apache

I'm Working on a server running apache version 2.2.25 on FreeBSD 8.1.
Lets say I have the the following url: this.domain.com/html/folder/index.php
And I want to rewrite it as this: this.domain.com/index
How specifically would I create the rewrite rule? Should I create the rewrite rule in the httpd.conf file or the .htaccess file, or does it not really matter?
Also, is there a way to change the domain name "this.domain.com" to something else?

Assuming, you want users to enter the shorter url this.domain.com/index and serve the php at this.domain.com/html/folder/index.php; add the following rules to the .htaccess at root /.
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^this\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ html/folder/$1.php [L]
I'm not sure what you mean by changing your domain to something else. this.domain.com can be changed to that.domain.com or addon-domain.com but would make sense only if their DNS point to the same server (to serve shared content). Otherwise, it's like an external redirect to some other site.
RewriteCond %{HTTP_HOST} ^this\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://that.domain.com/$1 [R=301,L]

Try this
RewriteRule ^/index$ /html/folder/index.php [PT]

Related

Keep URL request but adjust server path

How can I achieve the following:
I have two domains hosted within the same web root path on the server. Usually php manages my HTTP_Hosts dynamically. Related to my question I am using the directory lisings function of apache. Each Request for /peter/ should point effectively to a different directory.
example.com/peter/ -> /peter_example.com/
xamplee.com/peter/ -> /peter_xamplee.com/
The Url should always contain /peter/ but in effect link to the respective real path which I'd like to have hidden.
Thank you!
Finally, after getting into regex and into mode_rewrite the hard way I can come up with the solution all by myself:
Options -MultiViews
RewriteEngine On
RewriteBase /
# Force adding a trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^peter/(.*)$ /peter_example\.com/$1 [NC,L]
RewriteCond %{HTTP_HOST} ^www\.xamplee\.com$ [NC]
RewriteRule ^peter/(.*)$ /peter_xamplee\.com/$1 [NC,L]

override apache rewrite rules with htaccess

working on a server that hosts multiple domains.
Somewhere in apache config is a rewrite rule that is set for ALL domains.
What happens is if a user goes to example.com/foo they are supposed to get redirected to example.com/foo_bar
However, in one domain, I want to override this behavior so that the url stays at example.com/foo and does not redirect. I've been searching and trying various rules and conditions to no avail. I don't have access to the apache config, but I am using .htaccess for some rewrite rules on this domain.
here's my rewrite rules in .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
# this isn't working
# RewriteRule ^(foo)($|/) - [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1
# this didn't work either
# RewriteRule ^/foo index.php?/$1 [L]
</IfModule>
Try redirecting /foo_bar back to /foo.
When Apache processes the rewrite rules it runs through them multiple times, which you can see when you turn on debugging. I think what's happening in your case is that you're trying to match the URI in the first pass, but Apache has already modified it to /foo_bar.
Also, as a matter of debugging, you should try to recreate the problem in an environment you control. Ask your sysadmin for a copy of the global configuration and mirror the set up you're constrained to.
You can create exception for one domain:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteRule ^foo(/.*)?$ /foo_bar$1 [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Apache2 mod_rewrite pattern restriction?

So I have a site that I want to make SEO friendly by using mod_rewrite. I want to make the URLs easy to remember by dropping the .php on the end of them and using mod_rewrite to re-attach them later on. So for example say http://example.com/about would point to http://example.com/about.php. I have the RewriteRule that should work from my experience but for some reason doesn't.
My rule:
RewriteEngine On
RewriteRule ^/?about$ about.php [L]
RewriteRule ^/?faq$ faq.php [L]
Now these rules don't work like that exactly. it seems that if I rename the files to blah.about.php and blah.faq.php and change the RewriteRule lines to reflect the new filenames it works.
Is this a restriction of mod_rewrite where the Pattern can't be so close to the target file?
Try this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*)\.php [NC]
RewriteRule ^ /%1 [R=301,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
First rule redirect the request to phpless page and the next internally to the php file itself.
Is this a restriction of mod_rewrite where the Pattern can't be so close to the target file?
Its not really a restriction, you just can't redirect the php file to non-php and then back to it as it generates a loop, so what we do is capture the request and redirect from there and then internally redirect to the file.
Unless of course like you have seen they have a different naming.

.htaccess in subdomain

I'm trying to set up a subdomain as follows:
http://subdomain.domain.com should load what is really http://subdomain.domain.com/index.php/contest/
http://subdomain.domain.com/stepone should load what is really http://subdomain.domain.com/index.php/contest/stepone
http://subdomain.domain.com/images/example.jpg should load http://subdomain.domain.com/images/example.jpg as normal.
I've had this all set up and working in a subdirectory on the main domain before but now that I'm trying to do it on a subdomain it's stopped working.
Here is my .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|javascript|email|robots\.txt|test\.php|terms\.html)
RewriteRule ^(.*)$ /index\.php/contest/$1 [L]
Please help me!
Are rewrite rules working at all? I.e. do you have AllowOverride All or something for virtual hosts, as per:
http://www.webhostingtalk.com/showthread.php?t=481815
Also, take a look this comment by Gumbo on this SO question:
Htaccess help, $1 being used before its set??? what?
The Drupal .htaccess file is a good example of mapping /?q=query to /query, but not redirecting things which provide an explicit file/directory match. Here's the relevant snippet from Drupal's .htaccess with ?q= changed to index.php/contest/.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/contest/$1 [L,QSA]
If you need to restrict the domain so that it doesn't redirect normally but just from the one domain, insert before the RewriteRule this:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
I think I have found the answer using:
RewriteRule ^(.*)$ subdomain.domain.com/index\.php/contest/$1 [L, P]
Is there any reason I shouldn't use the force proxy like this? (Does it slow things down?)
Thanks for your answers guys.

.htaccess Rewrite Rules for subdomain

I use codeigniter as my main install on the main domain. I have created a subdomain and a folder called live e.g. live.domain.com maps to public/live . However in public I use codeigniter.
I now have the dynamic codeigniter url:
http://domain.com/api/
which I want to map to my subdomain:
https://live.domain.com
So going to:
https://live.domain.com/api/functioname
would be using the script:
http://domain.com/api/apifunctioname
and possibly:
http://domain.com/api/apifunctioname/parameter1/parameter
Everything is on the same server so no redirects are needed.
Anyone have any ideas on which rewrite rules to use?
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.domain\.com [NC]
RewriteRule (.+)$ "http://domain.com/api/$1" [L]
The above works great as a rewrite but redirects to http://domain.com/api/functionname instead I want it to route; so that when going to:
https://live.domain.com/api/functioname
It stays at that url but uses the script of
http://domain.com/api/functionname
Thank you very much,
Ice
How about something like the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.domain\.com$ [NC]
RewriteRule (.+)$ "https://domain.com/api/$1" [L,P]
Just add index.php, see below
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.domain\.com$ [NC]
RewriteRule (.+)$ "https://domain.com/index.php/api/$1" [L,P]