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).
Related
I'm trying to make my dynamic URL's into static looking URL's.
This is a typical URL that I now have:
http://www.somedomain.com/design/index.php?p=about
I would like it to be: http://www.somedomain.com/about
So far, I've gotten it to look like this: http://www.somedomain.com/design/about.html
This is the Rewriterule I'm using: RewriteRule ^([a-z]+).html$ index.php?p=$1 [L]
How would I modify it so it would look like this: http://www.somedomain.com/about?
Thanks for any/all help!!!
Very much appreciated!
Using rewrite rules to give 'static' URI is NEVER a good idea.
A few other ideas you can use:
Make the 'about' page a directory (folder) with a file called index.php or index.html in it. This way the URL shows http://example.com/about/ and the information you wish can still be displayed as needed.
Use the POST method instead of GET methods. This will display as http://example.com/about.php (Note: there is no ? or other parameters behind that.)
Utilize both methods to give a 'seamless' URI on page transitions.
Rick, you're on the right track. You need to read the Apache rewrite documentation. For your docroot/.htaccess start it with:
RewriteEngine On
RewriteBase /
Then generalised version of your rule:
Rewrite Rule ^(\w+)$ index.php?p=$1 [L]
This will rewrite any requests which are for a word string to index.php. You need to be aware that the rewrite engine rescans the .htaccess file if a match has occured so you need to make sure that you don't create a loop. In this case the replacement string has a "." in it and the pattern doesn't, so this won't occur, but for more complex cases you may need to 'guard' the rules with one or more RewriteCond statements. Again, read the Apache documentation.
I'm developing a multilanguage web app with Yii.
I applied changes to hide the index.php, changed urlFormat to path and added to the url path a slug with the user language example /it/index.php /en/index.php etc...
The problem now is that I need to redirect automatically to a different url once the user chooses another language. For example:
http://localhost/~antonio/project/it/women
needs to redirect to:
http://localhost/~antonio/project/it/femme
I have been playing with htaccess with no luck at all. Here is the actual code:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteBase /~antonio/project/
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
#My redirection code (tried a good few more to no use apart from this)
RewriteRule ^it/women$ it/femme
I would really appreciate any help on this issue, as it is driving me mad.
Thanks
Edit::
I surrendered with mod_rewrite. I found another solution by adding this code to /layout/main.php:
<?php
$onurl = Yii::app()->getRequest()->requestUri;
if ($onurl == "/~antonio/project/it/women") {
$this->redirect("/~antonio/project/it/femme");
} elseif ($onurl == "/~antonio/project/it/men") {
$this->redirect("/~antonio/project/it/uomme");
}
Rinse and repeat per combination of language/word
This might not work without setting up a proper Virtual Host (so that instead of local urls like http://localhost/~antonio/project/it/women you have nice urls like http://project1.dev). But I would do that anyway, since it makes your dev environment nicer! (Here's a place to start).
Anyway, I would try this: just leave the .htaccess file set like you normally would for "path" style urls, and then just parse a $_GET['lang'] parameter using the UrlManager? So you would have a urlManager setup like this in your config.php:
'urlManager'=>array(
'urlFormat'=>'path', // use path style parameters
'showScriptName'=>false, // get rid of index.php
'rules'=>array(
// this parses out the first chunk of url into the "lang" GET parameter
'<lang:[\w\-]+>/<controller:[\w\-]+>/<action:[\w\-]+>'=>'<controller>/<action>',
)
)
This way a url like this http://project1.dev/it/controller/action will redirect to the "action" action in your Controller like normal (controller/action), but in your action $_GET['lang'] will now have the value of "it".
I hope that helps, it's kind of a shot in the dark. I'm not sure how you are actually triggering the different languages, so a $_GET parameter might not be helpful after all.
Found a way to do this in htaccess:
RewriteCond %{REQUEST_URI} ^/~antonio/project/it/donna/shoes/(.*)$
RewriteRule ^it/donna/shoes/(.*)$ /~antonio/project/it/donna/calzature/$1 [L,R]
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).
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.
For SEO friendly urls i'm using this expression:
RewriteRule ^([0-9]+).([^/]+)$ index.php?id=$1&titel=$2
The url is domain.com/55-page-title and it works perfect.
Now i have to determine different languages and my url should look like domain.com/de/55-seitentitel. I've no idea how to rewrite regular expression in .htaccess file.
RewriteRule ^([0-9]+).([^/]+)$ index.php?lan=$1&id=$2&titel=$3
What could I insert behind '^'? thanks a lot!
Try this:
RewriteRule ^([a-z]{2})/([0-9]+).([^/]+)$ index.php?lan=$1&id=$2&titel=$3