PHP URL Rewriting - apache

I am setting up some rewrite rules on an Apache server using mod_rewrite. I was wondering if it was possible to write a rule that will basically re-direct the user to the home page if the page is not found i.e.
http://example.com/test <-- does not exist
However, I would like if the user was to navigate to this domain they are automatically re-directed to:
http://example.com/
With this in mind, I don't want the URL to still display "http://example.com/test" I would like the URL to update itself to become "http://example.com/".

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ http://domain.com/ [L,R=301]
Essentially, "if the requested filename is not a file or directory, redirect".

I wouldn’t use a HTTP redirection but instead send an error document together with the proper error status code.

Related

Apache mod_rewrite - unwanted redirect instead of rewrite

I have an issue with mod_rewrite and I can't seem to solve it. I stripped the example down to the bare bones and I don't understand why a specific rule forces my browser to redirect instead of rewrite:
RewriteEngine on
#if request is for a physical-file OR for one of the language paths - skip (return as-is)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{REQUEST_URI} ^/de [OR]
RewriteCond %{REQUEST_URI} ^/en-US
RewriteRule ^ - [L]
#otherwise: rewrite to en-US folder
RewriteRule ^(.*)$ /en-US/$1 [NC,L,QSA]
I read the documentation very carefully and it seems like this should actually rewrite every call, so https://example.com/fuBar.html should actually retrieve the file /en-US/fuBar.html from my server - the users browser shouldn't know about it.
What's really happening is that for some reason the browser is redirected to https://example.com/en-US/fuBar.html. While this does display the correct content, it's just not what I want or what I thought this RewriteRule should do. What am I doing wrong?
*add - the .htaccess of the subfolders de and en-US:
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
There's nothing in the code you've posted that would trigger an external "redirect".
Make sure you have cleared your browser (and any intermediary) cache(s) to ensure you are not seeing an earlier/erroneous 301 (permanent) redirect. (301 redirects are cached persistently by the browser.)
Check the "network traffic" in the browser's developer tools to see the precise nature of this redirect to see what it redirects from/to, and well as the 3xx HTTP status code of the redirect (if indeed this is an external redirect).
It would seem the front-end (JavaScript/Angular) is manipulating the URL in the address bar (there is no redirect). From comments:
Actually there was no redirect happening at all! Rather since I set <base href="/en-US"> somehow my frontend (Angular) seems to have outsmarted me, manipulating the address without me realizing it. Turns out I don't even need to change the base href, I just need the rewrites.

Can't get rewrite to redirect all to front controller

I'm trying to redirect all site traffic to my front controller, but I've been having trouble with it getting my site to redirect when the file already exists. For instance: www.example.com/IDontExist/ doesn't exist and sends me to the controller, but www.example.com/maintence/ exist and therefore skips my controller entirely. Not the intention.
I've tried a couple of things in .htaccess files, and I need to use them because I have to upload it to a Godaddy Shared Linux Server, and don't have much access to higher class configs.
This is my current .htaccess file:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php [NC,L,QSA]
</IfModule>
I need to send people going to www.example.com/maintenance/ and all other urls to the front controller to get the page processed before sending it off to the client. Right now it's finding the file and loading that up before going to the front controller.
Edit: Forgot removing RewriteCond commands for some reason result in a 500 error.
Remove your file and directories excludes, after that
RewriteEngine On
RewriteRule ^([^?]*)$ /index.php [NC,L,QSA]
It was a redirect loop. Removing the conditions and then adding the following one would fixed it.
RewriteCond %{REQUEST_URI} !/index.php

Make 301 redirect when file does not exist in the server

I know I can use "ErrorDocument 404" to set a redirect when somefile does not exist anymore in my server BUT the server sends a 404 status to the browser. I want to make that redirect when the file does not exist using 301 status and using only my htaccess file.
So my question is this: if the user hits URL like http://some-domain-blabla.com/fldsjfds.php and there is no such file "fldsjfds.php" in the server, how an I redirect the user to my MAIN PAGE using 301 redirect?
I dont want use 404 for very reasons, one of them is SEO, a programmer moved some pages of us to other names and I dont want to lose all that juice, I want to send a 301 redirect so google pass that PR along.
You can use this rule in site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [L,R=301]

Using Apache mod_rewrite to send all requests to a file

How can I use mod_rewrite to send certain types of incoming urls to a particular file on the server?
Ideally any html requests would load a file (eg /var/www/sites/urlprocessor.php) which would then determine what is being requested and the display the appropriate content based on the url.
Example: Say I wanted a url to list some 'cars'. I would want the following URLs to load the urlprocessor.php file instead of loading the actual page and instead of doing a redirect.
http://www.mydomain.com/cars.html --> /var/www/sites/urlprocessor.php
http://www.mydomain.com/cars.htm --> /var/www/sites/urlprocessor.php
http://www.mydomain.com/cars --> /var/www/sites/urlprocessor.php
On the flip-side, non-html requests (jpgs, pdfs, etc) would continue on normally without loading the urlprocessor.php file.
I am currently using the last line of code in the following to accomplish this. It 301 redirects all html page requests to 'index.html' which then loads the urlprocessor.php file. I prefer to just load the file without the 301 redirect.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteRule !^.*(\.gif|\.GIF|\.png|\.mp3|\.m3u|\.js|\.jpg|\.JPG|\.css|\.swf|\.flv|\.ico|\.wav|\.doc|\.DOC|\.pdf|\.PDF|\.xls|\.XLS|\.txt|\.TXT)$ index.html [L]
Is there a way to go directly to a file without doing the redirect?
Thanks!!
Tom
Given that /var/www/ is the root folder then this would work:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /sites/urlprocessor.php [L]
Basically this will redirect any non-existent folder and file to the urlprocessor.php
For more information on how to handle the redirect with your PHP you can refer yourself to this answer.

mod_rewrite: How to get all requests and send to a PHP script for routing?

I need the Rewrite settings and rules for getting all server/URL requests and send them to a PHP script, and later route the requests from that PHP.
Is this possible and how?
I have the following, but with this I can't process all requests to my PHP script:
RewriteRule ^([0-9]+) router.php?first_request=$1
You get the idea what I want...
Put these rewrite rules to your .htaccess file, which would redirect all requests which are not pointing to valid directory or file to your specific script:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ your_php_file.php?url=$1
From your script you can obtain value of the url variable $_GET['url'] and split it as needed.