301 Redirecting Root Files - file

I'm having issues with files in my root folder as well as files with the same name in the sub-folders being redirected. Here's my htaccess file:
rewriteengine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
redirect 301 /newsletters/ http://www.mydomain.com
redirect 301 /sample.html http://www.mydomain.com
The folder redirect works fine, but sample.html will redirect (great!) but so will /folder/sample.html /randomfolder/folder/sample.html and so on (not so great).
Any ideas? I've tried just using sample.html without the slash prefix but it simply doesn't redirect at that point.
Thanks!

Use a RewriteRule directive with a regular expression. Start with a ^ to only match the start of the string.
RewriteRule ^sample.html$ http://www.mydomain.com

Related

.htaccess redirect to index.html and index.php doesn't work together

I have a project with angularjs for front end and slim for backend api. Therefore I want to map any api/(...) to server/index.php, and map anything else to index.html. However I can't get them work together with .htaccess. api/(...) will direct to index.html instead of index.php.
This is the htaccess I'm using:
RewriteEngine on
RewriteBase /
RewriteRule ^api/ server/index.php [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !\.private/
RewriteRule \.(jpg|png|js|css|html)$ - [L]
RewriteRule ^.* index.html [L]
This is the file structure I have now (with some files omitted):
root/
index.html
server/
index.php
.htaccess
I tried to add some random characters in .htaccess file, and got a 500 error, so the .htaccess file works. I also tried to remove the last line for index.html rule, and the api works after commenting out.
Please help me with this problem, thanks!
I finally figured out why from the best answer in this link -> view
The problem is that [L] command will not terminate the rewriting process totally, it will match from the beginning again. So in the first iteration, api/ will be changed to server/index.php. Then in the next iteration, server/index.php will be matched to index.html.
Apache will terminate rewriting process when the pattern and substitute are the same. So the way to work around it is to add termination rules. I update my htaccess, and now it works.
RewriteEngine on
# Final rule for assets
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !\.private/
RewriteRule \.(php|jpg|png|js|css|html)$ - [L]
# Final rule for back end index
RewriteRule ^server/index\.php$ - [L]
# Redirect to back end index
RewriteRule ^api$ server/index.php [L]
RewriteRule ^api/.* server/index.php [L]
# Final rule for front end index
RewriteRule ^index\.html$ - [L]
# Redirect to front end index
RewriteRule ^.* index.html [L]
I know that the question got answered but it might help those people who want to host their React (front end) and Laravel (backend) on cpanel of GoDady.
Assuming that you people know the basics of cpanel, first go to the public_html and to your folder where your domain or subdomain is located.
you need to upload the application to the public html but remember when you are building react static files (npm run build) you need to define the domain name or the sub folder name with the key homepage in you package.json
or if you want to add the static files in sub folder then it is must to do
"homepage": "http://ghulam.covidbaml.com/yourSubFolderNameHere",
so, I am assuming my folders structure on the cpanel (public_html)
public_html/Static React build Files
public_html/ server (the server folder is also located in same place which contained laravel files)
Now we can write .htaccess which is really imported assuming godady linux hosting.
htaccessFile below:
Options -MultiViews
RewriteEngine on
# Rule to start the laravel api on work
RewriteRule ^server/public/index\.php$ - [L]
# Redirect to index.php
RewriteRule ^api$ server/public/index.php [L]
RewriteRule ^api/.* server/public/index.php [L]
# now I am redirecting the React index file (index.html)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
note:
RewriteRule ^ index.html [QSA,L]
The above command is for the purpose of react routing that it doesn't give 404 error while you refresh the page.

How can I change the website document root?

I want to move to my document root from public_html to public_html/new_version/. I did this using .htaccess
RewriteRule ^$ new_version/ [L]
RewriteRule (.*) new_version/$1 [L]
It works fine but the links in my CakePHP application are appearing: domain.com/new_version/the-link-is-shown-here/
I want to either change completely the document root or have the links appear without the "/new_version/" folder name in them.
You can set App.baseUrl config in core.php to /.

How to make mod_rewrite silently redirect and pass parameter to index when the {REQUEST_FILENAME} is an existing directory or file

Synopsis:
localhost/root/admin is an existing folder
I want /root/admin to go to /root/index.php?url=admin without changing the url in the address bar.
The only except is if the requested file is an image
This is what I have written for the .htaccess file in localhost/root to try and do all this:
RewriteEngine On
RewriteBase /root/
RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !(.*\.(png|jpg|gif))$ [NC]
RewriteRule (.*) index.php?url=$1 [QSA,L]
What this does instead is redirect localhost/root/admin to localhost/root/index.php and changes the url in the address bar to localhost/root/admin/?url=admin.
It will only not do this if I add a trailing slash (/) onto "admin".
I know that, on my mac laptop, this mod_rewrite code does exactly what it is meant to, but only when running it in the "Sites" folder and not in the localhost htdocs folder.
Any thoughts on what I can do to fix this?
I think you're just missing the "PT" flag to prevent an actual redirect. PT (Passthrough) means that Apache redirects the resource served internally, but the browser sees the same URL. (See the Apache wiki for more details: http://wiki.apache.org/httpd/RewriteFlags/PT)
RewriteEngine On
RewriteBase /root/
RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !(.*\.(png|jpg|gif))$ [NC]
RewriteRule (.*) index.php?url=$1 [PT,QSA,L]
Note the only change was to add the flag. I tested this on my server and saw the behavior that I think you want.

htaccess various rewriting needs

I have a website let's say http://myweb.com and I want to migrate it into a subfolder of the root directory http://myweb.com/subfolder/
I need the following things, I guess using the htaccess file:
1) Redirect all non-www to www
2) Redirect all files in the root to equivalent files in the subfolder at the same time changing all htm extension to html, that is a file in the root file.htm must be rewritten to a file in the subfolder like subfolder/file.html
3)I want that in the address is still displayed www.myweb.com rather than www.myweb.com/subfolder
is it all possible? And how? Thanks for any answer
Add these rules to the htaccess in your document root:
RewriteEngine On
# 1) Redirect all non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
# 2) Redirect all files in the root to equivalent files in the subfolder
# at the same time changing all htm extension to html
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteRule ^(.*)\.htm$ /subfolder/$1.html [L]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteRule ^(.*)$ /subfolder/$1 [L]
The last rules don't have the R flag so they won't cause an external redirect (thus changing what's in the browser's URL address bar).

Help with my .htaccess file

I don't know why this is happening but I can access html pages WITHOUT typing in the .html extension. This is causing duplicate content.
Example: If you type in
http://www.math-aids.com/Addition/Single_Digitwill go to the
http://www.math-aids.com/Addition/Single_Digit.html file.
This is happening for all of my files.
I do not want that to happen. If someone does not type in the .html for a file, then I want it to return a 404 error " file not found ".
How can I fix this? I completely removed the .htaccess file and it still happens.
Here is my .htaccess file.
Options +FollowSymlinks
rewriteengine on
RewriteBase /
### re-direct IP address to www
### re-direct non-www to www
RewriteCond %{http_host} !^www.math-aids.com$ [nc]
RewriteRule ^(.*)$ http://www.math-aids.com/$1 [r=301,nc,L]
### re-direct index.html to root / ###
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*\/index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ "/$1" [R=301,L]
### re-direct index.php to root / ###
RewriteCond %{THE_REQUEST} ^.*\/index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
### re-direct default.html to root / ###
RewriteCond %{THE_REQUEST} ^.*\/default\.html\ HTTP/
RewriteRule ^(.*)default\.html$ /$1 [R=301,L]
### re-direct home.html to root / ###
RewriteCond %{THE_REQUEST} ^.*\/home\.html\ HTTP/
RewriteRule ^(.*)home\.html$ /$1 [R=301,L]
### re-direct errors to error page
ErrorDocument 404 /missing.html
How can I fix this? I completely removed the .htaccess file and it still happens.
This is because the .htaccess file you posted doesn't append .html suffixes to anything. It must be another .htaccess file or something in your main Apache configuration.
It sounds like you have "Options MultiViews" somewhere in your Apache configuration (not the .htaccess file).
See http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html#multiviews

Resources