how to rewrite nextcloud's url - mod-rewrite

I have set up docker infrastructure with LAMP and nextcloud 16 and 17 installed. Based on the domains Apache is redirecting request to specific directory what produce 500 error in nextcloud.log (not in apache):
The error message is:
{"reqId":"3Kfv593PUs4Kc43ybuXH","level":3,"time":"2019-10-23T16:09:02+00:00","remoteAddr":"172.18.0.2","user":"--","app":"index","method":"GET","url":"/index.php","message":{"Exception":"Exception","Message":"The
requested uri(/) cannot be processed by the script
'/domains/nextcloud.domain.com/index.php')","Code":0,"Trace":[{"file":"/www/apps/nextcloud-16.0.5/lib/base.php","line":918,"function":"getRawPathInfo","class":"OC\AppFramework\Http\Request","type":"->","args":[]},{"file":"/www/apps/nextcloud-16.0.5/index.php","line":42,"function":"handleRequest","class":"OC","type":"::","args":[]}],"File":"/www/apps/nextcloud-16.0.5/lib/private/AppFramework/Http/Request.php","Line":780,"CustomMessage":"--"},"userAgent":"Mozilla/5.0
(X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/77.0.3865.120 Safari/537.36","version":"16.0.5.1"}
My rewrite rule in .htaccess:
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteCond %{REQUEST_URI} !^/domains/ [NC]
RewriteRule ^(.*)$ domains/%1/$1 [L]

Related

Laravel API routes 404 error using Heroku Apache

I am setting up my Laravel API on Heroku. Everything seems to be working fine, I am able to go to the site and see the Laravel public page. But when I try to access my API routes (signup, login, etc.), it returns a 404 error.
The routes work fine on my own virtual hosts that I created but once I uploaded it to Heroku, all the routes except for "/" return 404. I've checked to see if "index.php/" would work but still returns a 404.
public/htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_URI} 1^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Procfile
web: vendor/bin/heroku-php-apache2 public/
routes/api.php
$api = app('Dingo\Api\Routing\Router');
$api->version("v1", function ($api) {
$api->get("/", function () {
return response()->json([
'status' => 'success'
], 200);
});
$api->post("/signup", "MusicShare\Http\Controllers\AuthController#signup");
})
Heroku logs
2019-06-16T23:04:37.778061+00:00 app[web.1]: 10.35.223.16 - - [16/Jun/2019:23:04:37 +0000] "GET /index.php/routes HTTP/1.1" 200 47843 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36
My Heroku logs do not return an error. Everything shows the status code as 200 but I still get a 404 in Chrome if I try to access "/api/signup" or "/signup" or "/index.php/signup"
if you are using apache LAMP server try this code
go to the conf file and edit this
i am using // for comminting for you
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory "/var/www/html"> // here you project path
Options Indexes FollowSymLinks
AllowOverride All // alow all permissions
Require all granted
</Directory>
and server restart
for this command sudo service apache2 restart
then try this command for rewrite
sudo a2enmod rewrite
and your sub routes working perfictlly
dont't forgot this command sudo a2enmod rewrite
You have to edit the config vars --- added API_PREFIX and API_DOMAIN into the Heroku settings for my site.

mod_rewrite does not hide subdirectory

On my server I have multiple domains.
RewriteEngine On
RewriteBase /
# 1. Redirect multiple domains to http://domain.de/
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.de [NC]
RewriteRule ^/?(.*) http://domain.de/$1 [L,R,NE]
# 2. Redirect all requests to /subdirectory
RewriteRule ^$ /subdirectory [L]
The 2. rule is working correctly, but it is not hiding the subdirectory in the url nor does it work as intended: a request for http://domain.de/content/image.png returns 404, because the actual file is located in http://domain.de/subdirectory/content/image.png
Furthermore, I have some folders with tools located aside the subdirectory /subdirectory. I want to make sure I can still access them. This is currently working.
Question
How can I make sure, that the request for http://domain.de/content/image.png works?
What I tried
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteRule (.*) /subdirectory/$1 [L]
But that just returns error 500 with the following entry in the apache error log: `Request exceeded the limit of 10 internal redirects due to probable configuration error.
Edit
After guidance provided by Ravi Thapliyal, there is (I guess) one thing remaining: Removing the subdirectory from the url.
[user#domain html]$ curl -I domain.de/
HTTP/1.1 301 Moved Permanently
Date: Mon, 21 Oct 2013 12:42:22 GMT
Server: Apache/2.2.22 (Ubuntu)
Location: http://domain.de/subdirectory/index.php
Vary: Accept-Encoding
Content-Type: text/html
This is what gets returned, but I actually want to get the html not a location header which then of course redirects me externally to the subdirectory, which is then visible to the user. Might it have something to do with another .htaccess file from a subdirectory?
Edit2
Seems the problem is related to the typo3 installation behind the subdirectory. The accepted answer works as expected.
Your first rule should do an external redirect (changing the domain internally won't matter at all)
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.de [NC]
RewriteRule ^/?(.*)$ http://domain.de/$1 [R=301,L,NE]
Your second rule is not required. The new rule would cover root / request as well.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{REQUEST_URI} !^/subdirectory [NC]
RewriteRule ^(.*)$ /subdirectory/$1 [L]
The two RewriteConds on %{REQUEST_FILENAME} would make sure you can access any files -f or directories -d present outside /subdirectory.
Basically, the condition %{REQUEST_FILENAME} !-d prevents redirection if the URL path points to any existing directory. This prevents a URL like /existing-directory to be redirected to /subdirectory/existing-directory.
But, this could also prevent a root URL / request which is why you received a directory index forbidden error. Hence, the above condition is [OR]'d with %{REQUEST_URI} ^/?$ to allow / to be redirected to /subdirectory as well.

mod_rewrtie remap and redirect

what i am trying to do is this:
If url http://example.com/foo/foo.php is called it should be redirected with 301 to http://example.com/bar/foo.php
AND
if url http://example.com/bar/foo.php is called it internally calles the /bar/foo.php script but browser url is not changed (remap).
my rules look like this
RewriteRule ^foo/foo.php(.*) bar/foo.php$1 [R=301]
RewriteRule ^bar/foo.php(.*)$ foo/foo.php$1 [PT]
But this gives me too many redirects error.
Each rule activated separately works but together they seem to conflict...
You can either match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo/foo.php
RewriteRule ^foo/foo.php(.*) bar/foo.php$1 [R=301]
RewriteRule ^bar/foo.php(.*)$ foo/foo.php$1 [L]
Or prevent rewrite looping altogether:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^foo/foo.php(.*) bar/foo.php$1 [R=301]
RewriteRule ^bar/foo.php(.*)$ foo/foo.php$1 [L]

zend url rewrite problem with .htaccess on windows 2003

I am having a problem setting up my Zend framework project.
I have xampp/htdocs/quickstart
my server name is mydomain.com. I want to access my project at https://mydomain.com/quickstart(note that i am using https, dont think if this could be a problem)
i used the following .htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
And i am getting a 404 error , why does this happen?
I am sorry, i am no expert in configuring .htaccess. All i want to do is route all the requests in quickstart to quickstart/public. and to access my zend project at https://mydomain.com/quickstart/
i am getting a 404 error with above .htaccess(found on Internet)
My problem is is i already ahve other apps running on the server and i want to build a project on /quickstart. i also cannot create a subdomain. Can you please help me with proper .htaccess config.
Try with this,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Mapping a secondary domain to a subdirectory using mod_rewrite

I am looking to map a secondary domain to a subfolder on my document root.
For example, if requests to the domain www.example.com map to my DocumentToot, then requests to www.exampletwo.com go to /sites/files/.
I am unable to accomplish a redirect from www.exampletwo.com/index.html to www.exampletwo.com/sites/files/index.html while making the URL still display www.exampletwo.com/index.html. Any ideas?
I believe you're looking for something like this:
RewriteCond %{HTTP_HOST} ^(www\.)?exampletwo\.com [NC]
RewriteRule ^/(.*) /sites/files/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^/(.*) http://www.exampletwo.com/$1 [L,R]
RewriteCond %{HTTP_HOST} ^(www\.)?exampletwo\.com [NC]
RewriteRule ^/(.*) http://www.exampletwo.com/sites/files/$1 [L,P]
The P flag uses the proxy module, therefore the url is not changed (no redirect) on the client.

Resources