removing file extension with htaccess failing - .htaccess

i'm using an htaccess script trying to remove the .php testing the .htaccess on a testing server it runs fine, but on the live server that is a different host it trys rewriting the file based on the absolute path and the rewrite fails
here is the htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
this is taking a url like this www.example.com/services
and trying to point it to /n/c/example.com/public/service.php
I know the {REQUEST_FILENAME} is suppose to be pulling the full local system path, but i don't understand why it's not finding the file. i know very little about htaccess and mod_rewriting so i'm not really sure what I should try to make it base everything off of just the url path, or if there is a better solution. I'm really open to suggestions.
Any help would be greatly appreciated.
Thanks

Use RewriteRule .* %{REQUEST_URI}.php [L]
It is hard to tell why your rule did not worked for you by having so little info about your Apache setup and any other rewrite rules that you may have.
Quite possible that the [L] flag did the trick for you -- you may have other rewrite rules that were rewriting this URL further, producing incorrect result in the end. I don't think that %{REQUEST_URI} did such a big job on its own, unless you have some symbolic links / aliases or even some transparent proxy in use which could make a difference.
Keep in mind, that the rules you have shown in your question cannot generate this sort of URL to be visible in browser's address bar (example.com//service.php/) -- it has to be a redirect (3xx code) involved .. which suggests that you have other rules somewhere.
Most likely it is a combination of your Apache specific settings & combined rewrite rules logic (where the L flag can make a big difference depending on those other rules).
The only way to give more precise answer will be enabling rewrite debugging and analyzing how rewrite was executed and what was involved.

Have you enabled mod_rewrite on the other server? AddModule mod_rewrite, I think.
Also - more likely - have you enabled .htaccess? You would need to have
AllowOverride All
or
AllowOverride FileInfo
for that.
These directives will need to go in the apache config files (usually /etc/httpd/conf/httpd.conf or one of the files in /etc/httpd/conf.d), and you will need to restart apache to get them to take effect.

Related

How do I remove a folder from a URL?

First of all, this question has been asked a few times on stack, however, none of the answers seem to work for me.
I have a website which has a "pages" folder in the root, I want to store all of my website pages in there.
Here's my structure
/pages/folder/folder2/index.php
I want to make it so the link displays:
https://wwww.website.com/folder/folder2/index.php
Removing the "/pages/" part of the URL, when I try all of the answers suggested previously, I get a 404 error.
Here is the htaccess I'm using
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^pages(/.*|)$ $1 [L,NC]
</IfModule>
and i also tried:
RewriteEngine On
RewriteRule ^pages/(.*)$ /$1 [L,R=301]
This htaccess is in the root. I can't seem to get it working, can anyone offer any suggestions? Thank you!
Your second attempt looks fine, though it can be imporoved a bit:
RewriteEngine On
RewriteRule ^/?pages/(.*)$ /$1 [R=301]
That rule should work inside the http servers host configuration or in some dynamic configuration file (".htaccess" style file) if the http server's rewriting module is loaded. You definitely should prefer the first option, but if you really need to use a dynamic configuration file then take care that the interpretation of such files is configured at all and that the file is placed in your hosts's DOCUMENT_ROOT folder and is readable for the http server process.
If that does not work then you need to start debugging. In this case you will start by monitoring your http server's error log file while making test requests. If no hint appears in there you need to enable rewrite logging which allows you to learn what exactly is going on inside the rewriting engine. See the official dpcumentation for the apache rewriting module for that. As typcial for OpenSource software it is of excellent quality and comes with great examples.

Redirecting URLs using RegExp and Mod Rewrite

While upgrading Magento to 1.9.3.4 it overwrote the .htaccess file and now I am left with 2000+ broken links that I had manually created.
Since the old product URLs had a random number at the end and now they don't have so I am looking for a way to redirect them.
Old URLs Structure:
http://example.com/category-1/subcategory/product-name-1421.html
http://example.com/category-2/subcategory/product-name-5421.html
http://example.com/category-3/subcategory/product-name-5891.html
New URLs Structure:
http://example.com/category-1/subcategory/product-name.html
http://example.com/category-2/subcategory/product-name.html
http://example.com/category-3/subcategory/product-name.html
I know I can use RegExp something like RewriteRule ^category/subcategories/(.+?)(-[0-9]+)?$ category/subcategories/$1 [L,R=301] but I couldn't get it working.
I'd say this probably is what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/subcategory/(.+?)-\d+\.html$ /category/subcategory/$1.html [R=301]
For this you obviously need the rewriting module activated in your http server. And in case you want to use a dynamic configuration file (see below...) you need to enable that too (see the AllowOverride directive).
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

htaccess redirect with multiple parameter

I am using following htaccess rewrite for clean URL:
RewriteRule ^([^/]+)/?$ category.php?cat=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ subcategory.php?cat=$1&subcat=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ item.php?cat=$1&subcat=$2&item=$3 [L,QSA]
And I get clean URL like:- domain.com/SHOE/MEN/ITEM-NAME
Now I want change URL to be:- 1. domain.com/BOOTS/MEN/ITEM-NAME
and similar changes at parent level like: 2. domain.com/BOOTS/MEN and 3. domain.com/BOOTS
I tried with one additional line
RewriteRule ^SHOE/.*$ /BOOTS/$1 [L,R]
But still not working. Sorry If I made it complex. Thank you for help in advance.
Your question is not really clear, but I assume you want to redirect incoming browser requests to URLs using BOOTS instead of SHOE, whilst keeping the rest of the requested URL as it is...
For that try something like that:
RewriteEngine on
RewriteRule ^/?SHOE(.*)$ /BOOTS$1 [R=301,END]
That rule should be placed at the beginning of a possible series of rewrite rules. If you are using an old apache http server you might have to use the L flag instead of END. That also means you might have to take additional measures to prevent an endless rewriting loop.
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

using mod_rewrite to strip out junk

We're seeing some really weird URLs in our logs and I've been told to start redirecting them.
I know of a couple of better ways to go about fixing this, but the boss wants it done this way. I apologize in advance.
We're seeing stuff like the following in our logs:
http://www.example.com/foo/bar/bla&ob=&ppg=&rpp=100&ob=&rpp=&ppg=&rpp=30&ppg=&ppg=1&rpp=10&rpp=50&ob=&ob=&ob=&rpp=40&ob=&rpp=5&rpp=30&rpp=&rpp=20&order_by=&results_per_pge=75
I've been told to 'toss some mod_rewrite rules in the .htaccess file' to take this and strip out all the ob, rpp, and ppg variables.
Now, I've found ways to strip everything out. And that wouldn't be too bad if I could leave the /foo/bar/bla in there. But I can't seem to do that. Basically, any help would be appreciated.
Try:
# strip out any params that's ob=, rpp= or ppg=
RewriteRule ^/?(.*)&ob=([^&]*)&(.*)$ /$1&$3 [L]
RewriteRule ^/?(.*)&rpp=([^&]*)&(.*)$ /$1&$3 [L]
RewriteRule ^/?(.*)&ppg=([^&]*)&(.*)$ /$1&$3 [L]
# if everything's gone, finally redirect and fix query string
RewriteCond %{REQUEST_URI} !&(ob|rpp|ppg)
RewriteRule ^/?(.*?)&(.*) /$1?$2 [L,R=301]
The problem here is that your URL:
http://www.example.com/foo/bar/bla&ob=&ppg=&rpp=100&ob=&rpp=&ppg=&rpp=30&ppg=&ppg=1&rpp=10&rpp=50&ob=&ob=&ob=&rpp=40&ob=&rpp=5&rpp=30&rpp=&rpp=20&order_by=&results_per_pge=75
has A LOT of ob=, rpp=, and ppg= in the URI. More than 10. That means you'll get a 500 internal server error if you use these rules against that URL. By default, apache has the internal recursion limit set to 10, that means if it needs to loop more than 10 times (and it will for the above URL), it'll bail and return a 500. You need to set that higher:
LimitInternalRecursion 30
or some other sane number. Unfortunately, you can't use that directive in an htaccess file, you'll need to go into server or vhost config and set it.

Stop mod_rewrite returning REQUEST_URI when (.*) is empty

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^mocks/site/(.*)$ http://thelivewebsite.com/$1 [R=301,L]
That is my htaccess file's contents.
The htaccess file is in the root directory of the hosting account and I just want to redirect the directory mocks/site/ to the new domain (with or without any extra directories).
eg: if someone goes to http://mywebsite.com/mocks/site then it needs to redirect to http://thelivewebsite.com. If they go to http://mywebsite.com/mocks/site/another/directory then it needs to redirect to http://thelivewebsite.com/another/directory. I hope that makes sense.
So the problem I have is that the htaccess code above seems to work pretty well when there is something after mocks/site/ however when there isn't something after that then the $1 in the redirect seems to reference the whole REQUEST_URI (eg: mocks/site/ rather than nothing - as there is nothing after it).
I don't know how to stop this. I thought about using a RewriteCond, but I'm not sure what to use there. I can't find anything that helps me to determine if there is anything after mocks/site/ or not.
Any help will be much appreciated.
Thank you.
That's very strange behaviour -- never seen anything like that. Therefore I think it could be something else (another rule somewhere -- on old or even new site). I recommend enabling rewrite debugging (RewriteLogLevel 9) and check the rewrite log (that's if you can edit Apache's config file / virtual host definition).
In any case, try this combination:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^mocks/site/$ http://thelivewebsite.com/ [R=301,L]
RewriteRule ^mocks/site/(.+)$ http://thelivewebsite.com/$1 [R=301,L]
It will do matching/redirecting in 2 steps: first rule is for exact directory match (so no $1 involved at all) and 2nd will work if there is at least 1 character after the /mocks/site/.
Alternatively (Apache docs even recommending this one) use Redirect directive (no need for mod_rewrite at all for such simple redirects):
Redirect 301 /mocks/site/ http://thelivewebsite.com/

Resources