HTACCESS Mod_Rewrite Troubles - apache

I'm having a bit of trouble with my mod_rewrite, I had this running perfectly fine on my previous litespeed VPS. Now I'm using my own dedicated server running CentOS 6, so I don't know if I haven't configured correctly.
This is how it currently looks:
http://***.com/?pageName=FourthPage
This is how I want it to look:
http://***.com/FourthPage
This is my current .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^([^/\.]+)/?$ index.php?pageName=$1

I am not good with htaccess syntax. Configuring such rules should be done directly in the server configuration if you have access. Using htaccess for such purposes is only a workaround. However:
The pattern inside the RequestRule is wrong. Especially the trailing slash ('/'). What is it meant to match? You might want to read again through the documentation of that rule. The pattern is not matched against a full URL but only against a part of the path in case of htaccess.
Have a try with the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ index.php?pageName=$1
Also you should enable rewrite logging inside the server. The relevant configuration options are RewriteLog (for the file) and RewriteLogLevel (well, for the level...). It help to understand what is going on inside the rewrite module. However that cannot be done in htaccess too, you need access to the servers general configuration.

Related

URL Rewriting in .htaccess (Apache) displaying 404-Error

this is my first question here on stackoverflow because in the past I always found a question that described my problem perfectly.
But now they were not able to do that, so I decided to ask for help myself.
My goal is to display profiles, but the url shouldn't look like "/profile/show-profile.php?user=admin", just "/profile/admin".
So looked it up on google and found URL rewriting to be potentially useful, by editing the .htaccess file.
The problem is, it doesn't work. I already have some things in my .htaccess (redirecting to https and the 404-Page "/pagenotfound.php") and it seems like they don't work in combination.
# https redirecting
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://int-politics.com/$1 [R=301]`
# 404 page
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /pagenotfound.php
ErrorDocument 404 /pagenotfound.php
# URL REWRITING
RewriteEngine On
RewriteBase /profile/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ show-profile.php?user=$1
When I add the URL Rewriting part the 404-Page doesn't work anymore. Every site that doesn't exist just outputs "/pagenotfound.php" (see image -->)
Not-existing site just outputs /pagenotfound.php instead of showing it.
And the url-rewriting doesn't work too.
It would be wonderful if you could help me with this problem and tell me whats wrong. Thank you very much!
You usage of the RedirectBase is wrong. It should appear only once in such a distributed configuration file. Actually it is not required in this example at all ... Please take a look into the documentation for details on that: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
Also it is vital to understand that the rewriting engine loops if a rule gets applied. And why that makes using the L or the END flag so important.
That probably is what you are looking for:
RewriteEngine On
RewriteBase /
# https redirecting
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://int-politics.com%{REQUEST_URI} [R=301,END]
# profile rewriting
RewriteRule ^/?profile/(\w+)$ /profile/show-profile.php?user=$1 [END]
# 404 page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /pagenotfound.php [END]
ErrorDocument 404 /pagenotfound.php
Best is to implement such rules in the central http server's host configuration. If you do not have access to that (read: if you are using a cheap hosting provider) then you can use a distributed configuration file instead, if the consideration of such files has been enabled (see the documentation for the AllowOverride directive on that).

URL Rewrite of Non-Existent File Under Subdomain Folder

My website structure is as follows:
.htaccess
index.html
internal
.htaccess
test.html
api.php
I have setup a subdomain in GoDaddy such that internal.ledworld-me.com maps to ledworld-me.com/internal and the following works fine:
Requesting internal.ledworld-me.com/test.html serves the test.html file correctly.
Now I need the following:
If internal.ledworld-me.com/nonexistent/file is requested, then the URL should be rewritten as ledworld-me.com/internal/api.php?__route__=/nonexistent/file.
What do I put in my .htaccess file in order to achieve this? And which .htaccess file should I put the code in, the one under WebRoot or the one under internal?
Please note that GoDaddy is taking care of the rewriting from internal.ledworld-me.com to ledworld-me.com/internal. All I need my .htaccess file to do is the second part of the rewrite, namely, /nonexistent/file should be rewritten as api.php?__route__=/nonexistent/file.
It's really hard to explain this question and I've tried my best, please ask for clarification if needed. Thank you!
Put this code in your /internal/.htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ api.php?__route__=/$1 [L,QSA]
The following text in my internal/.htaccess file seems to do the trick:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://ledworld-me.com/internal/api.php?__route__=/$1
I'm still testing all the scenarios and will confirm that everything is working once I'm done all the tests.

RewriteCond Being Ignored?

I am trying to use mod_rewrite on a Ubuntu 12.04 server to make my URLs more readable, however I want to add an exception for images and css files.
My input URLs are in the format \controller\action which is then re-written to index.php?controller=controller&action=action. I want to add an exception so that if an image or css file is specified, the URL is not re-written, e.g. \images\image.jpg would not be re-written.
My .htaccess code is as follows:
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png|\.css)$ [NC]
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)$ test.php?controller=$1&action=$2 [L]
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)/([^/]*)$ test.php?controller=$1&action=$2&$3 [L]
My re-write code is working fine and the URLs are coming out as intended, however even if I request an image, the URL is still being re-written. It appears that my RewriteCond is being ignored, anyone any suggestions as to why this might be?
The RewriteCond only applies to your first RewriteRule, it should be reproduced for the second rule. However, I think that is better to add a non-rewriting rule, before, to exclude existing stuffs.
# Do nothing for files which physically exist
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
# your MVC rules
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)$ test.php?controller=$1&action=$2 [L]
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)/([^/]*)$ test.php?controller=$1&action=$2&$3 [L]
The rewriteCond rule is only applied for the next RewriteRule.
So you need to at least repeat the rewriteCond for your seconde RewriteRule.
No there is certainly better things to do.
For example a usual way of doing it is to test that the url is matching a real static ressource. If all your php code is outside the web directory (in libraries directory, except for index.php) then all styatic ressources available directly on the the document root can only be js files, css files, or image files.
So this is the usual way of doing it:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)$ test.php?controller=$1&action=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)/([^/]*)$ test.php?controller=$1&action=$2&$3 [L]
But this is a starting point. We could certainly find something to avoid doing 2 rules for this (maybe I'll have a look later)

Apache SetEnvIf and using %{ENV:...}

My question is related to the use of SetEnvIf and how I can then use the environment variables correctly as I need them. I shall explain what I need to do, then what I already have...
I need to set the php application directory in my htaccess file, as some of the rewrite rules use that directory name, and I'm trying to allow the admin user the ability to change it. It is currently set in the php, and changing it is done manually in the htaccess file and then again in the php. To achieve this, I currently have the following (taking advice regarding using env vars in rewrite rules)
SetEnvIf SERVER_PROTOCOL ".*" APP_DIR=application
It's set up that way to get the env var set before any rewrite rules, as SetEnv is processed after any rewrite rules, whereas SetEnvIf is processed before.
Now I can access APP_DIR using %{ENV:APP_DIR} in rewrite conditions, and the second part of any rewrite rules.
What I need to achieve is a little more complex than that, as the application directory doesn't exist, but is rather a redirect to let the PHP application know to process as a back-end rather than a front-end, for example the rewrites currently (without the env var) are
### ADMIN REWRITE
RewriteRule ^application\/?$ index.php?mode=administration&%{QUERY_STRING} [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^application(\/(.*\/?))?$ index.php?page=$1&mode=administration&%{QUERY_STRING} [NC,L]
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?page=$1&%{QUERY_STRING} [L]
So, as you can probably see, I need to be able to substitute application for APP_DIR to let the administrators of the system change the path, so it could be cms or admin etc.
Sadly, the following won't work
### ADMIN REWRITE
RewriteRule ^%{ENV:APP_DIR}\/?$ index.php?mode=administration&%{QUERY_STRING} [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^%{ENV:APP_DIR}(\/(.*\/?))?$ index.php?page=$1&mode=administration&%{QUERY_STRING} [NC,L]
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?page=$1&%{QUERY_STRING} [L]
%{ENV:APP_DIR} cannot be used in the regex of a rewrite, as the regex is processed before any rewrites, and the env vars.
Has anyone ever achieved what I need? Can it be done, even if it means adjusting the rewrite logic somewhat? I must state that the php logic after the rewrites can't be changed as it's a long time existing system which would have backwards compatibility issues if it were.
Use RewriteCond to match %{REQUEST_URI} with the environment variable up to the first slash (/), then start your RewriteRule regex with ^[^/]*/. That will work if you're not dealing with subdirectories; otherwise, you'll have to forego a definitive starting point in your RewriteRule regexes.

Apache .htaccess mod_rewrite and clean urls

Ok. So I'm building this site which is accessible through two different domains. So I can't use RewriteBase in my .htaccess. The rules (below) I use to work around this problem seem to work fine. However, when I use the below .htaccess settings on my local box with clean URLS (WAMP) it all works fine but the moment I use this on the live server (shared hosting LAMP) every page I navigate to displays the home page (the one under index I guess) even though the URL in the browser is clearly being updated.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/domain1.com/(.*)$
RewriteRule ^(.*)$ /domain1.com/index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/domain2.com/(.*)$
RewriteRule ^(.*)$ /domain2.com/index.php?q=$1 [L,QSA]
</IfModule>
Any help or ideas are very much appreciated.
Luke
Probably the best thing to do is to reproduce the problem on your local box and turn up RewriteLogLevel so you can see what's going on. (Since you usually can't change the log level on shared hosting)
You may be able to "simulate" the problem by doing a directory rewrite in your Apache main configuration. (The shared hosting obviously does its own rewriting before it gets to the .htaccess!) If you can't reproduce the problem, you may have to start trial-and-error debugging on the remote server. This is ugly but if it's your only option:
Use the R (redirect) flag in substitutions to send any rewritten URL back to your browser. Use TELNET (or an appropriate browser add-on) to inspect the HTTP responses.
Don't forget to escape dots in regexes!
As a side note, the RewriteRule pattern is matched before the RewriteConds above it. This kind of setup is probably better for performance:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/domain1\.com/(.*)$ /domain1.com/index.php?q=$1 [L,QSA]
# ^ should be escaped
Note that I haven't tested this.