I need to redirect a large amount of pages that have added random numbers onto the end of the page urls, I need to redirect these pages to a single url but there will be a large amount of urls throughout the website e.g.;
http://www.domain.com/a002-product-ring-50134.html
http://www.domain.com/a002-product-ring-50140.html
http://www.domain.com/a002-product-ring-50145.html
http://www.domain.com/a002-product-ring-50168.html
http://www.domain.com/a002-product-ring-50384.html
http://www.domain.com/a002-product-ring-50393.html
http://www.domain.com/a002-product-ring-50404.html
These need redirecting to http://www.domain.com/a002.html but the only thing i need to query is the number so something like this is what I thought would work;
Redirect 301 /a002-product-ring-(*).html http://www.domain.com/a002.html
You need to use RedirectMatch for regex capabilities:
RedirectMatch 301 (a.+?)-product-ring-[^.]+\.html$ http://www.domain.com/$1.html
We can use RewriteCond and match the uri pattern as following.
RewriteCond %{REQUEST_URI} ^.*(hello.html|contact_us.php)$
RewriteRule ^/(.*) http://www.domain.com/a002.html [R=302,L]
Related
I want to redirect users from www.example.com/ANYTHING to www.example.com.
Is .htaccess the better way to do it? How can I do it?
If you really just have to remove everything after www.mydomain.com, then you just have to delete everything from the first / to the end of the URL:
Search for this regex
%^([^/]*).*$
and substitute with \1, as I did here. Note that I have used the % sign as delimiter instead of /, so I don't need to escape the / in the regex. (I could have used any other available symbol other than /.)
You'll need to use a mod_rewrite RewriteRule (as opposed to a mod-alias RedirectMatch) in order to avoid conflicts with mod_dir and the DirectoryIndex*1.
For example, in .htaccess (Apache 2.2 and 2.4):
RewriteEngine On
RewriteRule . / [R,L]
The single dot matches something (ie. not the document root) and we redirect to the document root.
However, if the document root is an HTML webpage that links to resources like JavaScript, CSS and images then you need to make exceptions for these resources, otherwise these too will be redirected to the root!
For example:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|jpg|png|gif)$ [NC]
RewriteRule . / [R,L]
*1 A mod_alias RedirectMatch directive such as RedirectMatch /. / ends up matching the rewritten request (by mod_dir) to the DirectoryIndex (eg. index.php) resulting in a redirect loop.
I have an old website and I had changed my CMS site to WordPress.
Now I have many error 404 records in my Google Webmaster log.
I want to redirect an URL to a new structure...
Old URL:
www.mysite.com/fa/contents/detailXXXX/postslug
where XXXX is a 4-digit number, e.g. 4256
New URL:
www.mysite.com/postslug
A very simple solution would be:
RewriteEngine On
RewriteRule ^fa/contents/detail[0-9]{4}/postslug$ /postslug [R=301,L]
However if you need to know what digits were in the URL and use it in your script, use a cookie:
RewriteEngine On
RewriteRule ^fa/contents/detail([0-9]{4})/postslug$ /postslug [CO=n:$1:.mysite.com:0:/,R,L]
...or use a query parameter:
RewriteEngine On
RewriteRule ^fa/contents/detail([0-9]{4})/postslug$ /postslug?n=$1 [QSA,R,L]
Going to show my lack of regex knowledge here but seem to be having trouble with the following:
I need to redirect the following example structure of a url:
www.example.com/blog-title-here/tags/this+is+a+long+tag+name
www.example.com/tag/this-is-a-long-tag-name
I need to redirect to just /tag and replace any instance of + with a -
You can use this recursive redirect rule as your very first rule in site root .htaccess:
RewriteEngine On
RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*[ +].*)$" $1-$2 [N,NC,DPI]
RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*)$" /$1-$2 [L,R=301,NC,NE]
This will redirect http://localhost/blog-title-here/tag/this+is+a+long+tag+name to http://localhost/tag/this-is-a-long-tag-name
I'm currently consolidating posts on a site we recently acquired that had multiple WordPress installs to manage content, one in the public_html folder and another in a subdirectory, like so:
1. http://domain.com/
2. http://domain.com/another-install/
We're moving all of the content from /another-install/ into the main setup, and using a 301 redirect to remove /another-install/ from all old links like so:
RedirectMatch 301 ^/another-install/(.*) http://domain.com/$1
Resulting in all articles redirecting like so:
http://domain.com/another-install/article-name/
TO
http://domain.com/article-name/
The problem is, we want to keep /another-install/ viewable as a page. With the current redirect, http://domain.com/another-install/ goes to http://domain.com/. Is there any way to add an exception, or rewrite the current rule so that it keeps /another-install/ viewable?
Change your regex from (.*) (which matches 0 or more of any character) to (.+) (which matches 1 or more of any character). That means there would have to be something following /another-install/ in order for there to be a redirect.
You need a RewriteRule to specify exclusions. Add this to your .htaccess file
RewriteCond %{REQUEST_URI} !^/old-install/(index\.wml)?$ [NC]
RewriteRule ^old-install/(.+)$ http://domain.com/$1 [R=301,NC,L]
I am planning to relaunch a website soon. Part of the relaunch is a switchover from Joomla! to Wordpress.
Besides some old pages, which will be redirected with some exact expression, I have a lot of pages, which will be redirected to the new root page.
Whats the right regular expression, if I want to redirect URLs like these:
http://www.somedomain.com/index.php?somevar=123&com_option=somevalue
http://www.somedomain.com/index.php?com_option=someothervalue
All the URLs have in common:
http://www.somedomain.com/index.php?
+
com_option
How can I redirect all those pages with one line of redirect code in .htaccess?
I assume you do not need somevar and only want to carry over com_option
RewriteEngine on
RewriteBase /
RewriteRule index.php(.*)com_option=(.*)$ http://newsite.com/index.php?com_option=$2 [L,R=301]
You will need to do something like this:
RedirectMatch 301 (.*)/the/old/domain/and/url/(.*) http://the/new/domain/and/path/$2
Now, that the site is gone live, I see that none of the answers to my questions worked out.
After some trial an error I found a solution:
By now Google offers a live-demo:
1) Go to google & search for "site:sqlxpert.de".
2) Click on one off the links having "option" in the URL.
3) You will be redirected to the new pages root - which in this case is the same domain and also an index.php, but the d*** QUERY_STRING is gone.
I needed a .htaccess snippet, which does the following (expressed in words):
If requested file is
INDEX.PHP or INDEX2.PHP
AND
QUERY STRING contains "option"
then 301 REDIRECT to
http://www.sqlxpert.de/
WITHOUT any QUERY STRING
The solution:
RewriteCond %{QUERY_STRING} option
RewriteRule ^index\.php http://www.sqlxpert.de/$1? [L,R=301]
RewriteRule ^index2\.php http://www.sqlxpert.de/$1? [L,R=301]