htaccess redirect on mvc framework - php

I am working on the htaccess file for my mvc site. The software that the company purchased for the site works only without the www, so I was able to fix up the htaccess to allow www in the URL since most of our affiliates are going to try to use it anyway. However, this renders the siteurl.org/index.php/admin and the siteurl.org/index.php/members unreachable. I'm trying to exclude these URL's from the www forward to non-www but everything I know and can find seems to relate to non-MVC sites, and it seems that mvc sites are set up differently across the board so the examples I'm finding aren't working for me.
Here's my current htaccess (I had to comment out the forwarding lines to allow admins to access the admin section and affiliates to access the member section)
<Files ~ "serial.txt$">
Order allow,deny
Deny from all
</Files>
RewriteEngine On
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^aff/(.*)$ /index.php/aff/?aff=$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [R=301,NC]
If it could be addressed at the same time, I'd also love a pointer on how to clean up that URL so that we could type in siteurl.org/admin as opposed to siteurl.org/index.php/admin (same for members), and also to show the affiliate name in the URL (it's currently cleaning up the URL to remove the /aff/affiliateusername but affiliates would like to see their name in the URL). If anybody has a great link to specific resources on writing htaccess for MVC I would be eternally grateful. Thank you in advance for any assistance.

Let's be clear on three things before going into explanations:
Apache doesn't have a care in the world whether your site is built on the MVC approach/design pattern or not. It. Doesn't. Even. See. It. To it, it sees htaccess mod_rewrite rules.
Whoever puts a serial code in serial.txt on the root is just begging for it to get nicked using a file include vulnerability in PHP.
This is suspiciously similar to CodeIgniter in rewrite rules.
Now. Your rules:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This rule will match only if the http host does not start with www. . If this is true, then it'll redirect to the http ://www. version.
Based on your description, you want the opposite: Your CMS does not work with www.. So, you will want this:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ http://mydomain/$1 [R=302, L]
Note that you'll need to hardcode your domain in there. Sucks.
Next set of rules:
RewriteRule ^aff/(.*)$ /index.php/aff/?aff=$1 [R,L]
This is bog-standard - redirects aff/whatever to /index.php/aff/?aff=$1
For the future, change it to this:
RewriteRule ^aff/(.*)$ /index.php?/aff/?aff=$1 [L]
This will clean up the URL and prevent an Apache rewrite cycle.
Next one:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [R=301,NC]
These will wildcard-match anything that does not exist. Same thing as before, change the last line to this:
RewriteRule ^(.*)$ /index.php?$1 [L]
This will, again, make the rewrite transparent.
P.S: get a real CMS developer. 301s are hardly useful.

Related

htaccess routing with url parameters

enter code here Hi I've the following code inside my htaccess file.
My wildcard subdomain routes to "mainfolder" - here i placed the htaccess file.
I've the following folders
"mainfolder"
"mainfolder/sub1"
"mainfolder/sub2"
etc.
Calling the subdomain - sub1.domain.com it should route to the subfolder "sub1" (subfolder=subdomain).
I tried to do it with this code
#Redirect to subdomainfolder if no special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^$ %1/index.html [L]
#Redirec to subdomainfolder if a special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain.com(.*)$ [NC]
RewriteRule ^(.*) %1/$1 [L]
The first rule works well, but if I add the second rule I receive a internal server error.
Whats wrong with this - how how I can change the first rule in this way, that it works with all url-parameters after .com - that was the reasons for me to add the second rule.
Hope I get help for this. thanks a lot.
A lot of web hosts today provide an easy implemention for subdomain creation in their administration panels. You just need to to go there, choose you subdomain name, and then point it to a directory in your tree.
If you can't, then it will be a little more complicated (You will need to resolve that subdomain to your server ip, configure some virtual hosts ... etc) and you may not have enough privileges to do that (unless you are on a dedicated server).
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/subfolder/(.*)$ http://subdomain.example.com/$1
LIke
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^sub1/(.*)$ http://sub1.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^sub1\.example\.com$
RewriteCond %{REQUEST_URI} !^sub1/
RewriteRule ^(.*)$ /sub1/$1 [L,QSA]
If more understanding step wise then follow https://beginnersbook.com/2013/08/redirecting-from-subdirectory-to-subdomain-using-htaccess/

Rewrite live site front-end to subfolder (but not /admin) and nothing on localhost

Lots of info nearly solves this, but nothing will quite crack it yet.
The site has a front-end (all in root basically), and an admin panel living in /admin. Basic stuff.
The site runs remotely on http://www.example.com and locally on http://foo
I want nothing locally redirected at all.
On the live server I just want front-end traffic redirected to a sub-folder /coming_soon but no redirection on the admin panel. So the client can start work in admin, but the public will only ever see the content in /coming_soon. (Plus I guess the admin login page, but that's fine).
Closest I came was:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !=foo
RewriteRule ^$ /coming_soon [L]
</IfModule>
But that let me hit the "real" front-end by browsing directly to http://www.example.com/index.php
Your help much appreciated.
Hopefully I got your question right^^ Wasn't sure about the /admin part, who should access or if possible no one or...But the following is my take on your problem:
RewriteEngine On
#excludes HOST=foo and URI=/admin from rewrite to /coming_soon
RewriteCond %{HTTP_HOST} !^foo [OR]
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^ http://www.example.com/coming_soon [R=301,L]
You can additionally set a location-directive and only allow entering /admin from specific IP(s).
<location /admin>
required ip 10.11.12.13
required ip 20.30.40.0/24
</location>
update
RewriteCond %{HTTP_HOST} !=foo
RewriteCond %{REQUEST_URI} !/(coming_soon|admin)
RewriteRule ^(.*)$ /coming_soon [R=302,L]
And R=302 is just temporary rewrite = won't be cached by browsers with target location. R=301 would tell browsers to save the target /coming_soon right away.
...if any one can improve this, or make it more elegant, I'd love to hear!
RewriteCond %{HTTP_HOST} !=foo
RewriteCond %{REQUEST_URI} !/coming_soon
RewriteCond %{REQUEST_URI} !/admin
RewriteRule ^(.*)$ /coming_soon [R=302,R]

How to create a server alias for joomla

I have a multilanguage website in joomla. The FaLang component creates the following URLs:
www.myDutchDomain.nl/nl/
www.myDutchDomain.nl/en/
www.myDutchDomain.nl/de/
I have 2 other URLs which should redirect to English and German URL:
www.myEnglishDomain.eu >> www.myDutchDomain.nl/en/
www.myGermanDomain.de >> www.myDutchDomain.nl/de/
After redirect I want to see myEnglishDomain and myGermandomain in URL. If I'm not mistaken it's called server alias.
How can I create a server alias for this purpose?
You should be able to use mod rewrite to solve this by adding lines to your .htaccess file, allowing anything on your country domains 'www.myEnglishDomain.eu/{wildcard}' to be rewritten to the country path '/en/{wildcard}'.
First you must make sure that the bindings for each domain are pointing to this website.
Then add the following lines to your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.myEnglishDomain.eu$[NC]
RewriteRule ^(.*)$ /en/$1 [L]
RewriteCond %{HTTP_HOST} ^www.myGermanDomain.de$ [NC]
RewriteRule ^(.*)$ /de/$1 [L]
If you need the domain within the browser to update to myDutchDomain.nl, then doing the following instead:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.myEnglishDomain.eu$ [NC]
RewriteRule ^(.*)$ http://www.myDutchDomain.nl/en/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.myGermanDomain.de$ [NC]
RewriteRule ^(.*)$ http://www.myDutchDomain.nl/de/$1 [R=301,L]
For further help, here is a mod rewrite cheat sheet: http://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/
Disclaimer: I have not been able to test the above rules, please let me know if this contains syntax or other errors so to maintain an accurate reference for other people.

URL Rewrite multilanguage site

I have a multilanguage website made using wordpress running on apache 2.2. The url structure is www.domain.it for italian and www.domain.it/?lang=es for (for example) spanish.
My client owns also www.domain.es. My goal is to have users write www.domain.es and be redirected to www.domain.it/?lang=es
this is my htaccess:
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.es$
RewriteRule ^(.*)$ http://www.domain.it/$1/?lang=es [QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
the last four rows are for wordpress to provide index.php in some specific cases. The other two are mine to achive the goal. What I get now is a 302 page telling me that the document has moved (it says again to www.domain.es. If I add R=301 I get no more luck.
What am I doing wrong? Can anyone help me to stop banging my head around?
Do you want it to redirect? Or just handle the rewriting internally somehow?
You may need the L flag to tell it to stop trying to do anything else and just do that redirect:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.es$
RewriteRule ^(.*)$ http://www.domain.it/$1/?lang=es [L,QSA,R=301]

help with setup of .htaccess file redirects

I need help configuring my .htaccess file to handle redirects properly.
Here’s what I need to have happen. Stackoverflow's spam filter wouldn't allow me to post the full domain. So where I say "DOMAIN" you can substitue "domain.com". (I also needed to add and extra t to the http.)
Requests for the DOMAIN/page version of the file should be redirected to www.DOMAIN/page.
Requests for the 'friendly' versions of the URLS should be allowed. So a file that is really at www.DOMAIN/index.php?q=37 should be viewable by going to www.DOMAIN/latest-news
I have a big list of 301 redirects. We recently changed the site from an .asp based CMS to one written in PHP.
Example:
redirect 301 /overview.asp http://www.DOMAIN/overview
Items 1 and 2 are working fine.
However for item 3, if I put in a browser request for "http://www.DOMAIN/overview.asp" instead of redirecting to the friendly name of the file ("http://www.DOMAIN/overview") it will redirect to http://www.DOMAIN/index.php?q=overview.asp. This is the problem.
What do I need to change to get this working right?
My configuration is below:
## Fix Apache internal dummy connections from breaking [(site_url)] cache
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]
## Exclude /assets and /manager directories and images from rewrite rules
RewriteRule ^(manager|assets)/*$ - [L]
RewriteRule \.(jpg|jpeg|png|gif|ico)$ - [L]
## For Friendly URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.DOMAIN/$1 [R=301,L]
redirect 301 /overview.asp http://www.DOMAIN/overview
redirect 301 /news.asp http://www.DOMAIN/news
# ETC....
thanks!
Mod_rewrite is doing exactly what you're asking it to do ... (yes :-), that's often the problem with computers).
On the /overview.asp http://www.DOMAIN/overview line you're setting the browser to send out a brand new request from scratch, which starts the whole cycle again from the top and gets catched by the ^(.*)$ index.php?q=$1 directive.
Right before this line you should put another RewriteCond to prevent the ^(.*)$ rule to apply if REQUEST_FILENAME is either overview or news. You might also simply rewrite /overview.asp to overview [L] instead of redirecting.
If you can, set the RewriteLog directive to its highest verbosity and look at the logfile - it usually gives very good insights into what's really going on...
EDIT - if I get it right you shoud be doing this:
RewriteCond %{REQUEST_FILENAME} ! \.asp$
RewriteCond %{REQUEST_FILENAME} ! ^overview$
RewriteCond %{REQUEST_FILENAME} ! ^news$
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This would prevent any file already ending in .asp, plus those looking for overview and news, to be redirected toward index.php.
I suspect anyway that you got something backwards regarding that SEO stuff. You should indeed start from the structure of the query string that your scripts expect and use that as a base to build a sensible URL addressing schema.
EDIT #2:
There was a space too many between the bang mark ant the regex. The following code doesn't come from memory as the previous - I've tested on my local Apache and it does what's supposed to do (as long as I've understood correctly..)
RewriteCond %{REQUEST_FILENAME} !\.asp$
RewriteCond %{REQUEST_FILENAME} !overview$
RewriteCond %{REQUEST_FILENAME} !news$
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Hope this helps

Categories