Using .htaccess to reroute all requests through index.php EXCEPT a certain set of requests - apache

So I just inherited a site. The first thing I want to do is build a nice little standard, easy-peezy, CMS that allows for creating a page with any URL (for example: whatever.html).
Therefore, if user hits example.com/whatever.html, it should get any db info for whatever.html and display it. This is run of the mill stuff.
My problem is that there are quite a few pages on the site (all listed in the .htaccess) that need to continue to be accessible. For instance, /Promotions is linked to promotions.php via .htaccess, and I need it to stay that way.
Anyone know how I can construct the .htaccess file to allow specific rewrites to still work but to reroute all other requests through index.php?
Currently, I just have .htaccess show a custom 404 page which in turn checks the db for the url and displays it if it exists. This is an easy solution, but I know that some people have weird browser toolbars (dumb or not, they exist :) ) that autoredirect 404s, and I'd hate to annoy my users with these toolbars by not allowing access to certain pages.
Thanks so much for your help!

The RewriteRule for promotions should still work as it's not 404ing.
If the 404 handler is showing the page because it exists in the database then it should really be returning a 200 OK status (overriding the 404 one), so you should not get any issues with browser toolbars.
As for doing the rerouting you can do something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/(promotions|anotherone|somethingelse)($|/.*$) [NC]
RewriteRule ^(.*)$ /index.php?p=$1

Here is another variant:
RewriteEngine on
RewriteRule ^/i/(.*)$ - [L]
RewriteRule ^/css/(.*)$ - [L]
RewriteRule ^index.php$ - [L]
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]

Related

Change URL in browser without redirection with htaccess

I've looked everywhere to find the proper solution/method but I can't seem to find anything that works for me.
I even asked friends and they helped but none prevailed.
What i'm trying to do is, changing the URL displayed in the browser but only that. (No rediraction, page re-loading).
I want to do this to make my UCP just cleaner looking when going through certain pages/files.
What am I trying to achieve?
Heres an example on a profile, the URL would be:
mysite.com/ucp/profile.php?player=Heartfire
However, I want it to look like
mysite.com/ucp/profile/heartfire
Or something else! I just want to get rid of the parameters AFTER the .PHP
I've tried various examples found with google and this website but none seems to work, could somebody please guide me along the way to achieve the result.
what have I tried so far?
Here are a few examples of what I tried before:
RewriteRule ^profile/([0-9]+)/?$ /ucp/profile.php?player=$1
RewriteRule profile.php?player=$1 profile.php [NC,L]
RewriteRule ^profile$ profile.php?player=$1
So what am I doing wrong that it isn't working?
Put the following in .htaccess file inside website's root directory:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /ucp/profile\.php?([^=]+)=(\S+) [NC]
RewriteRule ^ucp/profile\.php$ /ucp/%1/%2? [R=301,L,NC]
# Now, deal with internal rewrites (which will not cause redirection):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ucp/([^/]+)/([^/]+)/?$ /ucp/profile.php?$1=$2 [NC,L]
You can use internal redirects what will not change your url but map your request as your wanted.
What you want is impossible because:
Htaccess and rewrite is at server side. The request arrived to the server, need to rewrite at serverside and you need to change it in the clients url bar.
To achieve this the server should send a redirect with the url what you expected. This ia why redirect is mandatory. Server can't rewrite clients urls, just can send a redirect response.
Internal redirect can simulate you something like the request was what you expected but it is transparent at for the clients.
Btw, permanent redirect is the right solution here to notify the user and give the chance to let them know the resource has been changed and update the bookmark / api / whatever.

How to rewrite all 404's to index.php using cPanel URLs?

I'm trying to rewrite all 404's to index.php where I use PHP's parse_url() to determine which file to include (e.g. about-us.php, contact-us.php) and I'm getting some really weird results.
I'm working on a 'dev' URL automatically created by cPanel:
http://xxx.xxx.xxx.xxx/~mySite/
Current Method
My .htaccess file contains the following:
RewriteEngine on
RewriteBase /~mySite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]+)$ index.php
And the results are a mixed bag:
xxx.xxx.xxx.xxx/~mySite/contact-us renders just fine.
xxx.xxx.xxx.xxx/~mySite/contact-us/phone throws a 404 that isn't caught by mod_rewrite.
xxx.xxx.xxx.xxx/~mySite/about-us rewrites to the server root (xxx.xxx.xxx.xxx/index.php).
Previously Tried
ErrorDocument 404 /~mySite/index.php
And I get the similar results, except for the following:
xxx.xxx.xxx.xxx/~mySite/contact-us/phone rewrites to index.php but all my CSS and JS includes are off because they're trying to load relative to /~mySite/contact-us instead of /~mySite.
Any help? I'm going out of my mind. Especially the fact that contact-us works fine, but about-us doesn't?
First don't use cPanel preview. That is not a good way to view your dev site. Who knows how it will affect the rules. Also control panels do weird things anyway.
Preview your site using your real site domain name. You can do that my modifying your HOST file on your computer so that only you can view it by the domain name. This little guide will show you how to edit it. It takes like 2 minutes.
Then that should help to check things better.
Most likely why the error document doesn't work because ~mySite is most likely not your document root. That is typically how cpanel does it's preview links. So your real error document should probably be as Marc B stated.
ErrorDocument 404 /index.php
If you want a mod_rewrite solution, this should also work. But I wouldn't use both.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [R=301,L]
On a side note, I think it's usually good to use a 404 page so that users know that the requested page is not a real page instead of some people thinking something is just wrong with your site because it redirects to the home page. Like facebook, it gives you a big thumbs up with a bandage on it saying it the page is not available. I've seen many custom 404 pages that were pretty clever so maybe it might be time to just get creative.

Htaccess multilanguage mod_rewrite

Ok, I've wasted a lot of time trying to figure this out but the more I'm trying to learn about rewriting rules, the less I seem to understand what's going on. I need to fix some rewriting rules for a codeigniter based project.
The application is a multilanguage CMS that basically works with modules, pages and posts and in a .htaccess file I need to check if the url is a page or a post and redirect to a appropriate controller.
So far in my .htaccess file i have the following:
#if I'm on the homepage and i have a language id
RewriteCond $1 ^([a-z]{2})/?$
RewriteRule ^(.*)$ index.php?homepage/index/$1 [PT,L]
#if module - this works ok when there is no language id
RewriteCond $1 ^(gallery|post|products)
RewriteRule ^(.*)$ index.php?$1 [PT,L]
# Rewrite all other URLs to index.php/URL - page controller
RewriteRule ^(.*)$ index.php?page/show/$0 [PT,L]
With the code above, if I enter
http://mydomain.com/gallery/something/9
everything is fine and dandy, dut if I add a language id like so:
http://mydomain.com/en/gallery/something/9
it doesn't work anymore, and I get a page not found/404 error from the page controller (redirect at the end of .htaccess).
So let's say that i have urls that can look something like this:
http://mydomain.com/gallery/something/9
http://mydomain.com/en/gallery/something/25
http://mydomain.com/fr/post/something/31
I need to somehow redirect those urls to
http://mydomain.com/gallery/something/9
http://mydomain.com/en/gallery/somethingelse/25
http://mydomain.com/fr/post/somethingelse/31
Notice that sometimes I have language id, and sometimes I don't.
Is this something that I should be doing by using htaccess? Is Codeigniter routing a better alternative?
Thank you for any help Stack Overflow :)
Try that:
RewriteCond $1 ^[^/]*/?(gallery|post|products)

htaccess redirect for global subdomains system - all subdomains redirected to one php file to handle them all

i really could use some advices related to subdomains.
recently we decide to make every subdomain of main portal to be available for blogs (something similar to lets say any blog platform)
so what i cant achieve is to make redirection to certain folder of main portal site or php file like:
xxxxx.domain.com/index.php?get=xx&get2=xx -> to be run by actual php file located on main portal account, lets say subdomains.php?get=x&get2=xx, alternatively - to be run by mainportal/subdomains/index.php?get=x&get2=xx
i tried with
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.pl$ [NC]
RewriteRule (.*) subdomain.php [L]
effect was almost correct but get parameters are missing:(
on the other hand it can be entirely wrong... i simply dont understand or that rewrite rules:(
thanks for any help/tips
You can try with QSA (query string append):
RewriteCond %{HTTP_HOST} ^.*\.domain\.pl$ [NC]
RewriteRule . subdomain.php [QSA]

.htaccess redirect doesn't hide url

my .htaccess in the root folder includes the following lines :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC]
when I open the address http://example.com/bla.htm, my browser doesn't hide the GET values specified in the .htaccess, it redirects me to ?city=bla. eventhough I'm not using the [R] switch. This has always worked for me before (as I remember, haven't dealt with htaccess in a while). What's wrong here ?
When you redirect to an entire URL, it doesn't do URL rewriting (you can't exactly rewrite URLs on someone else's website).
Assuming both URLs are on the same server, you need to do something like
RewriteRule ^(.*)\.htm$ index.php?city=$1 [NC]
Also, I'd recommend getting into the habit of using the [L] switch whenever you can - it helps avoid bugs when you have a lot of URLs to rewrite.