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

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

Related

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

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.

.htaccess redirection to directories depends on URL

1) I have such directory structure:
.htaccess
album/
index.php
web/
.htaccess
index.php
...
...
In 'album' there is a gallery page and in 'web' there is a WordPress page.
How to fill .htaccess (I suppose in root dir) to redirect addresses depends on what is in URL:
www.domain.org -> goto /web and display wordpress page
www.domain.org/album -> goto /album and display gallery page
I've tried such thing in root .htaccess but without success (there is constant redirection to domain.org/web):
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^(.*)$ /web
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org/album$ [NC]
ReWriteRule ^(.*)$ /album [L]
.htaccess in web directory is default of WordPress:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /web
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
How to solve this riddle?
Regards.
------------------
One of possible answer to thequestion (
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^album(.*)$ album$1 [L]
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule web/$1 [L]
Weard is last line of this code. There are two spaces. I suppose first space means "empty string". This is strange (and also bad coding) however it works and I have enough of those redirections.
You can use these rules in root .htaccess:
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^((?!album).*)$ web/$1 [L,NC]

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 404 Stopped Working

I had a custom 404 error redirect page working just fine through htaccess. However, after adding new code to htaccess, it stopped working. What's causing the conflict and how can I fix it?
EDIT: I've tried putting the ErrorDocument line at the top of the page and it still doesn't work.
htaccess code:
RewriteEngine On
#Removes www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule . http://%1%{REQUEST_URI} [R=301,L]
#Redirects to extensionless php urls
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^&\ ]+).php
RewriteRule .* /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L]
#Redirect nonexistent files to a 404 page (not working)
ErrorDocument 404 /404.html
The problem is not about putting ErrorDocument in the top or in the bottom of your htaccess.
You have an infinite loop because of your rule (the one rewriting to php extension).
You need to check if it exists before rewriting it, otherwise you'll get a loop conflict between your rule and ErrorDocument.
You can replace your current code by this one
RewriteEngine On
#Remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
#Hide php extension (only for existing files)
RewriteCond %{THE_REQUEST} \s/(.+)\.php(?:\s|\?) [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%1? [R=301,L]
#Redirect to extensionless php urls (only if it exists)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L]
#Redirect nonexistent files to a 404 page
ErrorDocument 404 /404.html
put the errorDocument statement at the top of the file. Above the RewriteEngine Statement.
This is the solution, however I can't tell you why the above htaccess code causes that to fail.

htaccess - canonical URL when redirecting to subdirectory

I've been playing around with this problem for quite a while but can't find the way how to achieve what I want. I want user coming to my website test.com to reach index.php located in subdirectory www immediatelly, which I am able to do. But when the URL is test.com/www I want it to be just test.com.
Snippets of code:
.htaccess in /
RewriteRule ^(.*)$ /www/$1 [L]
.htaccess in /www
RewriteCond %{REQUEST_URI} ^/www.*$
RewriteRule ^/www/(.*)$ /$1 [L]
gives me 500 Internal Error.
UPDATE:
I came to a solution thanks to #Justin Iurman's answer:
.htaccess in /www
RewriteCond %{THE_REQUEST} \ /www/?(.*)\ HTTP/ [NC]
RewriteRule ^(.*)$ /%1 [R=301,L]
Have another problem tho. On the server machine I have multiple websites and I want to serve files according to HTTP_HOST variable in root .htaccess file. I redirect user accessing test.com to proper directory (say test), but I want URL test.com/test to redirect just to test.com. Directory test contains directory www where user is redirected and it does not stick to URL thanks to solution above. I would like to edit just .htaccess file in webserver root so I don't have any dependancy on websites' domains in projects.
SOLVED
.htaccess in webserver root:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect test.com/test (and everything into test folder) to test.com root
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteCond %{THE_REQUEST} \ /test/? [NC]
RewriteRule ^.*$ / [R=301,L]
# Forward (internally rewrite) test.com/one/example/page to test.com/test/www/one/example/page
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
.htaccess in the "test" directory:
<IfModule mod_rewrite.c>
RewriteEngine On
# allow /test in URL only for certain filetypes
RewriteRule ^(.*\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz))$ /test/www/$1 [L]
# allow /test on localhost
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
Thanks a lot Justin!
You have to avoid infinite loop when doing what you want.
Put this code in your htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} \ /www/?(.*)\ HTTP/ [NC]
RewriteRule . /%1 [R=301,L]
RewriteRule ^(.*)$ /www/$1 [L]
EDIT: taking your update into consideration, here's the new code
RewriteEngine on
# Redirect test.com/test (and everything into test folder) to test.com root
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteCond %{THE_REQUEST} \ /test/? [NC]
RewriteRule . / [R=301,L]
# Forward (internally rewrite) test.com/one/example/page to test.com/test/www/one/example/page
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]