Apache htaccess cache - apache

Using XAMPP and Windows 10 I'm experimenting with some htaccess rules.
I have a folder C:\xampp\htdocs\website and in this directory there is a .htaccess file, the full contents are;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
To test the .htaccess was working I added the following redirect rule to the end;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RedirectMatch 301 ^(.*)$ http://www.example.com
This successfully redirected my whole domain, great.
I have since deleted this line from my htaccess file, however my domain is still being redirected.
I have restarted Apache numerous times, cleared my browser history, cache and tried different browsers. I have also tried viewing the site in incognito mode.
Eventually the site returns to normal, this is after numerous service restarts and history deletions.
Is this the expected behavior? Is there a more efficient way of testing the .htaccess?
Any advice is appreciated.

No need to restart APache. Simple make the change in the htaccess file, clear your history/cache and hard reload the page (Ctrl+F5).
You can also close and re-open the browser, see if that helps.

Related

Started implementing .htaccess but when navigating from the reduced URL, when going into a directory it then can't get back

So I have included an htaccess file to my server in the root directory and changed all of the ./ I could find set the absolutes.
However, when I search by URL into one of the directories pressing the home button does not take me home. Instead, it appends the index onto the end:
/website/book/index.php?p=home
Instead of
/website/index.php?p=home
Where have I made a fuddle?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ https://%{HTTP_HOST}/paperbound/$1 [R=301,L]
RewriteRule ^([^/\.]+)?$ index.php?p=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?p=$1&id=$2 [NC,L]
</IfModule>
Is the htaccess used. https://sitehost/website/book/2 is URL entered and page retrieved which exists as https://sitehost/website/index.php?p=book&id=2, clicking navlink to return to https://sitehost/website/index.php?p=home, instead places https://sitehost/website/book/index.php?p=home into the URL bar and returns an error as the file does not exist.
With your shown samples/attempts, please try following htaccess rules file. Make sure to keep your index.php file is present in website folder and htaccess is present along side with website folder(not inside it).
Please make sure to clear your browser cache before testing your URLs.
##Enabling rewrite engine here.
RewriteEngine ON
##Checking conditions for non-existing pages here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##performing internal rewrite here to index.php file.
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ $1/index.php?p=$2&id=$3 [QSA,L]

CodeIgniter htaccess for subdomain without changing URL

I have been searching this but could not find the answer. I might have missed something here. Basically I have two applications under one CI installation. Each of the app will have their own subdomain. So for example http://foo.example.com/ which will redirect to http://example.com/foo and http://bar.example.com/ which will redirect to http://example.com/bar. The http://example.com/ itself will be some kind of a landing page.
The redirect part is working fine. But it keeps changing the URL from http://foo.example.com/ to http://example.com/foo, which I want to prevent. I have read a solution to use a [P] instead of [L] flag, but that will require me to enable proxy.
Is there any other option for me to do this other than enabling proxy?
Currently my htaccess file is as below:
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# The line for subdomain
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,NC,QSA]
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
Thank you very much for the response.

htaccess file not working correctly after Migrating to new host

I've just migrated web hosting and the .htaccess file isn't functioning correctly on the new host.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^(.*)$ index.php?page=$1&%{QUERY_STRING}&param=%{REQUEST_URI}
The front end of the website works perfectly but when I attempt to access the admin area http://www.website.com/Manage the url is getting rewritten even through the Manage directory is available for the web server to read. When I removed the rule the admin area works again.
I attempted to add a rewrite condition to exclude the admin directory but didn't have any luck.
Any help would be appreciated.
The following doesn't only check if the requested url is a existing file, it also checks if it's a folder.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1&param=%{REQUEST_URI} [QSA]
PS I also cleaned up your rule a bit.

Remove ".html" from URL via .htaccess for a WordPress website

Background information:
I've searched stackoverflow for a specific solution and couldn't find one that fixed my situation. Thanks in advance for any help you can offer. Your knowledge is appreciated.
I've decided to accept a contract to "convert" (in the client's words) a Joomla site into a WordPress site. Everything is going along smoothly, except that the Joomla site links to .html files, both in its navigation and in the content of 100+ posts.
Instead of going through each post one-by-one and updating the links or running a SQL command to remove ".html" from URLs, I've decided to put the pressure on .htaccess, with which I am somewhat comfortable.
What I'm trying to do ↓
In WordPress, I have custom permalinks enabled, and it is as follows:
/%category%/%postname%
Here's an example of what one of the old URLs in the posts looks like:
http://the-site.com/category/the-webpage.html
I need the htaccess file to tell the webserver to remove the .html so the user, after visiting "http://the-site.com/the-webpage.html" is instead sent to:
http://the-site.com/category/the-webpage
I'm setting up the page stubs to follow the file name of the Joomla pages, so http://the-site.com/category/the-webpage will work.
My question:
Can you help me discover the solution to removing .html from the URL when someone visits the site, even if the HTML file doesn't exist on the server?
Here's how the .htaccess file looked before I made changes:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Here's the latest .htaccess file as of 5:35pm Eastern:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
The ↑latest .htaccess changes work. Thanks Tim!
This will work to force an external redirection to your new URLs, but this may not be ideal for your situation. I'm still trying to think if there's a way to keep the redirection internal and update the variable that WordPress uses to determine which page to serve up, but so far I haven't thought of anything that would work.
Entire .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You want to use a URL rewrite
RewriteEngine On
RewriteRule ^(.*)\.html$ $1
This should do it. It will rewrite a request to site.com/category/whatever.html to site.com/category/whatever. it shouldn't be dependent upon the requested file existing.
<Directory /var/www/category>
RewriteEngine on
RewriteRule (.*)\.html$ /category/$1
</Directory>
This is the format for apache2.conf or virtual host files. Not sure if you use the command in .htaccess. It's best to take care of it in the server conf, if you can, as that is only parsed once, on server startup, and htaccess is parsed on each request.

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.