I have developed my website in codeigniter. I have a couple of things to accomplish in .htaccess file.
For any page/resource other than index.php, img, js, css i want it to add index.php before the actual resource in the url.
I have so many static pages having unserscore "_" in them, e.g, contact_us.php, our_services.php etc. I want when user gives the url like www.mywebsite.com/our-services it should open up the original page, that is, www.mywebsite.com/our_services. There are 1,2 upto 7 underscores for different pages, e.g, mywebsite.com/speech_writing_services
Here is my .htaccess file which i could build up so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/StyleAdmin|/images|/StyleAdmin/images|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5_$6 [R=301,L]
The problem here is that when i give the url like www.mywebsite.com/contact_us it works just fine and shows the same url in the address bar. But when i give this url www.mywebsite.com/contact-us it does show the page but the url shown in the address bar of the browser becomes www.mywebsite.com/index.php/contact_us whereas i want it to be like www.mywebsite.com/contact_us with index.php removed.
You need to do the routing after the redirecting.
RewriteEngine on
RewriteRule ^([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5 [R=301,L]
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5_$6 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/StyleAdmin|/images|/StyleAdmin/images|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
Related
I've been trying several solutions in the internet and can't make the redirect voodoo to work.
Setup
My setup is a website build with jekyll, and I want to consume it as mydomain.com/~user.
The folder structure is
~user
- _site
- index.html
- folder1
- index.html
- folder2
- index.html
- other_stuff
Problem
I was able to redirect from mydomain.com/~user to mydomain.com/~user/_site. So, half way through. The problem came when I tried to mask and remove the _site from the URL again. And I hit a wall.
Basically I want to use: mydomain.com/~user and see that URL in the browser, while beeing served the page that lives in mydomain.com/~user/_site/index.html. And same goes for other links
Request See Served
======= ======= =======
mydomain.com/~user mydomain.com/~user mydomain.com/~user/_site/
mydomain.com/~user/folder1 mydomain.com/~user/folder1 mydomain.com/~user/_site/folder1
What I try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/~user/_site/?
RewriteRule ^(.*)$ /~user/_site/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule !^~user/_site(.*)$ /~user/_site$1 [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule ^~user/_site(.*)$ /~user$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule !^~user/_site(.*)$ /~user/_site$1 [L]
However, all these
Do not end with the desired hidden URL in the bar
Give an error, as some of the redirects are not allowed in the server
Partial solution
After tweaking the proposed solution, I was able to identify one variation that partially works
RewriteEngine On
RewriteBase /~user
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/_site/? [NC]
RewriteRule ^(.*)$ _site/$1 [L]
Note that in the last two line, i remove the ~user from the regex. This redirects all subfolders and pages, and allows other folders that are in ~user to be accessed.
However, the "home" mydomain.com/~user is the only one that is not redirected. As it exists the RewriteCond %{REQUEST_FILENAME} !-d doesn't allow it to be processed.
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^/?$ _site/ [L]
To redirect the home (~user) to mydomain/~user/_site, but it doesn't work. And tried variations of ^/?$ and ^$. But they don't work either.
Ideas?
You should try it this way.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/~user/_site/? [NC]
RewriteRule ^~user/(.*)$ /~user/_site/$1 [L]
We are using a CMS called ExpressionEngine.
We have template/folder called Contact - that has and index file. We only have the index page for contact because we have no deeper URLs that we need for Contact.
However if you add anything to the end of the normal contact pages URL it loads the same content at the contact page but isn't redirects and keeps the random URL.
Example:
This is the correct URL and displays the index content:
http://www.example.com/contact/
These are incorrect URLs (that still work - everything works) and it displays the index content:
http://www.example.com/contact/kjhfd/
http://www.example.com/contact/kjhfd/kljhdf/
http://www.example.com/contact/f/
We want that to stop and 301 redirect to http://www.example.com/contact/.
I have tried RedirectMatch with htaccess - I've tried 301 Redirect in htaccess and I've tried a funky PHP if redirect - none of which have worked so far
We have no PHP redirects. We have many htaccess redirects, please see below:
RewriteEngine On
#Trailing backslash
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* [QSA,L]
A quick and easy fix is to check for the segment_3 in the URL,if it exist then redirect to contact index page.You can use {redirect} tag.
{if segment_3}
{redirect ="/contact"}
{/if}
You can use the following handler in htaccess :
RedirectMatch ^/contact/.+$ /contact
This will redirect a url with trailing path(s) to the correct location
/contact/foo
to
/contact
I created some static sub-domains for images:
www.static1.domain.com
www.static2.domain.com
Now I want to redirect files that are not images from static domains to www.domain.com, to avoid duplicate content. I have these rules in my htaccess (non-existing files are redirected to index.php silently):
#Redirect static to main
RewriteCond %{HTTP_HOST} static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]
#Redirect non exisitng files to index.php (silent)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
The redirect works fine. But if I enter a non-existing image e.g. http://www.static1.domain.com/test.gif, I am redirectd to http://www.domain.com/index.php.
The redirect of test.gif should be a silent redirect to index php ... what I am doing wrong?
Thanks for hints.
I'm not sure if I'm understanding you correctly. By silent redirect to you mean that "index.php" shouldn't be in the url bar or do you mean the url bar should still read "test.gif" but the page should render index.php?
You can not add multiple RewriteCond lines. Only the last one applies to RewriteRule.
So this line does not have any effect:
RewriteCond %{REQUEST_FILENAME} -f
And that is why non existing images are also being redirected.
How about:
#Redirect non exisitng files to index.php (silent)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
#Redirect static to main
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]
Did it this way. Throws the default server 404 Page when http://www.static1.domain.com/test.gif is not found. Not best, but well.
RewriteCond %{HTTP_HOST} !static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]
I have a website http://rochesterwaterskishow.com which they've recently changed their name so they want to update their url to http://skidox.com. I'm trying to redirect any page from rochesterwaterskishow.com to skidox.com/site/index.
I have this line of code which redirects http://rochesterwaterskishow.com to http://skidox.com, but if I go to something like http://rochesterwaterskishow.com/test, it doesn't redirect to http://skidox.com.
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
How can I make it a catch all so anything rochesterwaterskishow.com/* gets redirected to skidox.com/site/index?
UPDATE: Full .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
That's because the search pattern ^$ will only match a URI path of "/". You need to pick up the request in a match variable, for example:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/site/index/$0 [R=301,L]
I am assuming that you are using SEO optimised-style URIs for the new site. If you want to simply redirect everything to the index page without any context, then you still need a pattern that matches:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^ http://skidox.com/site/index [R=301,L]
Update following post of full htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/$0 [R=301,L]
RewriteCond $0 ^(index\.php$|robots\.txt$|resources)
RewriteRule ^.* - [S=1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index/$1 [R=301,L]
So I'm using .htaccess to redirect old site pages to new site pages. A typical rule in my file looks like this:
Redirect 301 /faqs.php http://blueprintprep.com/classroom/faq
Strangely, it works fine when the old file exists on the server, but when I remove the file, the final URL actually looks like this:
http://blueprintprep.com/classroom/faq?/faqs.php
What in the blue blazes is going on??
The file is made up of a bunch of these rules and this for CodeIgniter:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^application_consulting/(.*) http://blueprintprep.com/oneonone/app_consulting [R=301,L]
RewriteRule ^weekend/(.*) http://blueprintprep.com/classroom [R=301,L]
RewriteRule ^workshop/(.*) http://blueprintprep.com/oneonone [R=301,L]
You can use mod_rewrite instead.
RewriteEngine on
RewriteRule ^/faqs.php$ /classroom/faq [L,R=301]
This will work regardless if the file exists or not.
Hope that helps.