I'm trying to create a modrewrite rule that will change:
/blah/correct/xyz.htm
to
/correct/xyz.htm
There is not always /blah but when it's there, it always appears at the beginning of the URL. The URL can be any length, with numerous sub-paths. It could even be /blah/myfile.htm (which should just rewrite to /myfile.htm).
What's the best way to do this?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule (?:^|/)blah(/.+)$ /$1 [L,NC]
This will internally forward /blah/foo to /foo or /blah/correct/foo to /correct/foo. If you want external rewrite then use:
RewriteRule (?:^|/)blah(/.+)$ /$1 [L,R=301,NC]
Related
We are changing our domain name and this is meant to work for stand alone applications. In Apache virtual host file the DocumentRoot is /var/www/website/html, not /var/www/example/html as in this block:
Alias /apps/dept /var/www/example/html/apps/dept
<Directory "/var/www/example/html/apps/dept/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I put an .htaccess file in /var/www/example/html/apps/dept directory as follows:
RewriteEngine On
RewriteBase /apps/dept/
RewriteCond %{HTTP_HOST} ^example.orgname.state.tx.us/apps/dept [NC]
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]
This seems to follow what is recommended here, http://httpd.apache.org/docs/current/mod/mod_rewrite.html and Apache : How to Use Rewrite Engine Inside Alias. I see no results. The new domain has a virutal host config in the VH file, also. This same basic rewrite works for our Drupal website which does not use an alias. What changes might be necessary to have the domain name rewritten with an appended application pathname? Is the RewriteBase incorrect?
Thx.
So you only want to redirect /apps/dept/, correct? This should work. Place it as an .htaccess or in the Apache config for example.orgname.state.tx.us and all should work as expected.
RewriteEngine on
RewriteRule ^/apps/dept/(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
So now, any requests going to this URL
http://example.orgname.state.tx.us/apps/dept/
Will now go to this URL:
http://example.orgname.texas.gov/apps/dept/
And any request parameters to the right of the URL will be passed along as well.
EDIT Just reread what you wrote here:
I put an .htaccess file in /var/www/example/html/apps/dept directory
as follows.
The .htaccess I described above should be placed in /var/www/example/html/ and not in the /apps/dept subdirectory.
But if you want the same behavior from an .htaccess placed in /apps/dept then use this:
RewriteEngine on
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
This way any request made from /apps/dept will to to example.orgname.texas.gov/apps/dept/ including subdirectories of /apps/dept such as /apps/dept/test_app1, /apps/dept/test_app2 or /apps/dept/test_app3.
Or perhaps try this:
RewriteEngine on
RewriteRule (.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
Note I removed the ^ which would force the RewriteRule to match the beginning of the URL.
You cannot match Request URI in %{HTTP_HOST} variable, it matches only domain name. Chang your rule to:
RewriteEngine On
RewriteBase /apps/dept/
RewriteCond %{HTTP_HOST} ^example\.orgname\.state\.tx\.us$ [NC]
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]
I would like to do a simple 301 direct on some old URLs that have parameters to them.
e.g.
/dir/page1/?id=4590
/dir/page1/?id=45
/dir2/page5/?id=4590
/dir2/page9/?id=45
/page90/?id=45etc
...
I want a 301 redirect of the above urls to the corresponding urls w/o parameters:
/dir/page1
/dir2/page5
/dir2/page9
/page90
My experience with .htaccess is limited. What would be the simplest and the most correct way to enable the above redirects to actually work.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id= [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
I have a single blog.php page that should get redirected from all these requests:
www.site.com/blog #goes to blog.php
www.site.com/blog/id-of-an-entry #goes to blog.php?id=id-of-an-entry
Also internationalised versions such for french:
www.site.com/fr/blog *# to blog.php?lang=fr
www.site.com/fr/blog/id-of-entry* #to blog.php?lang=fr&id=id-of-entry
What would be the more eficient and effective cond/rules for .htaccess? I've made many attemps but end walking in circles or with to many specialised rules :-) Thanks for any insights!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^blog/([^/]+)/?$ /blog.php?id=$1 [L,NC,QSA]
RewriteRule ^blog/?$ /blog.php [L,NC,QSA]
RewriteRule ^([^/]+)/blog/([^/]+)/?$ /blog.php?lang=$1&id=$2 [L,NC,QSA]
RewriteRule ^([^/]+)/blog/?$ /blog.php?lang=$1 [L,NC,QSA]
I am not using this regulary so there can be better way but this could help you. Rules works like this. In () are your variables which corresponds to what you want to match. Than you can refer to them by $1 and so on. For example news/local would redirect them to file news-local.php you can use the same system to match you get variables.
RewriteRule ^([a-z]+)/([a-z]+)$ $1-$2.php
I'm having an issue with a format of URL.
I need to send
/en
to /
The problem is that I need only /en (exactly), not /en/foo...
I know how to do it with string that ends with .html, but here I have other URLS that have /en/stuff and they are also matched.
Would really appreciate assistance...
You need to specify end of string in RewriteCond e.g.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/en$
RewriteRule ^(.*)$ / [L,R=301]
Please note that Condition pattern as well as Rewrite Rule pattern are a perl compatible regular expression (with some additions):
For Anchors:
^ - Start-of-line anchor
$ - End-of-line anchor
EDIT
BTW, there is very nice htaccess tester at http://htaccess.madewithlove.be/ which you could use to test the rules.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^en/?$ / [L,R=301,NC]
This will redirect /en or /en/ to / also note that /EN will also be handled because of NC (Ignore Case) flag used here.
I'm setting up some simple url rewriting rules using mod_rewrite and a .htacces file, but I've got some problems.
If I set up the .htacces this way:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule /index.html /index.php [L]
when I call from the browser this url:
http://localhost/~dave/mySite/index.html
I got a 404 error.
Using this .htacces instead
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule /index.html http://localhost/~dave/mySite/index.php [L]
everything works fine and I get the index.php page I'm expecting.
Am I forced to user absolute urls as the target of a rewrite? Is this a Apache configuration problem?
I'm using Max OS X 10.6.2 with its standard Apache installation.
The RewriteBase base directive is setting a base path; you don't need to provide absolute paths in the redirection rules.
The rule pattern is not plain text; it's a regular expression.
If you set a full URL as target you instruct the browser to redirect to that other location; that kind of redirection is not transparent to the user.
To sum up, try this instead:
RewriteRule ^index\.html$ index.php [L]
Try doing
RewriteRule ^(.*)/index.html $1/index.php [L]
That should sort it.