Background
I have a CakePHP application that lives in /m/. I want to write a root-level .htaccess file which will redirect "subdomains" for the site as parameters to actions.
For example: I want to write a rewrite rule which will result in redirects like this -
http://mysite.myserver.com → http://myserver.com/m/mysite/
http://mysite.myserver.com/home → http://myserver.com/m/mysite/home
http://mysite.myserver.com/foo/bar?baz=true → http://myserver.com/m/mysite/foo/bar?baz=true
Ideally, this redirect should be invisible to users (I don't want to use a 301, because I don't want to change the URL).
Here's my current attempt:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.myserver\.com$ [NC]
RewriteRule ^(.*)$ http://myserver.com/m/%1/$1 [L]
</IfModule>
As far as I can tell, the main issue is with $_SERVER['REQUEST_URI']:
If I browse to http://myserver.com/m/mysite/home directly, $_SERVER['REQUEST_URI'] = /m/mysite/home.
If I browse to http://mysite.myserver.com/home using the .htaccess file above, $_SERVER['REQUEST_URI'] = /home.
The Issue
Because of the issues with $_SERVER['REQUEST_URI'], my routes aren't parsing correctly: it's trying to take the user to /home rather than /m/mysite/home as desired.
How can I change my rewrite rule to make this work properly? Is there another way to accomplish this?
What you're asking (change the domain name in URL but don't let browser see URL change) is not achievable under normal scenario. However to make it possible you have enable mod_proxy in your Apache and restart it. Once that is done use following code in your site root .htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.myserver\.com$ [NC]
RewriteRule !^m/ http://myserver.com/m/sites/%1%{REQUEST_URI} [NC,L,P]
Related
I am having an issue with a subdirectory that has many different paths, but on the new website we are no longer using that structure. The old website worked this way:
example.com/photos/photo-1
example.com/photos/photo-2
example.com/photos/photo-3
On our new site we are not using /photos, and my current redirect attempts have not worked
Redirect 301 /photos https://example.com/
ends up redirecting to
example.com/photo-1
example.com/photo-2
example.com/photo-3
Removing the photos subdirectory, but still attempting to access the subdirectory that follow it.
The desired result would be for any attempt to access example.com/photos or example.com/photos/* should redirect to homepage at example.com/
Any help would be appreciated.
This should do it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^photos/(.+) https://example.com/ [R=301,L]
</IfModule>
This probably is the approach that makes most sense:
RewriteEngine on
RewriteRule ^/?photos/[^/]+/(.+)$ https://example.com/$1 [R=301]
It uses the rewriting module which means that has to be loaded into the http server. That is the standard setup, though.
In case it is not all subfolders that should get redirected you can name a pattern instead. This allows for exceptions. For example:
RewriteEngine on
RewriteRule ^/?photos/photo-\d/+(.+)$ https://example.com/$1 [R=301]
And in case both host are identical for the old and the new "site" you can use an internal redirection to simplify things:
RewriteEngine on
RewriteRule ^/?photos/photo-\d/+(.+)$ /$1 [R=301]
In general you should try to implement such rules in the actual http server's host configuration. And only use a distributed configuration file instead (".htaccess") if you have no access to the central configuration. Various reasons.
I would like to set up an htaccess file that routes requests for any subdomain to the index.php file in the /public_html/ folder. I have already configured the DNS to accept wildcard subdomains.
I plan to give users a personalised url for the site and pass the username via the subdomain, so it is important that the url remains and hence 301 redirects are out of the question.
My htaccess currently looks like this, but does not work (server not found):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule . /index.php?subdomain=%1 [L,QSA]
My end goal is to be able to get a url such as //joebloggs.mydomain.com/foo/bar to translate into something like //www.mydomain.com/index.php?user=joebloggs&route=/foo/bar.
What do I need to have in the htaccess file to make this work?
You can have it this way
# if request is for a subdomain (except "www")
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.mydomain\.com$ [NC]
# internally rewrite to index.php
RewriteRule ^((?!index\.php$).*)$ /index.php?subdomain=%1&route=$1 [L]
Since it seems the rule is executed when you're trying to navigate to the index.php, can't this be done with simple php logic within index.php?
I mightve misunderstood your goal, but it seems to me you could easily replace this with a GET. Your url would be ready for that approach, too.
I have a site called www.example.com and I have my php files in it. I store all my working files in www.example.com/site. I want to view the site in www.example.com instead, without moving my site content. What can I do?
This is currently what I am typing in .htaccess. It will redirect my site to www.example.com/site but I think the url is ugly
RewriteEngine on
RewriteBase /
RewriteRule ^(/.*|)$ /magento$1 [NC,L]
Let me see if i understood this correctly.
You have a domanin, www.example.com, and on this domain you want to display the content of a directory, www.example.com/site !?
If this is the case then you need to change the document root
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !^/site
RewriteRule ^(.*)$ /site/$1 [L]
or
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /site/$1 [L,NC]
There are 2 things you must consider :
If I understand your request, you are accessing some PHP files using www.example.com, but what you want is to access www.example.com/site, but without the /site, right ?
So basicaly, what you're looking for is NOT rewrite, it's just pointing your domain to the good folder, which is /site, right ?
If you're using Apache2, you have to edit your apache's configuration file in /etc/apache2/site-available/default (or remplace default with the name of the virtualhost you may have created).
In this file, look for the directive DocumentRoot. It should lead to the "root" directory of your pages (the one you access typing www.example.com)
I think you just have to append /site to this DocumentRoot and then reload your apache2 with service apache2 reload
You're website www.example.com will now lead to the correct directory.
If it's still not working, you must consider looking into magento's admin, because magento is rewriting url according to the Base URL you specify inside Admin / Configuration / general / web.
You'll have to modify Base_URL in both Secure and Un-Secure sections.
Then it should work fine.
I would comment out the Rewrite bloc you're using at the moment, or maybe I didn't fully undestand what you want to achieve.
I've a website at http://ex.com/web2/ this is a real path in my server, but I wanted visitors to be able to access the website also over the URL http://ex.com/web3/ (without changing the URL on the browser), so after looking around (and asking help) I added the following to my .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^web3/?$ /web2/ [L,NC]
RewriteRule ^web3/(.+)$ /web2/$1 [L,NC]
</IfModule>
The "silent" redirect that DOES NOT change the browser URL works fine, but in PHP if I print $_SERVER[REQUEST_URI] I get the URL the user placed on the browser, /web3/ instead of /web2/.
Is there any way .htaccess can also "fake" the path that is sent to the PHP var?
(I was told this would be hard or even impossible.)
Thank you.
If you have mod_proxy loaded, you could internally proxy the request. That will change the REQUEST_URI server variable, but it's sort of a round about way to do it. Would be far more efficient if it all happened in the php scripts.
RewriteRule ^web3/?$ /web2/ [L,NC,P]
RewriteRule ^web3/(.+)$ /web2/$1 [L,NC,P]
Just need to add the P flag to the square brackets.
I just redeveloped an existing site from the ground up. The old site used pure HTML while the new site will draw content from a database. Now I need to redirect the old pages to the new URL structure, but I’m a bit confused about how to write the .htaccess rules for a file-to-file redirect.
In all the examples I’ve found, the first part of the rule looks like an absolute directory path from the root, but they only contain the part of the URL that immediately follows the domain name.
For instance, I want to redirect
https://garrettcounty.us/archives/12262011news.html
to
https://garrettcounty.us/news/20111226/house-fire-on-christmas-day
From the examples I’ve seen (both on StackOverflow and abroad), I guess the rule would be
redirect 301 /archives/12262011news.html https://garrettcounty.us/news/20111226/house-fire-on-christmas-day
but the actual path to the original file on the server is
/home/username/public_html/archives/12262011news.html
Should I use the directory path or the path from the domain?
I would love to be able to use a rewrite rule. Unfortunately, the original developer didn’t use a consistent file naming scheme so I’m faced with things like
12262011news.html
Jan-19-2012-Headlines.html
State-Of-The-Union-25jan2012.html
In the new model, I'm directing everything through index.php with
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
so if anyone knows of an easier way to map all the old pages to the new URLs, I’d love to hear it. As it stands, it looks like I have to redirect 70+ pages one-by-one.
Well you old URL doesn't have news title so obviously mod_rewrite cannot create it. However to redirect
https://garrettcounty.us/archives/12262011news.html
to
https://garrettcounty.us/news/20111226/
you can use code like this in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} =on
RewriteRule ^archives/(\d+)([^.]*)\.html$ https://garrettcounty.us/$2/$1/ [R=301,L,NC]
Path used in Redirect directives: For mod_alias or mod_rewrite you must use path relative to DOCUMENT_ROOT not the full path on filesystem.