.htaccess addon domain issue gives 500 error - apache

This is my .htaccess located at public_html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I have a (legacy) CakePHP app running in public_html/app for maindomain.com
I added an addon domain otherdomain.com under public_html/otherdomain.
However, when trying to load the index page it returns a 500 server error.
Removing the public_html/.htaccess permits browsing otherdomain.com correctly, but the CakePHP app (maindomain.com) then has routing issues.
This is the CakePHP .htaccess under public_html/app:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>

it appears that your main htaccess is redirecting traffic to your app/webroot/ which is causing problems for otherdomain.com.
Maybe you can check the hostname that is entered and redirect based on that.
Something like this.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^maindomain\.com [NC]
RewriteRule ^(.*)$ app/webroot/$1 [L]
RewriteCond %{HTTP_HOST} ^otherdomain\.com [NC]
RewriteRule (.*) http://otherdomain.com/$1 [L]
</IfModule>

Related

Having a issue rewriting URLs via the htaccess file

I'm currently attempting to rewrite URLS for my employer's website for hopefully better results in SEO. However, I can't seem to get the url rewrite to work.
I have used this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^myemployerssite.com.au$
RewriteRule (.*) https://www.myemployerssite.com.au/$1 [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 https://www.myemployerssite.com.au/page-not-found.php
RewriteRule ^services/civil-construction$ /services.php?serviceid=1 [L]
</IfModule>
I expected, for example, the first service page which is civil construction to have it's URL to be rewritten from https://www.myemployerssite.com.au/services.php?serviceid=1 to https://www.myemployerssite.com.au/services/civil-construction
But the URL remains as https://www.myemployerssite.com.au/services.php?serviceid=1
The redirect from non-www to www is working as intended as is the redirect from to http to https.
Some advice would be appreciated
I changed the order of the rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^services/civil-construction$ /services.php?serviceid=1 [L]
RewriteCond %{HTTP_HOST} ^myemployerssite.com.au$
RewriteRule (.*) https://www.myemployerssite.com.au/$1 [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
ErrorDocument 404 /page-not-found.php
Try changing this line
RewriteRule ^services/civil-construction$ /services.php?serviceid=1 [L]
to
RewriteCond %{QUERY_STRING} serviceid=1$
RewriteRule ^services.php$ /services/civil-construction [L,R]

How to redirect a www url to non-www url using 301 permanent redirection?

My website is running on virtualmin control panel's virtual server currently it is redirecting 302 temporary URL while I have added redirection rule in .htaccess file.
Here is my .htaccess file configuration:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ index.php
RewriteRule ^(([a-zA-Z0-9-_]+)?)\.html$ page.php?pagId=$1
RewriteCond %{http_host} !^example.com$ [nc]
RewriteRule ^(.*)$ https://example.com/$1 [r=301,nc,L]
Kindly let me know what changes I need to make in my web server so it redirects correctly 301 permanent redirection?
I want to redirect all requests for my domain in this URL format "https://example.com/".
You can use:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^index\.html$ index.php [L,NC]
RewriteRule ^([\w-]+)\.html$ page.php?pagId=$1 [L,NC,QSA]
I have resolved the issue by writing two rewrite rules. One in each vhost
i keep the same in .htaccess file
>
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^index\.html$ index.php [L,NC]
RewriteRule ^([\w-]+)\.html$ page.php?pagId=$1 [L,NC,QSA]
And in /etc/httpd/conf/httpd.conf file
>
<VirtualHost*:80>
.
.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
</VirtualHost>
<VirtualHost*:443>
.
.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
</VirtualHost>
Now it's working fine.
Hope this may help someone.

Htaccess ( or + Cakephp)

I would like to know how can I configure a subdomain to a website.
Example
www.domain.com
subdomain.domain.com
How can I configure the htaccess to access the folder subdomain and use it
public_html
app
lib
plugins
vendors...
subdomain
app
lib...
this is my htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
thanks
in public_html/.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !subdomain/
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{REQUEST_FILENAME} !subdomain/
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

htaccess 301 redirect to different domain

Trust me, I've searched all over for a solution to my problem, so I don't believe this is a duplicate question.
I need to do the following:
301 Redirect blog.example.com to blog.newexample.com.
What I have is close to working. When I go to the homepage of blog.example.com it will redirect perfectly to blog.newexample.com. However, if I go to blog.example.com/directory/page it does not direct me to blog.newexample.com/directory/page.
Here is what my .htaccess looks like:
# 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]
RewriteCond %{HTTP_HOST} !newexample.com$ [NC]
RewriteRule ^(.*)$ http://blog.newexample.com/$1 [L,R=301]
</IfModule>
# END WordPress
I'm not sure to understand what your problem is, but this works just fine :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newexample.com$ [NC]
RewriteRule ^(.*)$ http://blog.newexample.com/$1 [L,R=301]
</IfModule>
The URL http://website.com/hello/world/ redirects me to
http://blog.newexample.com/hello/world/.

Symfony2 - Rewrite url to add www prefixwith existing .htacess file

Symfony2 core configuration comes up with the following .htacess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I understand that this will erase the app.php from the url in the browser.
I would like to add the following rewrite rule to add www. to my WebSite everytime a user enter the domain name without the www. :
RewriteCond %{HTTP_HOST} ^valutao\.com$ [NC]
RewriteRule ^(.*)$ http://www.valutao.com/$1 [L,R=301]
So now i have the following .htacess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
RewriteCond %{HTTP_HOST} ^valutao\.com$ [NC]
RewriteRule ^(.*)$ http://www.valutao.com/$1 [L,R=301]
</IfModule>
My issue is that app.php will show up again. It seems that my rule just hides the base configuration. Here is an example of what is displayed in the url when I enter valutao.com : http://www.valutao.com/app.php
Does someone know how to fix that. Unfortunately I have just basic knowledge with the rewrite rules.
Thanks for help
You simply need to change the order (www rewrite should go first).
I'd also make it less dependent on the domain name:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>