I have a bunch of HTML files with link tags like this:
[...]
Link 1
Link 2
Link 3
[...]
The website is inside a folder on the server, e.g. http://www.website.com/folder/, but links beggining with / point to the root, e.g. http://www.website.com/, but the files file1.php, file2.php, file3.php, etc. are inside the folder.
How would I be able to make the links act as if the root folder were the folder where they are, without changing HTML contents?
You can try with a internal redirect like this:
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule ^(.*)$ /folder/$1 [L,QSA]
The condition is to redirect only PHP files to the folder, if that gives you a loop you can go with a more specific one:
RewriteRule ^(.*)\.php$ /folder/$1.php [L,QSA]
With RewriteBase something like this:
RewriteBase /folder/
RewriteRule ^(.*)\.php$ $1.php [L,QSA]
Related
How can I redirect every index.html file to /
For example redirect this URL: https://www.example.com/contact/index.html to https://www.example.com/contact/
Note: I don't want to redirect to the root, I want to redirect to the same directory just remove the index.html
Thank you in advance for your time
Assuming you are using Apache the following lines in .htaccess file under the /contact folder will do the trick:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/index.html$ %{CONTEXT_PREFIX}/folder/$1/ [R=301,L]
RewriteRule ^index.html$ %{CONTEXT_PREFIX}/folder/ [R=301,L]
where folder is contact in your example.
Earlier I suggested using the following rule for a shortcut of the above two:
RewriteRule ^(.*)index.html$ %{CONTEXT_PREFIX}/folder/$1 [R=301,L]
However, this has an unwanted side-effect of redirecting say something like https://www.example.com/contact/myindex.html to https://www.example.com/contact/my
Note: Please be careful not to cause redirection "loops" like:
xxx/index.html -> xxx/ -> xxx/index.html -> xxx/ ...
I was in the need of migrating my site to a subfolder, it is a php script, its links are like this :
aff_link.php?id=812 (number changes at the end)
and html files like
gifts.html
rss_feeds.html
and others ending in .html
I need to redirect those two kind of links to a subfolder called "old"
any advice on this?
Thank you in advance.
You can use this rule in site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)q=id=\d+ [NC]
RewriteRule ^aff_link\.php$ /old/$0 [L,NC,R=301,NE]
I have a two files in my root folder: main.html and index.php.
Well, normally index.php takes places as it has higher priority to respond, just wanted to change the logic, so then at first the user has to visit the main.html and then get redirected to index.php. I tried the below code in my .htaccess but it doesn't work as I expected. Actually after doing that, now the first page is correctly changed to main.html, but it never get redirected to index.php!!! It actually returns to main.html again and again (like loop!)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
DirectoryIndex main.html /main.html
ps:
1- even if i try to request the index.php it will open main.html instead!
You can change the default page for directory using the following in your htaccess file, as you have done in the question. Users would land on main.html (instead of index.php) when navigating to "www.coolsite.com/".
//Change default directory page
DirectoryIndex main.html
To redirect the user to another page, you can use the following:
window.location = "http://www.coolsite.com/index.php";
Well, it seems wordpress and static content [index page!] does not not come along! (mostly because of the existing RewriteRule[s] + WP internals)
The workaround is to create a 'page template' (the static content you wish to display as index page) and ask WP to display it as Front-page .
My aim is:
domain.com/folder
rewrite ->
domain.com
this shall concern ALL links inside that site.
I mean on the site are links like:
domain.com/folder/forum.html
domain.com/folder/community.html
etc.
This is my aim:
domain.com/forum.html
domain.com/community.html
etc.
and its very important that the "folder" is never in the url in the adressbar visible.
I tried already many codes but I couldnt really solve this problem.
My best try was with this code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)folder
RewriteRule ^(.*)$ folder/$1 [L]
If I enter
domain.com
I get the content of
domain.com/folder
displayed, what is correct ("folder" is not in the url shown). But when i click on some links of the site like: domain.com/folder/community.html then I can see again "folder" in the url, but I want that it becomes ALWAYS removed.
here is my site:
thewedgiecommunity.x10.mx/wedgiecommunity/
My aim is to remove the "wedgiecommunity" (=folder)
This link is working
thewedgiecommunity.x10.mx/
But when you click on Community (
thewedgiecommunity.x10.mx/wedgiecommunity/community.html
) then i get again "wedgiecommunity" in the URL.
Would be awesome when someone could help me
You can use this code:
Goes in DOCUMENT_ROOT/wedgiecommunity/.htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+wedgiecommunity([^\s]*) [NC]
RewriteRule ^ %1 [R=301,L]
Goes in DOCUMENT_ROOT/.htaccess:
RewriteEngine On
RewriteRule !^/?wedgiecommunity wedgiecommunity%{REQUEST_URI} [L,NC]
You can use this rule to "remove" the folder from the URL when it is accessed directly via the browser:
RewriteCond %{THE_REQUEST} \ /wedgiecommunity/
RewriteRule ^wedgiecommunity/(.*)$ /$1 [L,R=301]
Then your other rule will handle the rest.
files : http://i.imgur.com/M7ioQzB.jpg
So I have an html file called hello.html and a folder called hello. But if you go to website.com/hello it will come come up with a 404 error because it finds the folder and not the html file. I have a .htaccess file that looks like this..
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
But I am not sure what I can do to fix this. I want website/hello to go to hello.html and not the hello folder.
Not sure if this is the best way, but you could have an index.html page redirecting the user to the hello.html page when a user goes to the hello directory.
Try
RedirectMatch 301 ^/hello/ http://www.example.com/hello.html