Opencart .htaccess - allows access to specified folder - apache

I've got an OpenCart site, but need to create an addtional /test directory which is accessible.
By default, all requests are sent to OpenCart. I'm trying to make it send all except /test. I've tried a few suggestions on this site already, but either nothing happens, or I get a 500 Internal Server Error.
This is the rewrite part of the OpenCart provided .htacccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
I think I need a line like the following after RewriteBase, but can't get anything working yet.
RewriteCond %{REQUEST_URI} !^/test
Thanks!

I think
RewriteCond %{REQUEST_URI} !^test/(.*)
should do it

RewriteBase /YOUR_SUB_DIR/
replace 'YOUR_SUB_DIR' with yours

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).

Trying to debug .htaccess file

I am getting started with the mod_rewrite function for my PHP webpage
I was superexcited to have created my first rewrite rule, but when I added to my .htaccess file, I got error 500 for all webpages (not only the one I am trying to redirect).
Here is my file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^([^/]*)/(([^/]*)/)*$
RewriteCond %{REQUEST_FILENAME}index.php !-f
RewriteCond %{REQUEST_FILENAME}index.html !-f
RewriteCond %{REQUEST_FILENAME}index.htm !-f
RewriteRule ^/([a-zA-Z\-]+)\/([a-zA-Z\-]+)\/([0-9]+)$ ficha_jugador.php?idjugador=$3 [L]
RewriteRule (.*) /index.html [L]
My new line is 8th, the rest came by default in my webserver
I tried something much easier for line 8th just to test
RewriteRule ^writetest\.html http://www.after5pc.net [QSA,L]
But it also ended up in 500 error, so it does not seem a syntax problem
Eventually, what I want to do is to change (slug my URLs)
http://cadistas1910.com/ficha_jugador.php?idjugador=1304
by
http://cadistas1910.com/alvaro-garcia-canto/alvaro-garcia/1304
Any help you guys can provide? Thanks a lot!
I believe last line of your .htaccess file could be the culprit, could you please try following once.
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z\-]+)\/([a-zA-Z\-]+)\/([0-9]+)$ ficha_jugador.php?idjugador=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
I have added a condition there to rewrite to index.html if and only if Uri has non existing directory/file, why because it will create an infinite loop without a condition, since you are rewriting everything to index.html and everything(by regex) matched index.html also, hence loop and 500 internal error you are getting.
Also your rule to rewrite from http://localhost:80/alvaro-garcia-canto/alvaro-garcia/1304 TO http://localhost:80/ficha_jugador.php?idjugador=1304 have been changed into a single line above.
NOTE: To fix this either make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

RewriteRule doesn't rewrite correctly

I'm hosting my development site on the localhost server and I used to access it at 127.0.0.1/dev. In the folder I have a .htaccess file containing following information. I'm new with the RewriteEngine and don't get this work.
RewriteEngine on
RewriteRule ^/(.*)$ /index.php?page=$1
When I'm trying to access 127.0.0.1/dev/home, I simply get a message that the page doesn't found. I can't see where rewrite is redirecting me, so I can't debug the problem in easy way. I think that you can see the problem at the first look.
Thanks in advance.
Try this rule in /dev/.htaccess without leading slash in source pattern and target URL:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?page=$0 [L,QSA]

htaccess Rewrite to same name file in different folders of site

I tried to search a lot but got no success. I wish that someone could really help me out with this.
Ok, so my website is like:
/rewrite.php
/folder1/rewrite.php
/folder2/rewrite.php
/folder1/subfolder1/rewrite.php
/folder1/subfolder2/rewrite.php
and so on...
In short, there exist a rewrite.php in each and every folder(except admin access folder).
So basically, I want to rewrite to the rewrite.php file in the corresponding directory, but I am not able to do it.
That is
If I request
/News
then it should rewrite it to:
/rewrite.php
If I request:
/folder1/News
then it should rewrite it to:
/folder1/rewrite.php
and so on for other folders.
Currently, this is what I have in my htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . rewrite.php [L,NC]
Somewhere, I read that If I remove RewriteBase, then it uses the current directory as RewriteBase. But it failed and didn't worked.
Also, I would like to keep some folders in exceptions(in which Rewriting should be disabled) like:
/administration
/config
I hope for a feasible solution, if anyone can help me... :)
Best Regards,
Ankur Thakur
Try:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(administration|config|rewrite.php)
RewriteRule ^(.*?)(/?[^/]+)$ $1/rewrite.php [L]

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.