I have a webpage where I'm going to move the old page to a subdirectory, while installing ExpressionEngine on the root. However, I want to redirect users to the subdirectory now. While I'm still able to build the new ExpressionEngine site on the root. Is this possible?
Basically, whenever a user navigates to http://www.mywebsite.com, I want to redirect them to http://www.mywebsite.com/old
But, when I navigate to http://www.mywebsite.com/new, I actually want to redirect it to http://www.mywebsite.com/index.php, because ExpressionEngine is installed on the root.
The idea is that when the page is done, I just have to remove these rules from htaccess and that's that.
As of now I've only got this rule set up:
redirect /index.php http://www.mywebsite.com/old/
This creates a problem because /index.php is the actual ExpressionEngine index.php, which is the default page of the site.
You can use this code in DOCUMENT_ROOT/.htaccess
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ /old [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.+$ /index.php [L,NC]
Related
I currently have a CRUD that has the following file structure:
The index.php file redirects to the add-product.php, delete.php and other files. The problem is, the URL looks like: myapp.com/resources/views/add-product but I want it to look like myapp.com/add-product. I found other StackOverflow posts similar to this, but it redirects to the desired link, instead of redirecting to the correct link but showing the desired one, and because of that the site doesn't work ("The requested URL was not found on this server"). The .htaccess file looks like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^resources/(.*)$ /$1 [L,NC,R]
I'm also using the file to be able to remove the .php from the URLs. The last line is the one handling the wrong redirection. How can I go about this? Thank you for your time.
You may use it like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /myapp/
# external redirect to remove /resources/views/ from URLs
RewriteRule ^resources/views/(.+)\.php$ $1 [L,NC,R=302]
# internal rewrite to add /resources/views/ and .php to show content
RewriteCond %{DOCUMENT_ROOT}/myapp/resources/views/$1.php -f
RewriteRule ^(.+?)/?$ resources/views/$1.php [END]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Let's say I have a website with given sites:
index.php
info.php
In order to hide the "index.php" part of the URL and to change "info.php" to "info" I create the following .htaccess:
RewriteEngine On
RewriteRule ^info$ info.php
RewriteRule ^.$ index.php
It works just fine when I type the new URLs directly into address bar, but inside index.php I have the:
<a href="info.php">
and so when I click the link, it directs me to the info page and displays "info.php" in the URL. If I want to see just "info" in the URL after clicking the link, do I have to change the links inside HTML code? Or is there a way to make .htaccess file do it automatically?
You can put this code in your DOCUMENT_ROOT/.htaccess file for changing these links automatically:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
But it is highly recommended to change these links in your HTML page to avoid one external redirect.
I have a Wordpress set up which requires redirection when the user enters the root of the site to a static HTML file start.html
http://www.myhomepage.com/
Redirect to
http://www.myhomepage.com/start.html
Wordpress adds url rewrites for calls to index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I only want to redirect
http://www.myhomepage.com/
and not
http://www.myhomepage.com/buy/
This will not work as all requests to Wordpress goes through index.php.
redirect /index.php /start.html
I guess I need a redirect for all pure requests to index.php and not those with query strings. Though I can not figure out how to rewrite it.
The reason is that I want all users that enters the site to get a static html of the wordpress site. Only when the user starts to navigate the site should request be made against wordpress.
EDIT: I required the rule to apply only on GET requests
It can be done without mod_rewrite, with mod_dir and the DirectoryIndex Directive.
DirectoryIndex start.html
Add this line:
RewriteRule ^/?$ /start.html [L]
just after this line:
RewriteBase /
In addition to #Oussama solution, as I have a form which posts to itself on the first page. Posting to start.html would not work. I added a condition so that rule only applies to GET requests. That way form post would be sent to index.php as usual.
RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^/?$ /start.html [L]
Final file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^/?$ /start.html [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I hope you can help me with this 301 issue.
Introduction
I've just published a re-design of a website with new clean urls. The old webpage had urls like this: www.domain.dk/Default.aspx?ID=66. And the new website urls look like this: www.domain.com/contact
So I wan't to redirect all these old urls to the new ones, and therefor i'm not keeping the old urls and no general rule can be applied.
That's just simple 301 redirects, but at the same time the old domain points to a new domain, and this is where things get dirty, I think. The old domain was www.domain.dk, but i wan't to 301 all traffic to the new domain www.domain.com and at the same time I wan't to make all the individual 301 redirects.
The problem
When I click on the link www.domain.dk/Default.aspx?ID=66 in Google I get this URL in my browser: www.domain.comindex.php/?ID=66.
On other links I get www.domain.comdefault.aspx/?ID=2
So the redirecting to the new domain works fine? But the individual redirects doesn't apply at all.
The code
This code is pasted as is from my .htaccess file on the server running apache.
The first bit is auto-generated by Concrete5 CMS to make pretty URLs.
# -- concrete5 urls start --
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
# -- concrete5 urls end --
This is the code I found to 301 redirect all traffic to urls that is not using www.esvagt.com to www.esvagt.com
## --- 301 Redirects --- ##
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]
This is just one of the manual 301 redirects.
## General - Redirects ##
redirect 301 /Default.aspx?ID=66 http://www.domain.com/contact/contact-us
Thanks in advance. If you need more information I'll gladly provide that.
I have zero knowledge about writing code in .htaccess, so I'm pretty clueless. I hope you can help. :)
Avoid mixing mod_rewrite and mod_alias rules.
Ordering of rules is also very important so have 301 rules first and then have your catch all controller rule
Use this code for 301 redirect:
## --- 301 Redirects --- ##
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.dk$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [R=301,L]
## General - Redirects ##
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+Default\.aspx\?ID=66[&\s] [NC]
RewriteRule ^ http://www.domain.com/contact/contact-us? [R=301,L]
# -- concrete5 urls start --
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
# -- concrete5 urls end --
RewriteRule www.domain.dk/Default.aspx?ID=66 domain.com/contact/contact-us [R=301,L]
If you want to redirect all posts automatically:
RewriteRule /Artical.aspx?ID=(.+?) domain.com/article-title-$1.html [R=301,L]
or
RewriteRule /Artical.aspx?ID=(.+?) domain.com/Post.aspx?ID=$1 [R=301,L]
Then the dynamic url Artical.aspx?ID=20 will be redirect to http://www.domain.com/article-title-30.html,
I successfully apply this method for my blog http://downloadapp.info
I apologise for a seemingly duplicate question, but none of the dozens I've looked at actually had the same problem.
I have the following directory structure:
/.htaccess
/index.php
/subfolder/.htaccess
/subfolder/index.php
I'd like all requests for pages to be handled by /index.php, unless the request starts /subfolder in which case it should be handled by /subfolder/index.php
e.g. /abc to be rewritten to /index.php?u=abc
e.g. /subfolder/def to be rewritten to /subfolder/index.php?u=def
I've been going round in circles over this, so any help will be massively appreciated.
EDIT: forgot to mention the problem!
Requests within the subfolder are handled by the root index.php, not the subfolder one. (Except requests for /subfolder)
Current File contents
/.htaccess
Options -Indexes -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ /index.php?u=$1 [NC,QSA]
/subfolder/.htaccess
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /admin/index.php?u=$1 [NC,QSA]
Have your root .htaccess like this:
Options -Indexes -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!admin/)(.+)$ /index.php?u=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/(.+)$ /admin/index.php?u=$1 [NC,QSA,L]
There is no need to have .htaccess in admin folder for this simple requirement.
This line of the root folder .htaccess:
RewriteRule ^(.*)$ /index.php?u=$1 [NC,QSA]
is causing all the requests to non-existent filepaths to be redirected to the root folder's index.php. That's the problem.
One possible solution could be to substitute the above line with this couple of lines:
RewriteRule ^subfolder/(.*)$ /subfolder/index.php?u=$1 [L,NC,QSA]
RewriteRule ^(.+)$ /index.php?u=$1 [L,NC,QSA]
By adding the L (last) flag and writing the rules in this order you'll get Apache to redirect correctly your requests, and eliminate the need for rewriting directives into /subfolder/.htaccess .