htaccess redirection - .htaccess

Ok my project is to make an old static site into a dynamic one. There are about 40 pages in the old site.
The new format will be in 2 languages for now, with the possibility for more in the future.
There are 2 sets of urls actually:
the basic ones: /index.html, /about.html, /contact.html
the rest: /folder1/page1.html, /folder1/page2.html, /folder3/page3.html, etc
The client wants the 2nd language to have the same pattern as the default one:
the basic ones: /language/index.html, /language/about.html, /language/contact.html
the rest: /language/folder1/page1.html, /language/folder1/page2.html, /language/folder3/page3.html, etc
The basic pages i can rewrite with htaccess:
# default language
RewriteRule ^about.html$ about.php?language= [NC,L]
#other language
RewriteRule ^([^/]+)/about.html$ about.php?language=$1 [NC,L]
As for the rest of the pages i am stuck.
In my database i have saved the existing urls and their content.
For example, for the basic language:
/folder1/page1.html will be served by dynamicpage1.php?language=&url=/folder1/page1.html
/folder2/page2.html will be served by dynamicpage1.php?language=&url=/folder2/page2.html
/folder3/page3.html will be served by dynamicpage2.php?language=&url=/folder3/page3.html
And for the other language:
/([^/]+)/folder1/page1.html will be served by dynamicpage1.php?language=$1&url=/folder1/page1.html
/([^/]+)/folder2/page2.html will be served by dynamicpage1.php?language=$1&url=/folder2/page2.html
/([^/]+)/folder3/page3.html will be served by dynamicpage2.php?language=$1&url=/folder3/page3.html
How can i construct these rules?
If i try: RewriteRule ^([^/]+)/(.*)$ subservices.php?language=$1&url=$2, it fails

You better specify/list possible languages in URL Rewriting rule -- it will be MUCH MORE accurate then. This rule works fine:
RewriteRule ^((EN|FR)/)?(.*\.html)$ /subservices.php?language=$2&url=/$3 [NC,QSA,L]
or
RewriteRule ^((english|french)/)?(.*\.html)$ /subservices.php?language=$2&url=/$3 [NC,QSA,L]
Change EN|FR to whatever languages you do use.
/index.html will be rewritten to /subservices.php?language=&url=/index.html
/FR/index.html will be rewritten to /subservices.php?language=FR&url=/index.html
/folder1/page1.html will be rewritten to /subservices.php?language=&url=/folder1/page1.html
/FR/folder1/page1.html will be rewritten to /subservices.php?language=FR&url=/folder1/page1.html
/ZZ/folder1/page1.html will be rewritten to /subservices.php?language=&url=/ZZ/folder1/page1.html (ZZ is not recognized as acceptable language).

Related

htaccess redirect of root domain, not subfolders with url masking

I am trying to do the following -
Redirect just the root domain to a different domain.
The redirect needs to be masked so the user still thinks they are on the url they typed.
Existing subfolders should still work with the existing root domain.
For example-
I have an installation using www.currentsite.com which has lots of subfolders for example www.currentsite.com/store
I want to redirect just the root of www.currentsite.com to www.newsite.com but want the browser to still say www.currentsite.com.
If the user goes to www.currentsite.com/subfolder I still want that to work with the original installation.
I have the following which seems to be handling redirecting just the root fine but does not mask the url...
RewriteEngine on
RewriteCond %{HTTP_HOST} www.currentsite\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.newsite.co.uk/ [L,R=301]
Any help id appreciated.
For what you call "masked" the usage of apaches proxy module makes most sense:
ProxyPass https://www.currentsite.com https://www.newsite.co.uk
ProxyPassReverse https://www.currentsite.com https://www.newsite.co.uk
It maps one base url to another one and takes care to transparently and reliably rewrite all contained references.
The proxy module can also be used by RewriteRules, the P flag does that. But in the end it comes out itself and the above, direct usage is more transparent and less complex.
Here is the documentation, as typical for the apache project it is of excellent quality and comes with lots of good examples: https://httpd.apache.org/docs/2.4/mod/mod_proxy.html

htaccess 301 redirect while replacing characters in URL - Helicon ASAPI ReWrite module

I have an old site that is being rebuilt. Instead of using a folders structure, it is using sub-domains. The segments are different, but the redirect itself is pretty simple. I can handle it like so:
RewriteRule ^segment/blog/view$ http://blogs.site.com/segment/article [R=301,NE,NC,L]
RewriteRule ^segment/blog$ http://blogs.site.com/segment [R=301,NE,NC,L]
So if I had www.site.com/segment/blog, it will now go to blogs.site.com/segment.
If I had www.site.com/segment/blog/view/catchy_name_goes_here, currently it redirects it to blogs.site.com/segment/article/catchy_name_goes_here and I NEED it to go here: blogs.site.com/segment/article/catchy-name-goes-here.
My issue comes from a decision to change the separator in the URI. The old articles were built with underscores '_' and the new articles are built with hyphens '-'.
How can I replace the underscores in the article titles with hyphens?
Try these rules:
RewriteRule ^/segment/blog$ http://blogs.site.com/segment [R,I,L]
# replace _ by - repeatedly
RewriteRule ^(/segment/blog/view)/([^_]*)_+(.*)$ /$1/$2-$3 [I,N,U]
# all _s gone, now do a redirect
RewriteRule ^/segment/blog/view/([^_]+)$ http://blogs.site.com/segment/article/$1 [R,I,L,U]
I ended up having to use the following. I don't know how many people this might effect due to the unique settings for this site in particular, but thought I would post the answer so it could help anyone that might need it.
The full settings on this are a server running IIS with Server 2k. The site consists of several static content pages, vb script, classic ASP, dot Net, and this is all intertwined with ExpressionEngine pages. It's a mess to say the least. To top it off, Helicon Tech's ASAPI Rewrite Module version 3 is running on the server for .htaccess usage. No sub-expressions, groupings, etc. were taking or being followed/processed. The index.php rule was getting bypassed as well.
This all said, I ended up with the following which parsed everything I needed.
RewriteRule ^index.php/segment/blog/view/([^_]*)_+(.*)$ http://www.site.com/index.php/segment/blog/view/$1-$2 [R,NC]
RewriteRule ^index.php/segment/blog/view/([^_]*)$ http://blogs.site.com/segment/article/$1 [R=301,I,L,U]
RewriteRule ^segment/blog$ http://blogs.site.com/segment [R=301,NE,NC,L]
RewriteRule ^/segment/blog$ http://blogs.site.com/segment [R,I,L]

redirect rule for multiple pages

I have page not found erros in webmaster because the page/2/0 part of a url is due to smart paging module clean url feature (Drupal) now i uninstalled the smart paging module but these page not found errors are still there.
www.mysite.com/a/b/c/page/2/0,
www.mysite.com/a/d/e/page/3/0,
www.mysite.com/a/f/g/page/4/0,
www.mysite.com/a/h/i/page/5/0
and so on.
I want to redirect
www.mysite.com/a/b/c/page/2/0 to www.mysite.com/a/b/c
www.mysite.com/a/d/e/page/3/0, to www.mysite.com/a/d/e
www.mysite.com/a/f/g/page/4/0, to www.mysite.com/a/f/g
www.mysite.com/a/h/i/page/5/0 to www.mysite.com/a/h/i
with one redirect rule. How to do this
in short i want to remove the page/x/0 part from the url and redirect it to the remaining part of that url.
You want something like this:
RewriteEngine On
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/page/[0-9]+/0$ /$1/$2/$3 [L,R=301]
The literals here are after the first 3 path nodes: /anything/anything/anything/, then it must be followed by a page, then some numbers, then a zero. You can make it even more general, say after the "page", by changing the pattern to:
RewriteEngine On
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/page/ /$1/$2/$3 [L,R=301]

Multilanguage website mod rewrite problem

I have a problem with mod rewrite and did not find any solution here. Here is the problem:
I have website with two languages and mod URL should look something like this:
/eng/contact
/srp/kontakt
/eng/news
/srp/vesti
/eng/event
/srp/najava
Mine rewrite rule is not working because I have in .htacess situation like this:
# news
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ news.php?lang=$1&pagename=$2 [NC,L]
# contact
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ contact.php?lang=$1&pagename=$2 [NC,L]
# event
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ event.php?lang=$1&pagename=$2 [NC,L]
My question is how to achieve rewrite for pages in the above examples?
I would use:
RewriteRule ^([^/]+)/([^/]+)/*$ index.php?lang=$1&pagename=$2&%{QUERY_STRING}
and then route the PHP flow from index.php to news.php/contact.php etc. by using some simple switch-case-include statement:
switch ($_GET['pagename'])
{
case 'news':
require_once 'news.php';
break;
...
...
}
This will also help you develop other routing related features simplifying the .htaccess file. This also enables easy lookup for native subpages names of subpages like "en/contact" but "pl/kontakt" etc.
I use this approach on almost all my sites (e.g. http://www.calculla.com/en/ascii2hex and http://www.calculla.com/pl/ascii2hex).

Simple url rewrite

I'm trying to rewrite part of an url as I've changed a CMS and still want Google to find my articles.
I have:
www.mywebsite.com/vision
www.mywebsite.com/vision/40/some-article-name
and want to rename them:
www.mywebsite.com/news
www.mywebsite.com/news/40/some-article-name
Any hints as to the re-write rules or where I can look? I'd like to change the rules in my .htaccess file.
# Activate Rewrite Engine
RewriteEngine On
# redirect /vision to /news
RewriteRule ^vision$ http://www.mywebsite.com/news [R=301,NC]
# redirect /vision/bla-bla to /news/bla-bla
RewriteRule ^vision/(.*)$ http://www.mywebsite.com/news/$1 [R=301,NC,QSA]
In theory (and practically) these 2 rewrite rules can be combined, but then if you have URL that starts with "vision" (like this, for example: /visions/hurray) then such rule may redirect wrong URLs. Therefore I have done it via 2 rules which is much safer.
Try: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
or: http://httpd.apache.org/docs/2.2/mod/mod_substitute.html if you want to change links in the html content returned to the browser.
Here is an example of how I might do the rewrite I think you're after...
RewriteRule ^(.)/vision/(.)$ $1/news/$2
This may be to broad of a rewrite scope in which case this may be better...
RewriteRule http://www.mywebsite.com/vision/(.*)$ http://www.mywebsite.com/news/$1
Also learning the basics of regex will be a needed skill for doing any complex rewriting IMO.
Hope that helps.

Resources