I would like help in rewriting a url eg domain.com/home to domain.com/folder/ using Htaccess. I have a folder named /var/www/home, accessing it I would type the Ip address followed by folder name eg 127.0.0.1/home. I want to access the same directory in the url by typing eg 127.0.0.1/folder. and also when the user types 127.0.0.1/home he/she is directed to 127.0.0.1/folder. I believe I can use Htaccess but I don't know how.
You can use these 2 rules in your site root /var/www/.htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+home(\S*)\s [NC]
RewriteRule ^ /folder%1 [R=302,L,NE]
RewriteRule ^folder(/.*)?$ home$1 [L,NC]
Related
I am able to achieve what I want partially by doing the following...
Putting this (see below) on DomainA .htaccess will do what I want. But I have to be specific at to what the folder name is... How would I wildcard it so that the folder name is captured and passed on to forwarding domain?
RewriteEngine On
RewriteRule ^folderA/(.*)$ http://domainB.com/folderB/$1 [L,R=301]
trying to achieve
domainA.com/folder/123 -> domainB.com/folder/123
Edit: I noticed an issue after implanting a solution. IF the redirecting domain i.e. DomainA.com does not have the folder on the site then the rewrite does not work.
trying to achieve domainA.com/folder/123 -> domainB.com/folder123
You can use a pattern instead of using folderA:
RewriteEngine On
RewriteRule ^([^/]+)/(.*)$ http://domainB.com/$1$2 [L,R=301,NC]
In the case when domainA and domainB are on same host and same DocumentRoot then you must add a RewriteCond to check for domainA being the original request's domain like this:
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule ^([^/]+)/(.*)$ http://domainB.com/$1$2 [L,R=301,NC]
I would like to write a rule to redirect that kind or URL :
https://www.mywebsite.com/blog/wp-content/uploads/2007/06/DSC_1690.jpg
TO
https://www.mywebsite.com/wp-content/uploads/2007/06/DSC_1690.jpg
without the blog directory but ONLY for /blog/wp-content, i don't want to remove the /blog enretirely, because there is the blog here. i just want to move the wp-content at the root folder.
for all files inside /blog/wp-content to /wp-content
i tried that rule but it doesn't work : RewriteRule ^blog/wp-content/(.*)$ wp-content/$1 [L,NC,R]
Thanks for your help
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/blog/(wp-content/)(.+)\sHTTP.*$
RewriteRule ^blog/wp-content/(.*)$ /%1$1 [L,R=301]
RewriteRule ^wp-content/(.*)$ /blog/wp-content/$1 [L,NE]
With rules above , you will be able to hide /blog/wp-content from URI but still get the same path internally.
Clear browser cache then test
I would like to change the URL structure of my site. My current URL structure is like this:
www.domain.com/events/events.php?location=san%20francisco
I want to change the URL structure to:
www.domain.com/events/san-francisco
My XMPL site map has the URLs listed like the first example. I want the search engines to index the URL like the second example. What are all the things I need to do to achieve this? The {city} in the location parameter is dynamic depending on the user's IP address. Do I need to change my XML sitemap to list new URL structure? What do I need to put in my .htaccess file. Do I need to change the actual URLs links on my website to the new structure or can I just use .htaccess?
This should work:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /events\.php\?location=(.*)%20(.*)\ HTTP
RewriteRule ^ /events/%2-%3\? [R=302,L]
RewriteRule ^events/(.*)$ /events.php?location=$1 [L]
Changing R=302 to R=301 when you know it redirect correctly
EDIT:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?location=(.*)%20(.*)&lid=(.*)&slid=(.*)\ HTTP
RewriteRule ^ /%3/%4-%5/%6/%7\? [R=302,L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ /$1/$1.php?location=$2&lid=$3&slid=$4 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?location=(.*)%20(.*)\ HTTP
RewriteRule ^ /%3/%4-%5\? [R=302,L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$1.php?location=$2 [L]
You have to use a .htaccess file similar to the one below. It will match fancy URL and forward the request to the right php file.
RewriteEngine On
RewriteRule ^([^/]*)/(.*)$ /$1/$1.php?location=$2
With this, both www.domain.com/events/events.php?location=san%20francisco and www.domain.com/events/san-francisco will lead to the same page.
But if you don't change your sitemap nor the links on your site, search engines won't know that.
You have then 2 solutions :
change all your links in your site and sitemap with the new structure URL
use a redirect rule to redirect "old" pages to the "new" ones
From what I know about SEO, the first option is the best. Or even better, a combination of both solutions : change all your links, and use a redirect 301 rule so pages already indexed by search engines will not lost their ranking and to avoid duplicate content.
RewriteRule ^([^/]*)/(.*).php?location=(.*)$ http://www.yourdomain.com/$1/$3 [R=301]
How can I use htaccess to always ADD a directory immediately after the domain name?
So for example, change requests for
http://domain.com/path-to/file.php
to
http://domain.com/added-directory/path-to/file.php
The context here is that i am migrating a site to a new server, and the domain name is not yet pointed to the new server. But the hosting company provides me with a "temporary url" based on the Shared IP and my account username, so http://216.172.172.211/~myusername/ , but all the paths in all the html are doc-root relative, like /images/logo.png, which translates to http://216.172.172.211/images/logo.png which is wrong. I need it to be http://216.172.172.211/~myusername/images/logo.png .
try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/~myusername
RewriteRule ^(.*)$ /~myusername/$1 [L]
If you want to redirect so that URLs show the ~myusername part in the URL address bar, add an R flag to the square brackets:
RewriteRule ^(.*)$ /~myusername/$1 [L,R=301]
This worked for me perfectly
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/~gye
RewriteRule ^(.*)$ https://%{HTTP_HOST}/gye/$1 [R=301,L]
If an URL comes without the /XYZ
Add https:// at the beginning and /gye/ after the domain.
Testing here really helped: https://htaccess.madewithlove.be/
I am building a download site, An users will be able to register and a folder with their username will be created on my server, something like: home/users/username
What I want to accomplish is, if anyone types: username.domain.com in their browser, it will route them to: home/users/username/, and if they type: username.domain.com/file.mp3, it will route them to: home/users/username/file.mp3
If its possible to accomplish sub folders routing, that would be great full aswell, example; home/users/username/sub/file.mp3
Thanks guys
Try adding the following to the .htaccess file in the root directory of your site.
This will rewrite any request to username.domain.com to the correct folder/subfolder and file.
I am assuming that home is a directory in the root folder of your site.
RewriteEngine on
RewriteBase /
#if request for usename.domain.com/anything
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
#send any request that is not already for home/users to home/users/username/anything
RewriteRule ^(?!home/users/) home/users/%1%{REQUEST_URI} [L,NC]
I tried what Ulrich Palha recommended but no luck. I hope this will help guys like me where Ulrich's answer doesn't work.
# Rewrite username.domain.com/<path> to domain.com/home/username/<path>
#
# Rewrite only if not already rewritten to /home/
RewriteCond $1 !home/
# Skip rewrite if subdomain is www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
# Rewrite to /home/username/URL-path
RewriteRule (.*) /home/%1/$1 [L]