htaccess internal rewrite from subdomain to subdir path - .htaccess

Im at least 24 hours busy on this subject, I cant seem to get this script to work. The script doesnt redirect at all.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^m\.skynet\.com$
RewriteRule ^/(.*)$ /mobile/final/$1 [L,NC]
What am I doing wrong?
UPDATE:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^m\.skynet\.com$
RewriteCond %{REQUEST_URI} !^/mobile/final/
RewriteRule ^(.*)$ /mobile/final/$1 [L,NC]
This partially works, but links like m.skynet.com\download.php wont get redirected
UPDATE: 45min later
Now it suddenly works.
But the URL gets rewritten in FireFox, while in Chrome it works properly
UPDATE
Seems that the problem with Firefox was caused by a 301-redirect I used earlier, the cache had to be flushed. for it to work properly again

Remove leading slash from match:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^m\.skynet\.com$
RewriteCond %{REQUEST_URI} !^/mobile/final/
RewriteRule ^(.*)$ /mobile/final/$1 [L,NC]
.htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
You need RewriteCond %{REQUEST_URI} !^/mobile/final/ to prevent looping.

Related

Trailing slash issue using mod_rewrite for SEO friendly URL

I consider myself reasonably competent with PHP. I am, however, completely and totally lost when it comes to mod_rewrite.
I have a URL structure that works like the following:
http://site/something/something-else/the-actual-page/
that redirects to:
http://site/index.php?page=the-actual-page
It's only ever the final 'folder' that is passed to the script. The preceding 'folders' (if any) are for SEO and structure purposes.
If there is a preceding folder "promotion" then it redirects to a separate file. This is along the lines of:
http://site/promotion/campaign-name/
redirecting to
http://site/promotion.php?campaign=campaign-name
I'm using the following code to achieve this:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^promotion/(.*)/$ promotion.php?params=$1 [L]
RewriteRule ^(.*) index.php?page=$1 [L]
</IfModule>
This works as intended, with links redirecting properly EXCEPT when there is no trailing slash. For example
http://site/something/thepage/
will work, whilst
http://site/something/thepage
will not.
To solve this problem I'm attempting to set up a 301 that redirects any URI without a trailing slash to a URI with a trailing slash.
The code below (placed above the other rules) works to a degree, but I lose folder data.
Code:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
The problem?
http://site/something/thepage
redirects to
http://site/thepage/
I'm afraid all the googling in the world is not helping me, as I cannot wrap my brain around mod_rewrite at all!
Appreciate any help.
You'll be better off using this:
RewriteEngine On
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# ... your other rerites
If you'd like to reverse it and strip the trailing slash instead, then use this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
You'd then need to change your rewrite accordingly:
RewriteRule ^promotion/(.*)$ /promotion.php?params=$1 [L]

Htaccess - Rewrite engine (reverse engineering a line of code)

On a site I'm working on, if you enter the url, plus 1 directory, the htaccess adds a trailing slash.
So, this: http://www.mysite.com/shirts
Becomes this: http://www.mysite.com/shirts/
The htaccess that runs the site is quite long and complex, so it's not easy to find or test which rule is causing the rewrite. I was able to track down the issue to this line of code (I think):
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Does this rule match the behavior I'm describing above? It seems to be the cause, but it doesn't make logical sense to me. I don't unsderstand where the trailing slash is coming from.
Can someone shed some light on this for me? Thanks in advance.
Edit: MORE:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
By default apache will add the ending /, you will have to use:
DirectorySlash Off
To disable that behavior which is caused by mod_dir, you can read more about it here.
However if you're trying to remove the / to fix images not showing. That is not the right way to do it, you should instead use the HTML base tag, for example:
<BASE href="http://www.yourdomain.com/">
Read more here about it.
Your current rule as you have updated on your question:
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Means:
if domain on the URL is only mysite.com
redirect current URL to domain with www.
So an example of it would be, if you access:
http://domain.com/blog/some_blog_article
It will redirect the user to:
http://www.domain.com/blog/some_blog_article
Note how it retains everything and only add the www. to the domain.
If you really want to redirect it regardless here is one way to do it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
# check if it is a directory
RewriteCond %{REQUEST_FILENAME} -d
# check if the ending `/` is missing and redirect with slash
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]
# if file or directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# and we still want to append the `/` at the end
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]

.htaccess redirect foo.com/ to /foo.htm, leave bar.com/ as bar.com

Note: I had to add spaces because it thought I was posting links...
I have two sites coming into one server and one folder (foo.com and bar.com).
Foo.com needs to point at a page named foo.htm under the root of the site.
It also has the requirement of not changing the URL.
If the url is bar.com it needs to be left alone.
If the full url is http://www.foo.com/ it needs to be switched to the equivalent of
http://bar.com/foo.htm
Does that make sense?
I have the following which works for every page except the root page, which isn't redirecting to foo.htm.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.bar.com/$1
RewriteRule ^$ /foo.htm [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^$ http://www.bar.com/foo.htm [L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteCond %{REQUEST_URI} ^/.
RewriteRule ^(.*)$ http://www.bar.com/$1
I'm not sure if it will run exactly like this as I can't test it - but if it doesn't work, just modify the regex in RewriteCond %{REQUEST_URI} .... possibly remove the slash and question mark... just try it.

Trying to add trailing slash with htaccess, results in a absolute path

What I'm trying to achive is to have all urls on my page look like http://domain.com/page/, no extensions, but a trailing slash. If a user happends to write http://domain.com/page or http://domain.com/page.php it will redirect to the first url. After some googling i found this code, and it's close to working, but when you leave out the trailing slash in your request the url becomes something like http://domain.com/Users/"..."/page/ and therefor returns a 404.
My .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !(.*)/$
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
I've been trying to add an additional rule but I really don't get any of this and I haven't been able to find any answers.
For a scenario like this one, the .htaccess author has to consider both what the browser URL bar should display and what file the web server should return/execute. Note also that each external redirect starts the processing of the rewrite directives over.
With that in mind, start by taking care of which file is returned when the URL is in the correct format:
RewriteEngine on
RewriteRule ^/?$ /index.php [L]
RewriteRule ([^./]+)/$ /$1.php [L]
Then, deal with URLs with no trailing slash by redirecting them with [R=301]:
RewriteRule ^/(.*)\.[^.]*$ http://www.example.com/$1/ [R=301,L]
RewriteRule ^/(.*)$ http://www.example.com/$1/ [R=301,L]
Note that the first of these two rules should also take care of the case where there is a filename (like something.php) but also a trailing slash by eliminating the filename extension and re-adding the slash.
Keep in mind that, if your internal directory structure does not match what the web server is serving (as is often the case in shared hosting scenarios), you will likely need to add a RewriteBase directive immediately after the RewriteEngine directive. See the Apache docs for an explanation.

Weird .htaccess url rewrite discrepancy

So http://myopicvoid.org/ when loaded in Firefox or Chrome, automatically redirects to http://myopicvoid.org/main as well it should, but not in IE8. What exactly would cause this? My .htaccess is as follows:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteCond %{REQUEST_URI} ^/$
RewriteRule / /main [r=301,L]
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteCond %{REQUEST_URI} !^/script/.*
RewriteCond %{REQUEST_URI} !^/style/.*
RewriteRule ^(.*)$ /script/$1.py [L]
There exists a main.py in /script, but I'm met with Error 404 Not Found (script not found or unable to stat: script/.py) in IE. Help?
I would recommend changing
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteCond %{REQUEST_URI} ^/$
RewriteRule / /main [r=301,L]
To:
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteRule ^/?$ /main [r=301,L]
So, what I've done is removed the condition, but made the rule only match either nothing, or just a single fore-slash. The condition wasn't really necessary; it's important to note that mod_rewrite processes rules first, then checks to see if they meet their conditions, so this should be a tiny bit more efficient.
This will be a little more forgiving if the request does not include the trailing slash.
Off the top of my head - maybe the URL http://myopicvoid.org/ has the trailing slash removed when you make the request from certain browers? This would prevent the "/" from matching the first RewriteRule.

Resources