Redirect part of the URL - .htaccess

I'm trying to redirect all the requested url to /artigo/
(except for links with ?dash)
for example:
example.com/como-aprender-php/ --> example.com/artigo/como-aprender-php
with htaccess

You can use this rule in your root htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^dash [NC]
RewriteRule ^(.*)$ /artigo/$1 [R=301,L]
example.com/como-aprender-php/ --> example.com/artigo/como-aprender-php
example.com/como-aprender-php/?dash --> don't redirect
Note: redirect only applied to non-existing folders/files

Related

.htaccess rewrite domain and subdomain to directory and subdirectory

I'm trying to use my .htaccess file to point domains/subdomains to directories/subdirectories without changing the url in the browser.
Examples of the incoming url and the directory it should point to:
domain1.com/* -> /domain1.com/www/*
foo.domain1.com/* -> /domain1.com/foo/*
bar.domain1.com/* -> /domain1.com/bar/*
domain2.com/* -> /domain2.com/www/*
foo.domain2.com/* -> /domain2.com/foo/*
bar.domain2.com/* -> /domain2.com/bar/*
Here's my current attempt that is continually appending the directory/subdirectory to the url:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:([^.]+)\.)?([^.]+)\.([^.]+)$ [NC]
RewriteCond %{REQUEST_URI} !^/%2\.%3/%1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%2.%3/%1/$1 [L,NE,P,QSA,R]
Why so complicated? The following should satisfy all requirements you listed:
RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+$
RewriteRule ^ /www%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.[^.]+$
RewriteRule ^ /%1%{REQUEST_URI} [L]
Have a try yourself at made with love
It took a little work, but that to the help of the first response, I was able to get everything working eventually.
Here's my final solution to this issue:
## Turn the rewrite engine on to allow for url mapping to work
RewriteEngine on
## Don't require a trailing "/" for directories
DirectorySlash Off
## If subdomain was missing, redirect into the "www" subdirectory
# If no %{REQUEST_FILENAME} was provided, redirect to the "www" subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$ [NC]
RewriteRule ^$ /%2.%3/www/ [END,NC]
# If a %{REQUEST_FILENAME} was provided, redirect to the directory/file in the "www" subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /%2.%3/www/$1 [END,NC,QSA]
# If subdomain was given, redirect into the given subdirectory
# If no %{REQUEST_FILENAME} was provided, redirect to the given subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)$ [NC]
RewriteRule ^$ /%2.%3/%1/ [END,NC]
# If a %{REQUEST_FILENAME} was provided, redirect to the directory/file in the given subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /%2.%3/%1/$1 [END,NC,QSA]

Remove URL trailing slash and redirect

I need some help with editing .htaccess file.
need to remove trailing slash from URL, for example: website.com/file/ to website.com/file. Also redirect to website.com/file when typed website.com/file/
redirect to website.com/file, when user type website.com/file.php/ or website.com/file.php
resolve HTML anchor IDs like website.com/file.php#anchor to website.com/file#anchor or better website.com/file/anchor. Is that second possible?
I tried to remove .php extension from URL and now my CSS/JS files are not working. Is possible to use relative paths to link CSS/JS or just absolute?
My .htaccess file now looks like this:
RewriteEngine On
ErrorDocument 404 https://website.com/404
# Redirect to https
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ https%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove .php extension from URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Any ideas?
Are there some .htaccess "trics" used on websites?
Thanks!

Index Page Showed on All Pages In Directory

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

Redirecting all requests to index.php without direct access

I have the following code in my .htaccess file.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This redirects all requests to index.php. However I am still able to access www.mydomain.com/index.php directly from the URL. As www.mydomain.com servers the same content as www.mydomain.com/index.php will this be recorded as duplicated in google, if so how do I prevent it.
Insert this rule before existing rule to remove index.php from URI:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]

Htaccess redirect static sub-domain to domain

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]

Resources