Deleting a directory from the path to the URL using .htaccess - apache

There is a link like
domain.com/dir/page
It displays the contents of the page page.php, which is located in the dir directory of the site.
Current view .htaccess at the root of the site:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule .* http://%1/$0 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php/
RewriteCond %{HTTPS} =off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]
ErrorDocument 404 /404.php
How do I replace the URL with domain.com/dir/page to domain.com/page (that is, remove dir from the path)?
P.S. It is understood that the solution should work for all URLs of the specified type.

Try using this in your .htaccess file, make sure it is above any other rules that you have:
RewriteEngine On
RewriteRule ^dir/(.*)$ /$1 [L,R=301]
Clear your cache before you test this.

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

htaccess point request to sub-directory if file doesn't exists and does under the subdirectory

I'm having a really hard time with trying to get this to work so I have a sub-directory example.com/projects with a directory under that called test and I'd like it if someone typed example.com/test it would show the file from example.com/projects/test but if the file does not exist I would like it to post a 404 error.
Additionally if possible I would like any time example.com/projects/test was typed it would redirect to example.com/test instead but if someone goes to just projects/ I want them to be able to stay there.
I have tried the following .htaccess files so far
This works almost perfectly passing through the files unless the file doesn't exist in which it throws a 500 Internal Server Error
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ projects/$1 [PT]
I thought something like this might work to only pass through if the file existed under projects but doesn't work at all:
RewriteCond projects/%{REQUEST_FILENAME} -f
RewriteCond projects/%{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ projects/$1 [PT]
For the second part pointing anything that goes to /projects/test to strip /projects I tried this (placed before the code above)
RedirectMatch ^/projects/(.+)$ /$1
Only to find a redirect loop. How can i prevent this?
My current .htaccess file for reference:
ErrorDocument 404 /404.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example2\.com$
RewriteRule ^/?$ "http\:\/\/example3\.org" [R=301,L]
#RedirectMatch ^/projects/(.+)$ /$1
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ projects/$1 [PT]
You can use these rules in your root .htaccess:
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{THE_REQUEST} /projects/(\S+)\sH [NC]
RewriteRule ^ /%1 [L,R=302,NE]
RewriteCond %{DOCUMENT_ROOT}/projects/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/projects/$1 -d
RewriteRule ^(.+)$ projects/$1 [L]

File download links returning 404 after .htaccess rewrite

I rewrote my .htaccess to define a couple of redirects:
non-www to www
feed to blogs/feed
admin to blogs/wp-admin
All these redirects are working perfect. However, this seems to have broken my file download links. For example, I have a download links pointing to:
Mysitedomain/decks/50_Essential_Spanish_Verbs.apkg
I know for a fact that the file IS there. Yet every time I click the link, it throws a 404 instead of letting you download the file. Is my htaccess rewrite to blame for this issue? Any suggestions on how to fix it?
Here's what my .htaccess currently looks like:
<IfModule mod_rewrite.c>
RewriteEngine on
Header set Access-Control-Allow-Origin "*"
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
#RewriteRule ^/(.*)$ http://www.thesite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
#Rewrite rule for blog feed
RewriteRule feed blogs/feed/burrorss [NC,L]
#Rewrite rule for blog admin
RewriteRule admin blogs/wp-admin [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
</IfModule>

.htaccess conflictions on different directories

Probably a duplicate question but here goes!
I'm not great at htaccess stuff. I've been using codeigniter and have multiple projects going on currently. I've made a redirect in my public_html to redirect to a directory called "home" right? And in this direct is the base for a codeigniter application where I have another .htaccess file removing the index.php from the url for controllers etc.
My question is are these two files conflicting one another? Here's my code:
public_html/.htaccess
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^myurl.co.uk$
RewriteRule (.*) http://www.myurl/home/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.myurl.co.uk$
RewriteRule (.*) http://www.myurl/home/$1 [R=301,L]
/home/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Also another question would be could I display my /home/index.php file on landing on myurl.co.uk without having home in the url?
In my public_html directory htaccess files I have
RewriteEngine on
RewriteCond %{HTTP_HOST} ^myurl.co.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.myurl.co.uk$
RewriteCond %{REQUEST_URI} !home/
RewriteRule (.*) /home/index.php/$1 [L]
And I changed my base url to http://www.myurl.co.uk/ whereas before I had http://www.myurl.co.uk/home. Ma bad!

Remove .html from URLs with a redirect

We have a website, unfortunately all the URLs have the .html suffix, its a Magento installation, Magento allows you to change this on the CMS, but again, unfortunately all this URLs with .html suffix have a good ranking in Google. We need to redirect to non .html.
So, consider the following scenario, we are rebuilding this site from scratch, so we have the same urls on the new site but without the .html suffix.
Now is: www.example.de/cool-shoes.html
Will be: www.example.de/cool-shoes
So www.example.de/cool-shoes.html will not exist anymore, and I've been trying a redirect with the .htaccess with no luck.
I've tried so far:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule (.*)index\.html$ /$1 [R=301,L]
and:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
but it doesn't seem to work...any ideas?
Ok so, after some research, and failing to achieve this with a rewrite rule, the following line of code worked:
redirectMatch 301 ^(.*)\.html $1
This is quite usefull to remove any url extension and avoid broken links, hopefully helps someone in the future...
cheers!
This will rewrite the url like so http://example.com/page.html -> http://example.com/page
# Remove .html from url
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Try adding the following to the .htaccess file in the root directory of your site redirect URLs with .html extension and remove it.
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
Here's the solution that worked for me.
RewriteCond %{THE_REQUEST} \.html [NC]
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
Follow the steps, and you'll be able to remove .html from url without modifying .htaccess file.
This should do the trick:
RewriteEngine On
RewriteRule ^(\w+)\.html$ /$1 [R=301,L]
Try this to putting in your .htaccess file
Redirect permanent www.mysite.de/cool-shoes.html
www.mysite.de/cool-shoes
this may be helpful to you
This is for URLs ending with .html /product/raspberrypi.html ---> /product/raspberrypi/ (/product/raspberrypi/index.php) the index.php is hidden. Took me awhile to figure this out. LOL...
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You have to use 'REQUEST_URI' and add it before index redirect rules since it could be overridden by the application. Its important to know that its URI not a filename or directory we are trying to redirect, since the file names all have index.php in the root folders(Wordpress).