By adding the language to my website i'm facing a problem.
Becouse i don't want to change all my urls from all my templates but i want to keep the language into my url i need to modyfy the .htaccess .
Basicly the problem is as folows: - To keep the language in url i have to change the site url:
ex:
without language: http://www.mysite.com/some/other/vars SITE_URL : http://www.mysite.com
with language: http://www.mysite.com/cn/some/other/vars changed SITE_URL : http://www.mysite.com/cn
when i request for css file SITE_URL/my_style/style.css , obiously the css will not be found in 2'nd case becouse is not in the folder cn/
How i can modyfy the .htaccess to change the requested path if the path is :
http://www.mysite.com/cn
http://www.mysite.com/es
http://www.mysite.com/fr
Originaly htaccess content is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Options All -Indexes
Options All -Indexes
Options All -Indexes
Thanks..it should be simple...but i can't do it since 2 hours.
EDIT:
All i want is:
If a request is made at: www.mysite.com/cn/any/thing/else/style.css (in this case css file will not exist), make the request at: www.mysite.com/any/thing/else/style.css
Was a long day with no progress ...haha. it happends.
I suggest to fix the issue by requesting correct url! That just would be right.
However this should work
RewriteRule ^(cn|es|fr)/my_style/(.+)$ /my_style/$2 [L]
Add this to the htaccess files that you have in your cn/es/fr folders, preferably before any rules that you may have there:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*)$ /$1 [L]
This should make it so a request for /my_style/style.css from within the cn folder will serve the one from the document root.
Related
I want to redirect every request for:
http://example.com/page1.php (<-non-existent url to file)
to
http://example.com/content/sites/page1.php (<-real path to file in folder structure)
while still continuing to show:
http://example.com/page1.php
in the browser and using a .htaccess file located in root folder.
Is this possible in any way? Thanks in advance :)
As per your comment ,The folwoing rules are what you did:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
And they do checking whether the request is not directory and then check if it is accept .php then rewrite it and no need for that at all .
Try only this , in general:
RewriteEngine On
RewriteRule ^([^\/]+\.php)$ content/sites/$1 [L]
If you want it only for page1.php do this :
RewriteEngine On
RewriteRule ^(page1\.php)$ content/sites/$1 [L]
Note: clear browser cache then test`
I have to move a Zend website on a shared hosting which is using Pretty URL but css files doesn't load when params is added. Before moving, i want to test with server ip adress to be sure.
For exemple
-SERVER_IP/~username/en (works)
-SERVER_IP/~username/en/news (fails because it search in "en" folder)
I would like to redirect every css files from path /css/ to my real css folder which has many css files.
I want same thing for every path /images/ and /js/ and maybe others.
Can someone help me ?
thank you!
UPDATE 1
Does path relative change between domain.com and SERVER_IP/~username? When i set /css/style.css, it search for SERVER_IP/css/style.css instead of SERVER_IP/~username/css/style.css. If i can make it works for both, my problems will be gone. But I can't test with domain because i don't have any for the moment
UPDATE 2
I've modified my hosts file in C:\windows\system32\drivers\etc\ to simulate with a real domain. As i expect, relative path aren't working the same ways.
- SERVER_IP/~username/index.php <img src="/img/world.jpg" /> => SERVER_IP/img/world.jpg (BAD, note i've lose ~username)
- www.domain.com/index.php <img src="/img/world.jpg" /> => www.domain.com/img/world.jpg (perfect)
With that change, all my path are good now and no need to redirect anything. But another problem appears. Since i use www.domain.com, index.php is shown in the URL and I don't want this : www.domain.com/index.php/en
UPDATE 3
I've found this rule : RewriteRule ^index.php(.*)$ http://%{HTTP_HOST}/$1 [R=301,NC,L] . index.php is now remove but when i'm on www.domain.com/en, it looks like en is not in the request uri and all my links move to www.domain.com/en/en/...
I paste here my .htaccess content :
Options +FollowSymLinks
Options -Indexes
RewriteEngine on
RewriteBase /
DirectoryIndex index.php index.html
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^index.php(.*)$ http://%{HTTP_HOST}/$1 [R=301,NC,L]
UPDATE 4
I got it ! I describe my entire solution below!
COMPLETE FINAL SOLUTION
I've found a solution, everything is working #1 now on a shared hosting. Here is my complete solution, I hope it will save a lot of time to somebody.
1- Complete Zend Architecture (D=Directory, F=File)
- public_html/
(D) application/
(D) data/
(D) library/
(D) docs/
(D) nbproject/
(D) public/
(F) index.php
...
(F) .htaccess
2- Remove the basic .htaccess file in "public/" folder and place this one in the root folder "public_html/" (or ../public)
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
END !!! It's all you have to do, nothing more, no tricky or dirty code! This .htaccess file solve the DOCUMENT_ROOT config problem in httpd.conf file for shared hosting.
IMPORTANT NOTE
If your domain is not forwarded at that moment (OR you want to test before moving host from a server to another (my case)) and you want to access to your website with that kind of URL http://SERVER_IP/~username/, it will not works. The best solution is to temporaly edit your hosts file in C:\windows\system32\drivers\etc\ to add a line like this :
SERVER_IP domain.com It works great!
i have a project done ready..Now the client want to rewrite the entire url...ie(www.mydomain/page.php) to www.mydomain/page ect...
There are multiple pages.say about 10-15 in the folder..is there any possibility that we can rewrite the entire url of these pages at a given shot using .htaccess?
Also the links to these pages carry ".php" and ".html" extensions in order to navigate to another pages..
Now should I erase all these extensions manually or can i change it through other means(eg;.htaccess)
Thanks
you would need to add something to know if it's PHP or HTML, like
all .php files becomes www.mydomain.com/p/page
and .html files become www.mydomain.com/h/page
(this would need another set of htaccess rules than the one below)
or you can change all .html files to .php so it would be easier..
you can now have all files hide their .php extension like
www.mydomain.com/page.php becomes www.mydomain.com/page
to do this, on your .htaccess put:
RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
I've edited my answer after some researching because you needed to add 301 redirects from the old links back to the new ones so the code above should work now. credits to the answer here: Redirect .php urls to urls without extension
You can use something like this in the .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
By this you can use urls like:
mydomain.com/page
or
mydomain.com/page/test
But notice all request will be moved to the index.php so index.php will be like a router.
Try this Apache provided feature in your root .htaccess:
Options +MultiViews
I've been searching for 4 hours by now and I still can't get this to work.
I have the following directories in my webroot:
- application
- assets
- css
- config
- protected
- .htaccess
- ...
- .htaccess
- framework
- [Yii framework inside]
- .htaccess
The .htaccess in my webroot should redirect/'rewrite' all requests for whatever xxx/xxx to http://www.example.com/application/ as long as the requested file or directory does not exist.
This way requests for .css, .js and other files can still work.
In my config I made sure Yii expects SEO friendly URL's and I don't tend to use the index.php. So it now looks like this:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'' => 'site/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
'request' => array(
'baseUrl' => 'http://www.example.com',
),
The 'request' was added later on because Yii was generating links as example.com/application/site/login. With the request Yii generates example.com/site/login instead.
In the .htaccess in my webroot I tried about everything.
First I was able to 'remove' the subdir from the URL. The index page was shown.
I tried to add a rule so all none existing directories would be redirected to the same url.
My first rule broke, and 'application' was in the URL again, but no css styles were loaded.
At this moment I got the index page with css, but now everything brings me to the index page.
my .htaccess looks like this:
RewriteEngine on
RewriteCond $1 !^application/
# if a directory or a file exists, use it directly
RewriteCond %{DOCUMENT_ROOT}/application/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/application/$1 -d
RewriteRule ^(.*)$ application/$1 [L]
# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^.*$ application/index.php
Mod_rewrite is enabled (I know because some things worked before). I looked at examples from the Yii docs.
I tried solutions from other questions on Stack Overflow like this one and many many others.
Still no luck.
Could someone please help me out?
edit:
With the .htaccess above a request to example.com ends at example.com/application .. I however would like to make the 'application' 'invisible' again (worked before, don't know why it broke)
I did change my .htaccess as follows:
RewriteEngine on
RewriteCond $1 !^application/
# if a directory or a file exists, use it directly
RewriteCond %{DOCUMENT_ROOT}/application/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/application/$1 -d
RewriteRule ^(.*)$ application/$1 [L]
# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /application/
RewriteCond %{HTTP_HOST} ^(www.)?brainfeeder.be$
RewriteRule ^$ application/$1 [L,QSA]
But still a url like www.brainfeeder.be/site/login brings me to the default controller/action which is the site/index.
I guess my conditions or rules are not exactly correct yet.
Please see my small test application I set up to tackle this issue.
What happens: brainfeeder.be get rewritten to brainfeeder.be/application/ My Yii app is in there so it runs the 'bootstrap' index.php file and gets to the default controller/action, in this case site/index.
Now when you click the 'login' button it should show you a login form. But it stays at the site/index view.
Ok, once again I updated my .htaccess a couple of times. Now I have the following situation:
www.example.com AND example.com are rewritten to www.example.com/application AND example.com/application.
(www.)?example.com/existingfolder just shows content of 'existingfolder'.
(www.)?example.com/var1/var2/../varn get redirected to (www.)?example.com/application/var1/var2/.../varn
Now the only thing I would like to happen is that the latter gets rewritten instead of redirected. So visitors don't know they are in the directory 'application'.
So (www.)?example.com/var1/var2/.../varn would bring the visitor directly to the correct page.
The contents of my .htaccess at the moment:
Options +FollowSymLinks
#IndexIgnore */*
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?brainfeeder.be$
RewriteRule ^$ /application/ [L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
# The directory or file does not exist
RewriteCond %{REQUEST_FILENAME} !-s [OR]
RewriteCond %{REQUEST_FILENAME} !-l [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /application/$1 [L,R]
The thing is, when losing the R flag in the last RewriteRule will bring me to the index.php file inside 'application' but it shows the home page instead of a login form when for example I go to example.com/site/login.
Which, I guess, the script does not see the vars. (if it did site/error would trigger) So it handles this as a request to example.com/application and not as example.com/application/var1/var2
I hope I did explain the problem better this time.
Thanks for the great help 'till now.
Try to check these configuration directives if you just want to rewrite all the unexisting /$var1/$var2 to /application/:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /application/
Now, if you want those unexisting files to redirect, rather than to rewrite them? Then just add a [R] flag at the end of the RewriteRule directive, just don't forget a single " " space before the flag.
Now, if you want to redirect /application to / and then rewrite /index.php to /application and to rewrite also the unexisting /$var1/$var2 to /application/$var1/$var2 then it's quite hard (and need some exact details) but you could still try these configuration directives:
RewriteEngine on
# rewrite index.php
RewriteRule ^index.php$ /application
# rewrite unexisting files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /application/$1/$2
# try to remove this if it causes redirection loop
RewriteRule ^application/?$ / [R]
You can also try to use DirectoryIndex application/index.php at the very top of those directives to change the index of your site and remove the line RewriteRule ^index.php$ /application if it causes an error.
Actually, I can't understand your question.. You said:
The .htaccess in my webroot should redirect/'rewrite' all requests for
whatever xxx/xxx to http://www.example.com/application/ as long as the
requested file or directory does not exist. This way requests for
.css, .js and other files can still work.
And now, you said to your comment:
So any link to brainfeeder.be/application/$var1/$var2 should be shown
as brainfeeder.be/$var1/$var2
If you would also like to redirect existing /application/$var1/$var2 to /$var1/$var2 then please try to add these directives, and if it causes an error to your system, just remove it:
# the condition is important as mentioned above
RewriteCond %{REQUEST_URI} !\.css$
RewriteRule ^application/([^/]+)/([^/]+)/?$ /$1/$2 [R]
You can add another condition (as many as you like) at the top of the RewriteRule, just use your thinking if you're a programmer. You could add another condition like RewriteCond %{REQUEST_URI} !\.jpg$ if you doesn't want to redirect the file with an extension like .jpg or else that you want such:
RewriteCond %{REQUEST_URI} !\.css$
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteCond %{REQUEST_URI} !\.png$
RewriteRule ^application/([^/]+)/([^/]+)/?$ /$1/$2 [R]
Please try to change the source of your .htaccess file with this code:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?brainfeeder\.be$
RewriteRule ^/?$ /application/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /application/$1 [L]
Not sure what your question is ...
But here is an .htaccess that should accomplish what you want:
RewriteEngine on
# 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
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). After you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php under webroot (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After moving the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details I describe the approach more fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory
I have the following in my htaccess file:
RewriteEngine On
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
From rewrite.php I redirect to the correct pages depending on the url. Problem is that it redirects all files including css and js. I tried including these files but I now realise that was dumb of me. Should I redirect when there is and appropriate extension in the url? If redirecting is the way to go what method would be best? header location or HTTP_redirect?
Or is this not a good idea performance or work involved wise? I could go for something like this but I know next to nothing about apache and would rather not work with it right now.
RewriteRule ^(.*).css$ /includes/compressor.php?i=$1.css [L]
I previously had the following in my htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
I decided to remove this because:
I would not be able to include the header and other common files in the rewrite.php file. I would also not be able to have a database call in the rewrite file that would determine the page to include and to reuse the data for the page contents.
Unwanted files would be reachable such as service used only by external app.
The compression should be done once, and not for every request. You can then exclude requests from the URL rewriting if the corresponding file exists:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
How about redirecting only if the requested file does not exist on the server?
You could use the following rewrite conditions to achieve this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
So if the request is for a CSS/JS/HTML/Image file that already exists on the server then no rewriting takes place and the request for the file is processed. If the file does not exist, it will run your rewrite rule to rewrite.php