I am trying to establish a rewritemap using a simple txt file for a special project that will ultimately contain about 150 redirects.
I've read through the other posts here and tried to apply alternate mods, but I just can't make a single redirect from this actually work as expected :-/
To confirm my setup:
I am using an include .conf to update my httpd.conf for the proper Virtual host. Upon running /scripts/rebuildhttpdconf (and 'service httpd restart') it is applied without error and the appropriate Include Path reference within httpd.conf becomes uncommented, which tells me it is finding the include file as expected. The Include file is called 'redirecttest2.conf' and contains:
RewriteMap redirect2 txt:/home/example/public_html/redirecttest/redirect2.txt
At the location referenced above, I have the file 'redirect2.txt', which for testing purposes contains:
maintenance.html http://www.example.com/maintenance2.html
maintenance1.html http://www.example.com/maintenance2.html
So my expectation would be that when entering 'example.com/maintenance.html' it would then be redirected to maintenance2.html, etc.
In the .htaccess file at the root of the effected domain, I currently have the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.(js|css|less|png|php|swf|flv|jpg))$
RewriteCond {redirect2:$1} !=""
RewriteRule ^/(.*) ${redirect2:$1} [R=301,L]
However, this does not currently have any effect. Entering example.com/maintenance.html or maintenance1.html does not perform any redirection - with or without the first two conditionals commented out.
I have tried numerous variations, and at times been able to break it altogether (rewrite loop error, whitescreen, or even cases in which the redirect works but instead of going to maintenance2.html, it appends /home/example/public_html to the url - ???) - but at no point can I get the example above to redirect as expected.
I tried getting assistance from the host of the VPS, but they do not provide support for such modifications.
I am hoping someone could look at the above and maybe see something glaring that I am missing? ANY thoughts appreciate...
I was finally able to resolve the issues with my RewriteMap.
In the .htaccess file at the root of the impacted domain, I am now using the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.(js|css|less|png|php|swf|flv|jpg))$
RewriteCond ${redirect2:$1} >"" [NC]
RewriteRule ^(.*) ${redirect2:$1} [R=301,L]
This now appears to work - at least for basic url's.
Related
Ok, so I know this is a question that has been asked many times, however, I have not been able to find an answer to my particular case, so please do not shoot me down.
I have a website: http://gmcomputers.co.za.
I am redirecting this URL, using .htaccess file, to a subfolder to load the content:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /gmcomputers/ [L,DPI,R=301]
Which works perefectly, except when I go to http://gmcomputers.co.za I get http://gmcomputers.co.za/gmcomputers/.
So my question is, how do I modify the above code to remove the /gmcomputers/ from being appended?
Please note I copied the code above from a website as I am not at all experienced in redirect, etc and am still learning. Also, the reason I am using .htaccess to redirect is due to there being other websites in the root directory and I therefore cannot edit any config files for Apache.
Thanking you.
You contradict yourself in your question. On the one hand you write that you want to redirect and that this "works perfectly", but then you write that you do not want that result.
My guess is that you actually do not want to redirect at all, but that instead you want to internally rewrite your requests to point to that server side folder. While the URL visible in the browser's URL bar does not show that folder. Is that what you are trying to ask?
If so take a look at this example:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
You might want to add an actual redirection to direct clients actually using the folder name in their requests:
RewriteEngine on
RewriteRule ^/?gmcomputers/(.*)$ /$1 [R=301,END]
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
Best is to implement such rules in the central http server's host configuration. If you do not have access to that you can instead use a distributed configuration file (typically called ".htaccess") located in the DOCUMENT_ROOT folder configured for the http host, if you enabled the consideration of such files in your host configuration . Though that comes with a number of disadvantages. Above implementation works likewise for both approaches.
I'm setting up a website for a user and it's become apparent that they have lots of subdomains which were previously directed to specific folders. I'd rather find some way that they can manage this themselves by creating the relevant directories rather than me keep adding virtual hosts or altering .htaccess rules each time they want to add/change them.
As such, I came up with the idea of using a catch-all vhost, and using .htaccess to direct the subdomain to the correct folder.
Now I know similar questions have been asked, but I'm trying to achieve this with a single ruleset, and without performing a full HTTP redirect.
Currently I have the below rules, but I'm getting a strange problem
RewriteCond %{REQUEST_URI} !^/\.well-known
RewriteCond %{HTTP_HOST} !=www.example.co.uk [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.example\.co\.uk [NC]
RewriteRule ^(.*)$ /%1/$1 [L,END,QSA]
Basically the idea is to avoid .well-known so that LetsEncrypt can use the document root to get certs for any sub domain, avoid www. which should use the standard path, but then match and redirect any other subdomain.
Without END this predictably ends with a server error and an exceeded the limit of 10 internal redirects message in the log. This at least seems to confirm it's matching and redirecting though.
However, when using the END keyword, as far as I understand it, the rewrite should only happen once; I'm seeing strange behaviour though.
For a specific path, it seems to work fine
GET /index.html HTTP/1.1
Host: journey.example.co.uk
HTTP/1.1 200 OK
... snip content from /journey/index.html ...
But if I don't give a path, it seems like it's processing the redirect twice.
GET / HTTP/1.1
Host: journey.example.co.uk
HTTP/1.1 404 Not Found
... snip ...
<p>The requested URL /journey/journey/index.html was not found on this server
... snip ...
Given the use of %1, which should be the first part of the hostname, $1, which should be either just a / or empty in this case, I don't see how it's ending up with journey twice in the rewritten path.
Think I might of managed to get this working myself by looking through the rewrite flags documentation for the 10th time and finding this.
nosubreq|NS Causes a rule to be skipped if the current request is an internal sub-request.
The further documentation talks about SSI which isn't relevant to my issue, but it does go on to mention the following:
Also, when mod_dir tries to find out information about possible directory default files (such as index.html files), this is an internal subrequest, and you often want to avoid rewrites on such subrequests
My understanding is that the request for / causes mod_dir to make a subrequest for index.html, which results in two requests, and two rewrites.
Adding the above flag to the rule seems to be working, at least in a few quick tests. As such the following rules seem to allow for redirecting any subdomain to the same-named directory under document root.
RewriteCond %{REQUEST_URI} !^/\.well-known
RewriteCond %{HTTP_HOST} !=www.example.co.uk [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.example\.co\.uk [NC]
RewriteRule ^(.*)$ /%1/$1 [END,NS]
I'm having difficulties getting an htaccess to work with a subdomain.
my server structure:
root / index.php ---//codeigniter index file, for application A, main domain points here.
root/staging/StagingWebsite ---// my subdomain is pointing here.
the folder StagingWebsite has a file called temp.html
moving on, my root htaccess file is this :
RewriteEngine on
RewriteCond
RewriteCond $1 ^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Which turns any access to MyDomain/foo to MyDomain/index.php/foo. (without showing the index.php).
The problem:
When I try to access my subdomain/temp.html, I get a 500 internal error.
when I remove my root/.htaccess, it works fine. So it's obviously my htaccess file.
I've figured out the problem is that the root/.htaccess rule is being applied to the subdomain, which breaks everything, But I have no ideahow to sort it out.
I've placed an empty .htaccess file inside the root/staging/StagingWebsite hoping it would just over-write any previous htaccess settings, But that didn't work.
EDIT
I fixed the issue specifically but I don't like the solution.
I added a RewriteCond to only run the rewrite rule for as specific domain.
Is there a way to solve this without specifying a domain?
Create /root/staging/StagingWebsite/.htaccess with this line only:
RewriteEngine on
This will overwrite any parent's rewrite rules.
SO I have this in my .htaccess file
RewriteEngine On
RewriteBase /core/
RewriteCond %{QUERY_STRING} ^page=([0-9]+)$
RewriteRule ^page page/%1/? [L]
my url is
http://localhost/core/page.php?page=8
with the rules applied I'm getting..
Not Found
The requested URL /core/page/8/ was not found on this server.
This is running on wampserver 2.2
the file structure looks like
c:/wamp/www/core
the .htaccess is inside the /core/ directory.
What is it that I'm missing.. i've checked my apache.conf file and it looks fine.
I think you got it the wrong way around. When logically thinking of rewriting you don't rewrite original URL to new URL (for example page.php?page=8 to page/8/) you actually rewrite page/8/ to page.php?page=8. You tell the server how it should interpret the unfamiliar URL.
So if I understood correctly what you want to achieve is:
User visits localhost/core/page/8/
User is served (under the hood) localhost/core/page.php?page=8
I believe the following RewriteRule will do the trick (The query string condition is not necessary):
RewriteRule ^page/(\d+)/$ page.php?page=$1 [L]
Here is my setup :
I have a website located at www.cabsh.org/drupal
I want to use mod_rewrite to do 2 things :
Redirect www.cabsh.org to http://www.cabsh.org/drupal/index.php (I got this one)
Rewrite /www.cabsh.org/drupal/index.php to www.cabsh.org/site/index.php
I cannot figure how to achieve the 2nd point. I'm using .htaccess files since I cannot use the main server configuration. Can anyone help me getting this to work?
Thanks!
From what I get from your comment, you just want something like this:
RewriteEngine on
# Prevent a request directly to the /drupal folder
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/drupal/
RewriteRule ^drupal/(.*)$ /site/$1 [R=301,L]
# Change a request for /site/(anything) to /drupal/(anything)
RewriteRule ^site/(.*)$ /drupal/$1
Be careful though, since Drupal (being in the Drupal folder) might generate links that point to /drupal instead of /site, which is seemingly not what you want.