I want to to redirect access to mysite.com/mydir/, to mysite.com/mydir/index.html
There is a .htaccess config file inside the mydir but it doesn't work as expected:
RewriteCond %{REQUEST_URI} ^/
Rewriterule ^(.*)$ index.html [L,R=301]
Now when I go to mysite.com/mydir/ , I am redirected to:
mysite.com/mydir/C:/wamp/www/mysite/index.html
and If use instead:
Rewriterule ^(.*)$ /index.html [L,R=301]
I am redirected to
mysite.com/index.html
Only thing that works is:
Rewriterule ^$ /mydir/index.html [L,R=301]
but I don't want to hardcode mydir, because this .htaccess might be used in another directory. Could this be solved in a modular way?
You can try with the following (.htaccess file stored in mydir):
RewriteEngine on
RewriteBase /mydir/
RewriteCond %{REQUEST_URI} ^/mydir/$ [NC]
RewriteRule ^ index.html [R=301,L]
I haven't been able to test this since I don't have apache installed locally, but the problem is that you are not appending the group in the parens to the start of the url, if you try
Rewriterule ^(.*)$ $1/index.html [L,R=301]
Then it should will prepend what ever the characters at the start of your URL are i.e. mydir/ before index.html
Try this
RewriteCond %{REQUEST_URI} /$
Rewriterule (.*) http://%{HTTP_HOST}%{REQUEST_URI}index.html [L,R=301]
Related
In the public_html folder which is the root folder of my main site thesite.com, I have this htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^m\.thesite\.com$ [NC]
RewriteCond %{HTTP_HOST} ^blog\.thesite\.com$ [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.thesite\.com [NC]
RewriteRule ^(.*)$ https://thesite.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2&did=$3 [L]
RewriteRule ^category/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2 [L]
RewriteRule ^category/([^/\.]+)/?$ category\_product\_list.php?mid=$1 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3&mid=$4 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/?$ product.php?id=$2 [L]
RewriteRule ^offer/(.*) product_offer.php?ind=$1
RewriteRule ^([^/\.]+)/?([^/\.]*)/?$ $1.php [QSA,L]
Here, am forcing non-www url as well as forcing https URL for the thesite.com. And I have two subdomains: m.thesite.com and blog.thesite.com.
Both are in separate subfolders inside public_html.
The m.thesite.com is in /public_html/m.thesite.com/ folder. And it has this htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.m\.thesite\.com [NC]
RewriteRule ^(.*)$ https://m.zapteka.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2&did=$3 [L]
RewriteRule ^category/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2 [L]
RewriteRule ^category/([^/\.]+)/?$ category\_product\_list.php?mid=$1 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3&mid=$4 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/?$ product.php?id=$2 [L]
RewriteRule ^([^/\.]+)/?([^/\.]*)/?$ $1.php [QSA,L]
RewriteRule ^offer/(.*) product_offer.php?ind=$1
For this subdomain also, I need to force https urls as well as non-www urls.
It seems to be working for the main site. I mean thesite.com. But when I try to access the m.thesite.com, it seems to be internally redirecting to the files of thesite.com instead of the m.thesite.com. I mean the url is being shown as https://m.thesite.com/ but the pages are from https://thesite.com
I have been trying to debug the issue for several hours now. Probably it could be something small that I missed. Any ideas?
BTW, thesite.com is having Wildcard SSL installed.
EDIT
Am sorry if my explanation was confusing. When I said url is showing as https://m.thesite.com/ but the content is from https://thesite.com/, I meant to say that in browser the url is shown as https://m.thesite.com. But the template file contents where from the desktop version of the main site thesite.com. The m.thesite.com is mobile version of the site. The pages that displays the contents where separate in both the desktop and mobile version of the site.
Like you see in the htaccess code that I included above, there is some PHP files inside the /public_html/ folder for the desktop version. All the files for the mobile version of the site is in /public_html/m.thesite.com/ folder.
So, in short, when I try to access the https://m.thesite.com/, it is displaying the index.php page from /public_html/index.php instead of /public_html/m.thesite.com/
So that's how I found that there's issue in the routing.
EDIT
VirtualHost entries fetched by using this command /usr/local/apache/bin/httpd -S:
I have also checked the Apache version and saw that it's 2.2.27 using the command: httpd -V
So I tried adding this line in the htaccess file:
RewriteLog "/logs/rewrite.log"
RewriteLogLevel 9
But it was giving internal server error. So I tried this line instead also:
LogLevel mod_rewrite.c:trace8
That was also giving me internal server error when site is accessed. So I commented it out.
I have:
mydomain.com/folder-name/segment1/segment2
I want to change it to:
mydomain.com/segment1/segment2
using a 301 redirect.
I've tried:
RewriteCond %{REQUEST_URI} !^/test/.*$
RewriteRule ^(.*)$ /test/$1 [L]
but its not working
here is my htacess file:
# #AddHandler application/x-httpd-php53 .php .php5 .php4 .php3
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/b1/.*$
RewriteRule ^(.*)$ /b1/$1 [R=301,L]
The answer for the first part of the question should be like this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? $2/$3 [R=301,L]
The second code that you've tried is the opposite of what you're asking for initially. This line matches anything not starting with /test/:
RewriteCond %{REQUEST_URI} !^/test/.*$
This line says take everything and rewrite it to the /test/ directory:
RewriteRule ^(.*)$ /test/$1 [L]
So together anything that's not in the test directory is being rewritten to the test directory.
If you're trying to specifically remove the word test then you would remove the ! symbol in your attempt to create a match. Since you already know it's called test there's no need to even make Apache perform this look for 'test' because Apache handles the RewriteCond statement after the RewriteRule (rather unintuitively).
RewriteCond %{REQUEST_URI} ^/?test
You can specialize the rewrite rule like this (I've added [QSA] to catch any query strings:
RewriteRule ^test/([^/]+)/([^/]+)/? $1/$2/ [R=301,L,QSA]
Simply change your code to:
RewriteRule ^test/(.*)$ /$1 [R=301,L,NC]
For my site I have a directory called /test/. I want to rewrite www.example.com/nl/test and www.example.com/nl/test/ to a certain page (test.php).
Some global conditions (for all the rules)
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R]
RewriteBase /
RewriteRule ^(nl|en)$ $1/ [NC,R]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ en/$2 [L,R=302]
RewriteRule ^(nl|en)/(.*)$ $2?language=$1&%{QUERY_STRING} [L]
RewriteRule ^sale$ sale.php
RewriteRule ^valentine$ valentine.php
Some conditions for the rewrite + folder
RewriteRule ^test/$ test.php
The redirect of www.example.com/nl/test/ is correct. The language parameter is also correctly rewritten.
For the second redirect (the version without the trailing slash) I can't get this working.
RewriteRule ^test$ test.php
Now my URL is rewritten as www.example.com/test/?language=nl
Can someone give me a tip or hint to fix this? I can't change the name of the directory since there are several external URLs linking to this directory.
This rule will do the whole job (instead of 4 lines you have there): it will rewrite both /nl/test and /nl/test/ to /test.php?language=nl.
RewriteRule ^(en|nl)/test/?$ /test.php?language=$1 [NC,QSA,L]
NOTES:
The [QSA] flag will preserve any existing query string (therefore, there is no need for &%{QUERY_STRING}).
Full .htaccess:
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ /en/$2 [R=302,L]
RewriteRule ^(nl|en)/(.*)$ /$2?language=$1 [NC,QSA,L]
RewriteRule ^sale/?$ sale.php [QSA,L]
RewriteRule ^valentine/?$ valentine.php [QSA,L]
RewriteRule ^test/?$ test.php [QSA,L]
NOTES:
There is no need for RewriteRule ^(nl|en)$ $1/ [NC,R] as you already have RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]. It does the same job.
The is my real folder scheme:
ROOT
index.html
news.html
+articles
|-obama.html
|-oil.html
I want some htaccess rule so if people go to domain.com/obama.html the server will fetch the one in the articles folder without redirecting.
If some one goes to domain.com/index.html will still fetchs the one in the articles even if there is an index in the ROOT.
Thanks
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond articles/%{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ articles/$1 [QSA]
RewriteRule .* articles/$1 [L,QSA]
Why don't you just use:
# RewriteRule /obama.html$ /articles/obama.html [L]
RewriteCond %{REQUEST_URI} !^/{index,news}.html$
RewriteRule (.*)$ /articles/$1 [L]
?
I'm trying to remove the index.php from the URL, which is working with the .htaccess examples found on the EZPublish site and ForceVirtualHost=true. The problem is that the old links that point to index.php are no longer working (which is problematic when linking from search engines).
I've tried to find a fix for this in using rewrite rules in .htaccess, but I can't get this to work. Some of my attempts are:
Attempt 1
RewriteCond %{REQUEST_URI} ^/index.php
RewriteRule ^index\.php(.*) http://www.mysite.com$1
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* index.php [L]
This attempt causes an infinite loop.
Attempt 2
RewriteCond %{REQUEST_URI} ^/(index\.php)?(.*)$
RewriteRule %2 index.php [L]
RewriteRule .* index.php [L]
Also doesn't work :-(.
NB. 'RewriteRule .* index.php [L]' is necessary to make the virtual host setup in EZPublish work.
Any help would be greatly appreciated.
Vivienne
in your settings/override/site.ini.append.php:
[SiteAccessSettings]
ForceVirtualHost=true
Try to check the request line instead:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php
RewriteRule ^index\.php/?([^/].*)?$ /$1 [L,R=301]
RewriteRule !^index\.php$ index.php [L]