.htaccess file redirecing incorrectly - apache

After creating a dev site on a new server the htaccess file is redirecting links incorrectly. For example if clicking on the link example.com/test from example.com it is trying to redirect to example.com/example.com/test. How can I correct the htaccess file below:
<IfModule mod_rewrite.c>
# Turn on the Rewrite Engine
RewriteEngine On
RewriteBase /
# If the file or directory exists, show it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# Blank queries get sent to the index
RewriteRule ^$ /index.php [L]
# All other queries get sent to the index as index.php/whatever
RewriteRule ^(.*)$ /index.php?$1 [L]
</IfModule>
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>

Insert this rule just below RewriteBase to redirect /example.com/test to /test:
RewriteRule ^example\.com/(.+)$ /$1 [L,NC,NE,R=302]

Related

Is there any function to change the URL Path of website?

I would like to change the path of the website from http://localhost/en-in/pan
to http://localhost/en-in/individual/pan
without creating a folder individual.
Kindly let me know if there is anything.
Following is the .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteRule ^(.*)index$ /$1 [R=301,L]
RewriteEngine On example.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
ErrorDocument 404 https://www.example.com/404
ErrorDocument 500 https://www.example.com/404
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
RewriteEngine On
RewriteBase /en-us
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$
It depends on the server you are running on.
On apache, this can be achieved by creating a .htaccess file on your root directory of the website and add there the Redirect command:
Redirect /en-in/pan /en-in/individual/pan
Create a .htaccess file under your base directory (public_html or www) and put the content:
RewriteEngine On #remove this line if it doesn't work
RewriteRule en-in/individual/pan(/?)$ en-in/pan [QSA,L]
Then you can redirect your traffic from active link to the new link

Alternative procedure to prerender(not using prerender.io) in htaccess. Full custom code recommended

Is there any alternative procedure not to use prerender.io or custom way to implement it by writing code in htaccess? Need help. I am using angular 4 and apache server in back. Prerender.io asking for subscriptions.Please if anyone can help me writing the same custom code in htaccess.Here is my code in htaccess.
<IfModule mod_headers.c>
RequestHeader set X-Prerender-Token "xxxxxxxxxxxxxxxxxx"
</IfModule>
<IfModule mod_rewrite.c>
DirectoryIndex
RewriteEngine on
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Options +FollowSymLinks
#RewriteRule ^api/(.*)$ http://weedbuys.com/api/$1 [P,L]
# Prerender.io stuff
<IfModule mod_proxy_http.c>
RewriteCond %{HTTP_USER_AGENT} Baiduspider|DoCoMo|Twitterbot|
TweetmemeBot|Twikle|Netseer|Daumoa|SeznamBot|Ezooms|MSNBot|Exabot|MJ12bot|sogou\
sspider|bitlybot|ia_archiver|proximic|spbot|ChangeDetection|NaverBot|MetaJobBot|magpiecrawler|Genieo\sWeb\sfilter|Qualidator.com\sBot|Woko|Vagabondo|360Spider|ExB\
sLanguage\sCrawler|AddThis.com|aiHitBot|Spinn3r|BingPreview|GrapeshotCrawler|
CareerBot|ZumBot|ShopWiki|bixocrawler|uMBot|sistrix|linkdexbot|AhrefsBot|archive.
org_bot|SeoCheckBot|TurnitinBot|VoilaBot|SearchmetricsBot|Butterfly|
Yahoo!|Plukkie|yacybot|trendictionbot|UASlinkChecker|Blekkobot|Wotbox|YioopBot|
meanpathbot|TinEye|LuminateBot|FyberSpider|Infohelfer|linkdex.com|Curious\sGeorge|FetchGuess|ichiro|MojeekBot|SBSearch|WebThumbnail|socialbm_bot|SemrushBot|Vedma|alexa\ssite\
saudit|SEOkicks-Robot|Browsershots|BLEXBot|woriobot|AMZNKAssocBot|Speedy|oBot|
HostTracker|OpenWebSpider|WBSearchBot|FacebookExternalHit [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
# Only proxy the request to Prerender if it's a request for HTML
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|
\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.
mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.
dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent))(.*) http://service.prerender.io/http://weedbuys.com/$2
[P,L]
</IfModule>
# Don't rewrite files or directories, but exclude adminer directory
RewriteRule ^(adminer)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^adminer - [L,NC]
RewriteRule ^ index.html [L]
</IfModule>

htaccess redirect directory to single page in same directory

All attempts failed... I want http://olex-design.at/journal to to be redirected to http://olex-design.at/journal/alle through .htaccess.
So far I have the following - non working - rule in place which produces a 403 forbidden page.
RewriteRule ^journal/$ /journal/alle [R=301,L]
The .htaccess file is placed in the root directory. Maybe someone can point me towards the right direction...
All rules in .htaccess files:
Options -Indexes -MultiViews
RewriteEngine on
#Force non-www:
RewriteCond %{HTTP_HOST} ^www\.olex-design\.at [NC]
RewriteRule ^(.*)$ http://olex-design.at/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
RewriteRule ^journal/$ /journal/alle [R=301,L]
ErrorDocument 404 /404
With the support of my webhost I was able to resolve my issues described above. This is the complete code in my .htaccess file.
Please note that the first 4 lines did the trick. The code below is responsible to remove the "www", remove the "index" when your in home (http://olex-design.at) and remove the .html file extension.
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^journal\/?$ "http\:\/\/olex\-design\.at\/journal\/alle" [R=301,L]
AddDefaultCharset utf-8
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.olex-design\.at [NC]
RewriteRule ^(.*)$ http://olex-design.at/$1 [L,R=301]
<IfModule mod_rewrite.c>
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\ HTTP/
RewriteRule ^index\.html$ http://olex-design.at/ [R=301,L]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

htaccess subdirectory + 301 on www

I'd like to do 2 things with htaccess but I can't figure out how to do it.
Let's say my domain is domain.com
First, I'd like to force www. in the url with a 301 redirect.
Another thing, my website is not hosted in the root directory but in /laravel/public/
So I'd like to set this subdirectory as root, and remove it from the URL if someone try www.domain.com/laravel/public/ => www.domain.com
How can I do that?
Thanks in advance.
Assuming that you can't change the document root to point to your public folder, you can try adding these rules to document root (not your public folder):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /+lavarel/public/([^\?\ ]*)
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/laravel/public/
RewriteRule ^(.*)$ /laravel/public/$1 [L]
You'll need to have your own .htaccess file in the root of your domain, along with another in the laravel/public directory (there already is one there, but you need to modify it.
Below are the two files in full as I have them on my testing server.
/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Rewrite requests to the laravel/public index
RewriteCond %{REQUEST_URI} !^/laravel/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /laravel/public/$1 [L]
</IfModule>
/laravel/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Force www. (also strips laravel/public)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Strip laravel/public, prevent loops
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} ^/laravel/public(/.+)/? [NC]
RewriteRule ^ %1 [R=301,L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

.htaccess rewriterule to create subdomains

I want to create subdomains, so that I can use the address test.mydomain.com.
After searching the web, and a lot of trial, I got this in my .htacces file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com(:80)?$
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/$1 -d
RewriteRule (.*) /subdomains/%1/$1 [L]
</IfModule>
If I write http: //test.mydomain.com/folder/ in my browser everything is fine.
But if I write http: //test.mydomain.com/folder (without ending slash), it gets redirected to http: //test.mydomain.dk/subdomains/test/folder/, and this is the address shown in the browser (which I don't want)
My question is:
How do I prevent the redirect?I want to only see http: //test.mydomain.com/folder in the browser. Regardless of the ending slash.
My web files is like this:
.htaccess
index.php (page for mydomain.com)
\subdomains
\test
\hello
index.php (page for test.mydomain.com/hello)
Thanks.
(sorry about the spaces in the url's. It won't let my post with links)
got it to work now:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
# If requested URI does not end in "/"
RewriteCond $1 !/$
# add a trailing slash
RewriteRule ^(.+)$ /$1/ [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com(:80)?$
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/%{REQUEST_URI} -f
RewriteRule (.*) /subdomains/%1/$1 [L]
</IfModule>