Here are some examples of the old URLs:
http://www.test.com/files/Old_Name.zip
http://www.test.com/files/Old_Name.php
http://www.test.com/files/Old_Name_Setup.php
http://www.test.com/files/Old_Name_100_Setup.php
Here are what the new URLs look like:
http://www.test.com/files/New_Name.zip
http://www.test.com/files/New_Name.php
http://www.test.com/files/New_Name_Setup.php
http://www.test.com/files/New_Name_100_Setup.php
I need to edit my .htaccess file to reflect these changes, so when people visit the old URLs, they are taken to the new URLs.
I've seen something like
Redirect 301 /files/Old_Name.php http://www.test.com/files/New_Name.php
Unfortunately, this way would require me to do this for every single file, and there are a lot. I'm guessing I'm going to have to use regex, but I haven't seen a solution online that only modifies part of the filename. I'm assuming this should be able to be done in 1 line as the structure change between old and new URLs is consistent.
You can use in /files/.htaccess:
RewriteEngine on
RewriteRule ^Old_Name(.*)$ New_Name$1 [NC,L,R=301]
Or in root .htaccess:
RewriteEngine on
RewriteRule ^files/Old_Name(.*)$ files/New_Name$1 [NC,L,R=301]
Related
I have a url like
domain.com/order/index.php?q=
the order folder no longer exists as its been changed to ecom, so it now looks like the below.
domain.com/ecom/index.php?q=
The problem is we have a ton of external links wanting the order url some scripts and images pull from this url, etc.
How can I simply set .htaccess to still allow the order directory.
I tried this
RewriteRule ^order/(.*)$ /ecom/$1 [L]
It works only if it exactly matches order (e.g domain.com/order/ , but anything after order it then breaks (e.g domain.com/order/index.php?q=)
Any help would be appreciated. I just keep seeing examples similar to what I have above so not sure if I am doing something wrong here.
Use QSA flag
RewriteRule ^order/(.*)$ /ecom/$1 [QSA,L]
you can do a permanent redirect to a page
RewriteRule ^url-string-to-redirect$ http://www.yourdomain.com/your-new-url-string [R=301,L]
I would like to remove the .html extension from my urls, located into specific directory and redirect 301 them.
Here is how the structure looks like:
mysite.com/category/nameofcategory/pagenumber.html
The thing is that nameofcategory and pagenumber could be any letter or number.
Could you please help me with this?
I wouldn't recommend having your content scattered in many html-files in different folders. This becomes very impractical if you for example want to change the layout of your pages.
Storing the content in a database is a much better solution. If that's not possible perhaps the html files could contain only the formatted text content and a back end script could embed that content to a layout when the page is requested.
This requires that the mod_rewrite module is enabled in the Apache configuration.
In both cases all of the requests would be routed through the back end script and the .htaccess might look something like this:
RewriteEngine on
RewriteRule ^category/([^/.]+)/([^/.]+)/?$ index.php?category=$1&page=$2 [L]
This part of the regex: ([^/.]+) matches and captures a string that doesn't contain the characters / or . and is 1 characters long or longer. The captured strings can be referenced with $1, $2 and so on.
Now the pretty urls like mysite.com/category/foo/bar work. In addition we need to define a rule that redirects the old urls ending in ".html". The rule required might look something like this:
RewriteRule ^category/([^/.]+)/([^/.]+).html$ category/$1/$2 [R=301,L]
One thing to remember while testing and adjusting the redirects is that the redirect may get cached in the browser which may lead to confusing results when testing.
To remove the .html extension on the URL and 301 redirect to the extensionless URL you can try the following in the .htaccess in your "specific directory":
RewriteEngine On
RewriteBase /specific-directory
RewriteRule ^(.*)\.html$ $1 [R=301,L]
I have found many 301 htaccess redirects examples but i need something specific and dont know how to build that exact redirect rule... the site i am build this for is a wordpress site.
Example:
http://www.example.com/article/some-article-01.html
http://www.example.com/article/data-analysis.html
http://www.example.com/article/another-page.html
To redirect to:
http://www.example.com/article/some-article-01/
http://www.example.com/article/data-analysis/
http://www.example.com/article/another-page/
looking for one htaccess rule that works in wordpress...
a pointer to relevant data would be accepted hapily. cant seem to find the right search query. An example code would be of course much more easier.
Insert this rule in your DOCUMENT_ROOT/.htaccess file as very first rule just below RewriteBase line:
RewriteEngine On
RewriteBase /
RewriteRule ^(article/[^.]+)\.html$ /$1/ [L,NC,R=302]
If it works for you then change 302 to 301 to make it permanent redirect.
I really can't understand how to write htaccess lines. I was trying to Google for few hours and couldn't find anything relevant. Can anybody suggest me a simple .htaccess line that can let me navigate to http://www.mydomain.com/mydirectory/index5.html
by calling it as http://www.mydomain.com/required
I tried in the below manner but I didn't work for me.
RewriteEngine On
RewriteRule ^required mydirectory/index5.html [NC,L]
And, an other question if I place this .htaccess file in the mydirectory folder will that work or am I supposed to place this file only in the root folder?
Thanks.
Put this code in .htaccess in DOCUMENT_ROOT:
RewriteEngine On
RewriteBase /
RewriteRule ^required/?$ /mydirectory/index5.html [NC,L]
PS: You cannot keep this code in /mydirectory since your original URI doesn't contain /mydirectory
Reference: Apache mod_rewrite Introduction
I have a situation where an entire folder's contents are no longer needed and will be redirected to the home page, except 6 or so files. The folder holds over 300 files, so individual redirects:
redirect 301 /folder/file.html http://www.domain.tld/
redirect 301 /folder/file2.html http://www.domain.tld/
redirect 301 /folder/file3.html http://www.domain.tld/
This would take quite a long time. I have some time before needing this done, and would like to know if anyone knows a good way to achieve this by using a little regex with mod_rewrite.
For optimum understanding for all who may use the potential correct answer, lets say the files that we don't want to redirect are:
/folder/stay1.html
/folder/stay2.html
/folder/stay3.html
Thanks in advance for this wonderful community of very knowledgeable people helping those of us who still have a few things to learn!
Edit
Is it possible to achieve this and keep the base url of the folder?
/folder/
/folder/index.html
I tried the following without success:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder(/|/index.html|/stay1.html|/stay2.html|/stay3.html|/etc.html)
RewriteRule ^/?folder/ http://www.domain.tld/ [L,R=301]
Edit Correct Answer
A big thanks goes out to Jon Lin for the answer.
The correct method to redirect all files of a /folder/ except a few, while still allowing access to /folder/ is:
RewriteEngine On
# Allow /folder/ to remain accessible
RewriteCond %{REQUEST_URI} !^/folder/$
# Allow specified files to remain accessible
RewriteCond %{REQUEST_URI} !^/folder/(index.html|stay1.html|stay2.html|stay3.html|etc.html)
# Redirect all non-specified files to home page
RewriteRule ^/?folder/(.+)$ http://www.domain.tld/ [L,R=301]
Using mod_rewrite, you can create exception conditions to the redirect, try putting these rules in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/$
RewriteCond %{REQUEST_URI} !^/folder/(index.html|stay1.html|stay2.html|stay3.html|etc.html)
RewriteRule ^/?folder/(.+)$ http://www.domain.tld/ [L,R=301]
So anything that's in the list: (stay1.html|stay2.html|stay3.html|etc.html) will fail the condition and the redirect won't happen. Otherwise, anything starting with /folder/ will get redirected to http://www.domain.tld/.
Note that if you have mod_alias redirects intermixed, they may interfere with each other.
You could use RedirectMatch with a negative lookahead, like:
RedirectMatch permanent ^/?folder/(?!(stay1\.html|stay2\.html|stay3\.html)) http://domain.tld
An alternative mod-rewrite solution would be like this:
RewriteRule ^/?folder/(stay1\.html|stay2\.html|stay3\.html)$ - [L]
RewriteRule ^/?folder/.* http://domain.tld
The first rule catches all the exceptions, the L flag ensures no further processing takes place in this pass, and the - instructs the engine not to rewrite, ensuring no further passes are made. Anything not caught by the first rule is redirected by the second rule.