I am modifying the .htaccess file of a legacy PHP web application. I am not familiar with apache .htaccess syntax. I found this tutorial. What I am trying to do is that I am trying to redirect all the requests to a URL/ path if the request URL is not a specific URL/ path. For example, all the requests to the website will be redirected to localhost/my-custom-page unless the request URL is localhost/my-custom-page.
I know how to redirect mapping 1 to 1 as follows:
RewriteEngine on
RewriteRule ^my-old-url.html$ /my-new-url.html [R=301,L]
But, what I am trying to do is that redirecting all the requests to the specific page unless the request is to that page. Even the home page will be redirected to that page. How can I do that?
When I tried the following solution
RewriteEngine on
RewriteCond %{REQUEST_URI} !/my-new-url.html
RewriteRule ^ /my-new-url.html [R=301]
I get the error
I want to check using OR condition as well. For example, if the path is not path-one or path-two, redirect all the requests to path-one.
2
Answers
Your question is a bit vague, due to your wording. But I assume this is what you are actually looking for:
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported
[END]
flag in your http servers error log file in that case. You can either try to upgrade or use the older[L]
flag, it probably will work the same in this situation, though that depends a bit on your setup.It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out…
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (“.htaccess” file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it’s interpretation is enabled at all in the host configuration and that it is located in the host’s
DOCUMENT_ROOT
folder.And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (“.htaccess”). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
There are a few potential issues with this, particularly since you hint in a comment that you are perhaps using a front-controller to “route” the URL.
This redirect satisfies the conditions outlined in the question, but does assume that you have no other rewrites, have an essentially “static site” and are not linking to any static resources.
You are missing an
L
(last
) flag, so processing will continue through the file and possibly be rewritten if you have later rewrites.If you are rewriting the URL to a front-controller in order to route the URL (as you suggest in comments) then this redirect will break, as it will redirect away from the front-controller. You need to only redirect direct requests, ie. when the
REDIRECT_STATUS
environment variable is empty.If you are linking to any static resources in the same file space then these will also be redirected. You need to create an exception for any static resources you are using, either by file extension (eg.
(css|js|jpg|png)
) or by location (eg./static
).So, try the following instead:
You don’t need a separate condition to implement the exception for the URL you are redirecting to. It is more efficient to do this directly in the
RewriteRule
pattern.The first condition ensures we are only redirecting direct requests and not rewritten requests to your front-controller.
The second condition avoids any static resources also being redirected. You could alternatively check the filesystem path if all your resources are stored under a common root. Or, as a last resort, implement filesystem checks (ie.
RewriteCond %{REQUEST_FILENAME} !-f
) if your static resources are too varied – but note that this is less efficient.You will need to clear your browser cache before testing, since any earlier (erroneous) 301s are cached persistently by the browser.