mod-rewrite remove folder name from url - apache

I am using OJS and I have installed it inside a folder called "journals". I have created some journals there (for example "journal1", "journal2", etc).
Now I am getting paths like this: www.example.com/journals/index.php/journal1,
www.example.com/journals/index.php/journal2, etc.
What I want is to map www.example.com/journals/index.php/journal1
to looks like www.example.com/index.php/journal1 (removing the journal part from URL).
I can't move OJS to root because I have some other files there.
Here is the .htaccess file which I am using currently (it's in "journals" folder and giving me a 500)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^journals
RewriteRule ^(.*) /journals/$1 [L]
</IfModule>
Also Here is the error.log
[Fri Oct 12 22:16:45 2012] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

When you put those rules inside the htaccess file in your /journals directory, it's causing a rewrite loop. $1 never starts with journals because you're inside the journals directory, thus the rule keeps getting applied.
You'll need to put something like this in your htaccess file in your site-root, the / directory:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/journals
RewriteRule ^index\.php(.*)$ /journals/index.php$1 [L]

Related

If URI is a file link then prepend with folder else rewrite with index.php

Dir stracture
- dist
- js
- css
- index.php
- .htaccess
I don't want to move index.php to /dist because the dist files are build by Vue.js and excluded from git and etc.
I'm trying to get this working for hours with help of Google and Apache docs but failing.
My .htaccess for now:
RewriteEngine On
RewriteRule ^(app|dict|ns|tmp)\/|\.ini$ - [R=404]
RewriteCond %{REQUEST_URI} .(css|js)$
RewriteRule ^(.*.(css|js))$ /dist/$1 [QSA,L]
RewriteRule ^ index.php [QSA,L]
It rewrites all needed requests to the index.php, but js and css throws an error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at [no address given] to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
Apache logs:
[Fri Aug 16 23:10:06.973003 2019] [core:error] [pid 10716:tid 996] [client 127.0.0.1:2106] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
As the title says I want to prepend all the assets with \dist folder otherwise rewrite to index.php.
I looked at backtrace and this is what I've got after changes:
RewriteEngine On
RewriteRule ^(app|dict|ns|tmp)\/|\.ini$ - [R=404]
RewriteCond %{REQUEST_URI} ^(?!dist\/?).*.(css|js|map)$
RewriteRule ^((?!dist\/).*.(css|js|map))$ /dist/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
It works
The thing is
A RewriteRule loops when the target of the rule matches the initial rewrite pattern. For example, if you were to rewrite all URLs starting with "foo" to "foo.php", then "foo.php" would also get rewritten to "foo.php", and so on, forever.
You should look in the source if you ran in to this.

RewriteRule htaccess issue

i have live-cams.php file in root directory of my project and i try to rewrite link as /live-cams
RewriteRule ^live-cams ./live-cams.php [NC,L]
This rule throw
500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
also throw the same error when i try to open file /live-cams.php. Rule for index.php looks like to live-cams.php and it work well
RewriteRule ^home ./index.php [NC,L]
When i type friendly link as ^live-cams/ instead of ^live-cams it works.
Apache error log:
[Fri Aug 18 12:20:38.011915 2017] [core:error] [pid 14024:tid 1920] [client ::1:58474] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Anyone know whats is wrong?
You need to use anchors in your regex pattenrs:
RewriteRule ^live-cams/? live-cams.php [NC,L]
RewriteRule ^home/?$ index.php [NC,L]
Otherwise live-cams is part of your original and rewritten URIs thus causing mod_rewrite to keep executing your rules until it hits LimitInternalRecursion count.

RewriteCond: unknown flag 'QSA'

When configuring Restler it suggests creating the following re-write rule:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
I have put this into a .htaccess file in root of my REST directory but I'm getting a 500 Server error when this rule is being fired. The error message the apache error log is:
[Wed Oct 10 10:39:30 2012] [alert] [client 127.0.0.1] /public/api/.htaccess: RewriteCond: unknown flag 'QSA'
[Wed Oct 10 10:39:30 2012] [error] [client 127.0.0.1] File does not exist: /public/favicon.ico
I assume the lack of a favicon.ico file can be ignored but I am concerned about the "unknown flag 'QSA'" error. I know very little about rewrite rules so any help there would be appreciated.
For those familiar with Restler, I am using 3.0.0rc2 (if that matters). Also it's worth pointing out that using the explicit call to index.php works so much as I then get a 404 JSON error response (a positive improvement) but as indicated above if I rely on the rewrite rule then I just get a 500 Server Error:
http://localhost/api/index.php/say/hi - WORKS (gives JSON 404 error)
http://locahost/api/say/hi - 500 SERVER ERROR
It doesn't look like the error has anything to do with the rules that you have:
/public/api/.htaccess: RewriteCond: unknown flag 'QSA'
which says that the QSA flag is an unknown flag for a RewriteCond, followed by a favicon.ico not found so I'm guessing that it's unrelated to requests into your REST directory.
If you had AllowOverride None, then your htaccess file is simply going to be outright ignored. There are different aspects of configuration options that can be overridden inside htaccess files, None means, htaccess cannot override any configurations, so it'll just get ignored.
The QSA flag isn't needed at all here. You only need it when you are rewritting the query string and want to append the original query string to the end. By default, the query string is appended anyways.
Interesting ... I was looking into my JSON 404 message and found this post:
Restler always returns not found
It not only solved my 404 error but also helped to fixed the 500 Server error. The only thing I needed to do was change the http.conf's AllowOverride to All.
I'd still be interested if anyone could tell me WHY this is happening but the this does now work.

How to fix infinite loop in this mod_rewrite rule?

I've got the following in an .htaccess. file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/css(/?|/.+)$
RewriteCond %{REQUEST_URI} !^/js(/?|/.+)$
RewriteCond %{REQUEST_URI} !^/img(/?|/.+)$
RewriteRule !^/handle\.php$ /handle.php [L]
I simply want all requests (except those beginning with /css/, /js/, and /img/) to be sent to handle.php.
But when I make a request I get a 500 error, and this is printed in the error log:
[Sat Mar 24 16:14:53 2012] [error] [client x.x.x.x] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Why is it getting in an infinite loop?
It's probably that rewriting a request to /handle.php does not match !^/handle\.php$, so it tries to rewrite the rewrite, etc. Your issue might be the leading slash? Try playing around with that.
If not, check out the RewriteLog directive (and see the entry just below that, RewriteLogLevel). You'll be able to see what rewrites are being attempted, and you'll likely be able to figure out what's going wrong.

mod_rewrite To Direct Subdomain Access

We are setting up an API and we want to use Apache mod_rewrite to direct all accesses to http://api.domain.com to the script located at /cgi-bin/api.pl. Here is what we're currently using:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^api.domain.com$ [NC]
RewriteRule ^(.*)$ /cgi-bin/api.pl [NC,L,QSA]
However, this is giving us the following error:
[Fri Sep 03 14:18:32 2010] [error] [client 67.180.34.0] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.
[Fri Sep 03 14:18:32 2010] [error] [client 67.180.34.0] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
If we try to access http://domain.com/cgi-bin/api.pl the script functions properly. What are we doing wrong? Please help! Thanks in advance.
I assume that you're defining your rules in a .htaccess file, where the L flag might not work like you were expecting.
Since your test pattern ^(.*)$ matches whatever input is given to it, the URL you rewrite to is also matched on the subsequent request, and you get an internal infinite redirection loop (resulting eventually in the server error).
A solution is to check to see if you've already rewritten the URL:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/cgi-bin/api.pl
RewriteCond %{HTTP_HOST} ^api.domain.com$ [NC]
RewriteRule ^.*$ /cgi-bin/api.pl [NC,L,QSA]