.htaccess rewrite advanced rule - apache

I would like a .htaccess rewrite rule, but I can't figure out how to make it.
Examples
www.example.com/index.php => www.example.com/index.php
www.example.com/folder/profile.php => www.example.com/profile.php
www.example.com/folder => www.example.com
A rewrite rule that can serve files from a specific folder at top level, while still serving the top-level files.

here u go! try this
EDIT: made changes to exclude index.php!
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
i do the same for my Zend Framework projects , where i route all the requests from my http://domain.com/public/ to http://domain.com/

To redirect if a file does't exist, do this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /folder/$1 [R]

Related

.htaccess redirect issue with multiple .htaccess file

I am using .htaccess file like below for redirect www to non www on my public_html directory
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
its working fine and redirect like below
www.example.com
to
https://example.com
Now in my sub directory called latest, I have another .htaccess file like below
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
which is making my url clean like below
www.example.com/latest/index.php?page=1
to
www.example.com/latest/1
but even I have .htaccess file in home directory which is making www to non www, my this directory does not redirect www to non www.
I want redirect
www.example.com/latest/1
to
https://example.com/latest/1
I am sure .htaccess file in my directory is causing issue but I do not know how to resolve it. Let me know if anyone here can help me for the same.
Thanks!
The problem is, mod_rewrite is not inherited (at least by default). If you have a rewrite rule in the "default" directory .. You're going to have to include that rule in all subsequent directories "below" that. I have never seen (or heard of) a setting for allowing mod_rewrite to allow inheritance from parent .htaccess files. In general, this is just accepted as "the way it is".
UPDATE
THIS QUESTION In Server Fault explains this as well. The mod_rewrite rules from the previous htaccess files don't even get processed.
COMBINE THE 2 in the SUB DIRECTORIES
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# add this only to sub directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

Wildcard subdomain .htaccess and Codeigniter

I am trying to create the proper .htaccess that would allow me to map as such:
http://domain.com/ --> http://domain.com/home
http://domain.com/whatever --> http://domain.com/home/whatever
http://user.domain.com/ --> http://domain.com/user
http://user.domain.com/whatever --> http://domain.com/user/whatever/
Here, someone would type in the above URLs, however internally, it would be redirecting as if it were the URL on the right.
Also the subdomain would be dynamic (that is, http://user.domain.com isn't an actual subdomain but would be a .htaccess rewrite)
Also /home is my default controller so no subdomain would internally force it to /home controller and any paths following it (as shown in #2 example above) would be the (catch-all) function within that controller.
Like wise if a subdomain is passed it would get passed as a (catch-all) controller along with any (catch-all) functions for it (as shown in #4 example above)
Hopefully I'm not asking much here but I can't seem to figure out the proper .htaccess or routing rules (in Codeigniter) for this.
httpd.conf and hosts are setup just fine.
EDIT #1
Here's my .htaccess that is coming close but is messing up at some point:
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).domain [NC]
RewriteRule (.*) index.php/%1/$1 [QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
With the above, when I visit: http://test.domain/abc/123 this is what I notice in $_SERVER var (I've removed some of the fields):
Array
(
[REDIRECT_STATUS] => 200
[SERVER_NAME] => test.domain
[REDIRECT_URL] => /abc/123
[QUERY_STRING] =>
[REQUEST_URI] => /abc/123
[SCRIPT_NAME] => /index.php
[PATH_INFO] => /test/abc/123
[PATH_TRANSLATED] => redirect:\index.php\test\test\abc\123\abc\123
[PHP_SELF] => /index.php/test/abc/123
)
You can see the PATH_TRANSLATED is not properly being formed and I think that may be screwing things up?
Ok, I believe I have solved it. Here's what I have so far.
First the .htaccess
RewriteEngine On
RewriteBase /
# if REQUEST_URI contains the word "user" and the
# SERVER_NAME doesn't contain a "." re-direct to the root
# The reason this is done is because of how the last two rules
# below are triggered
RewriteCond %{REQUEST_URI} (user) [NC]
RewriteCond %{SERVER_NAME} !\.
RewriteRule (.*) / [L,R=301]
# Allow files and directories to pass
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# Codeigniter rule for stripping index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [C]
# Force wild-card subdomains to redirect.
# E.g. http://me.domain/foo/bar/123 as http://domain/user/me/index.php/foo/bar/123/bar/123/
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).domain [NC]
RewriteRule (.*) /index.php/user/%1/$1/ [L]
And finally routes.php
<?php
// Force routing to userhome controller if URL contains the word "user"
// otherwise force everything else to home controller
$route['user/:any'] = "userhome";
$route[':any'] = "home";
?>
As you can see from above everything works. The only thing I can't figure out is why the last arguments are repeated when I use a subdomain?
If I do: http://domain/foo/bar/123
Then my PATH_INFO is shown as /foo/bar/123/ which is perfect
But if I do: http://me.domain/foo/bar/123
Then my PATH_INFO is shown as /user/me/index.php/foo/bar/123/bar/123/
Which for the most part is OK but why is the parameters repeating in the end?
So yea overall I think it's working. Only thing I'll have to do is have several routes for any controllers I add to my \controllers. Unless there's a way around it?
This should work. Please test and let me know if it works:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+)\.domain\.com(.*) /$1$2 [L]
RewriteRule ^(.*) /home$1 [L]

how do i rewrite URL such as

I need to rewrite my web URL such as
http://www.site.com/profile/ -> profile.php
http://www.site.com/settings/ -> settings.php
and another word of profile and settinhs is link to user.php
example,
if I push http://www.site.com/giffary/ - it's link to user.php?name=giffary
but when i push http://www.site.com/profile/ - it's link to profile.php not user.php.
how can i write .htaccess file
thank you :)
You will need to use mod_rewrite for this, obviously. I have not tested it, but this config should work:
RewriteEngine On
RewriteBase /
# Ignore URLs that point to files/directories that actually exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Special rewrites
RewriteRule ^/profile/?$ /profile.php [L]
RewriteRule ^/settings/?$ /settings.php [L]
# User profile rewrites
RewriteRule ^/([^/]+)/?$ /user.php?name=$1 [L]

Need help with Apache Rewrite issues

My developer has provided me some Apache rewrite rules that are required for our application to work. When I added them to Apache my www.domain.com/blog and www.domain.com/phpmyadmin pages no longer worked. I tried to add the first RewriteCond rule for my blog and also the final phpmyadmin rule but neither one is working as expected. Essentially I want any requests to /blog or /phpmyadmin to NOT rewrite and go to my document root directory and run those applications outside of rewrites. Can you help me figure out a solution? Thanks
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^/(.*_css.*\.css.*) /$1 [QSA,L]
RewriteRule ^/(.*_js.*\.js.*) /$1 [QSA,L]
RewriteRule ^/(.*_swf.*\.swf.*) /$1 [QSA,L]
RewriteRule ^/(.*_img.*\.[jpg|JPG|jpeg|JPEG|gif|GIF|bmp|BMP|png|PNG].*) /$1 [QSA,L]
RewriteRule ^/(.*)$ /index.php?url=$1 [QSA,L]
RewriteRule ^/phpmyadmin(.*)$ /phpmyadmin$1 [QSA,L]
</VirtualHost>
They are located at www.domain.com/blog and www.domain.com/phpmyadmin.
Apache 2.2.13
Thanks!
If you want to avoid rewriting URLs that begin with /blog/ or /phpmyadmin/ then you may be able to get away with the first rule being:
RewriteRule ^/(?:blog|phpmyadmin)/ - [L]
(replacing your RewriteCond and then removing the old phpmyadmin rule).
A quick explanation: this matches any URLs that begin with /blog/ or /phpmyadmin/ and doesn't rewrite them (- for the replacement), and then stops any further rewriting ([L]).
Previous answer:
Your rewrite rules are conflicting, for a start. Also, at the moment, the RewriteCond only applies to the first rule. Also, the final rule is ignored because of the rule before it matching everything. You may want something like this:
RewriteRule ^/blog/(.*)$ /index.php?url=$1 [QSA,L]
as I assume you're just trying to rewrite all URLs from /blog/foo/bar to /index.php?url=foo%2fbar? If not, please explain what you're trying to accomplish and I'll edit my answer.
Err... not sure what you are trying to do. Are you trying to "Exclude" static files as images/stylesheets/flash movies/javascript files and the link to PHPMyAdmin? Maybe something like this could do the job for you 'quick 'n dirrrrty'
# Enable the RewriteEngine
RewriteEngine On
RewriteBase /
# Check if the file, directory or symlink does not already exists.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-F
RewriteCond %{REQUEST_FILENAME} !favicon\.ico
RewriteCond %{REQUEST_FILENAME} !robots\.txt
# Rewrite all other requests to the index.php
RewriteRule ^(.+)$ /index.php?url=$1 [QSA,L]

.htaccess rewrite to default language folder?

I have my site broken down into several folders by language:
/
/en/
index.php
about-us.php
faq.php
...
/fr/
index.php
about-us.php
faq.php
...
...
etc.
I'd like to have a rewrite rule that automatically rewrites to the en folder if somebody tried to enter mydomain.com/about-us.php.
FYI, I also already have a rewrite rule in place that removes the extension, so really I want to make sure that mydomain.com/about-us rewrites to mydomain.com/en/about-us. Here's my existing rewrite rule that does this:
# allows for extension-agnostic urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
That make sense? Anyone help me out here?
EDIT:
#Gumbo -
Here's what my .htaccess file looks like (this is all that's in it):
RewriteEngine On
# defaults to the english site
RewriteRule !^[a-z]{2}/ /en%{REQUEST_URI} [L,R=301]
# allows for extension-agnostic urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
This file is in the root of my website (not in a language folder). I've reloaded my webserver, just to be on the safe side, and I still get this error message when I try to go to mydomain.com/about-us:
Not Found
The requested URL /about-us was not found on this server.
... where mydomain.com/en/about-us works just fine.
Any other thoughts?
Put this rule before your rule:
RewriteRule !^(fr|en)/ /en%{REQUEST_URI} [L,R=301]
Or more general:
RewriteRule !^[a-z]{2}/ /en%{REQUEST_URI} [L,R=301]