how are you? please one very little question if you don't mind.
after the previous episode of redirecting and rewriting rules in this thread: redirect to default language except for /amp/ Urls, our SEO agency recommended us to change the URLs site.com/fr, site.com/be to site.fr and site.be
I already set the 301 redirections for old URLs as follow:
RewriteRule ^[a-z]{2}$ / [R=301,L]
RewriteRule ^[a-z]{2}/(.*)$ /$1 [R=301,L]
I know this can be achieved in one rule but didn't manage to make it work :/
since I now removed the language folder, removing the trailing slash does't really work well. site.com/test adds the slash because the folder "test" exists and is a real folder. I tried DirectorySlash Off but it returns a forbidden 403 error.
please any idea how to solve this?
Thanks a lot
Following my comments:
Combining those 2 shown rules into one:
RewriteRule ^[a-z]{2}(?:/(.*))?$ /$1 [R=301,L,NE]
Related
I'm having some real issues getting a redirect to work in .htaccess with drupal 7.
I have links like this:
example.com/vid/category/filter?subcat_filter_depth=125
and I need them to go to: (on same domain)
example.com/vid/category/filter?type=All&subcat_filter_depth=125
after = the number may be different like 67 or 32 etc. meaning that can be any number and whatever number is at the end of the original link needs to also be at the end of the new link.
I think the fact that the link is pretty much the same path just different filter is what is causing the issue as even the Drupal redirect url module will not allow it at an attempt to add them one at a time as they occur.
here is everything I've tried:
RedirectMatch 301 ^/vid/category/filter?subcat_filter_depth=(.*) /vid/category/filter?type=All&subcat_filter_depth=$1
RewriteRule ^(.*)/vid/category/filter?subcat_filter_depth=(.*) $1/vid/category/filter?type=All&subcat_filter_depth=$2 [R=301]
RedirectMatch ^/vid/category/filter?subcat_filter_depth=(.*) ^/vid/category/filter?type=All&subcat_filter_depth=$1
RedirectMatch 301 ^(.*)/filter?subcat_filter_depth=(.*) ^$1/filter?type=All&subcat_filter_depth=$2
RedirectMatch ^/(.*)/filter?subcat_filter_depth=(.*) ^/$1/filter?type=All&subcat_filter_depth=$2
RewriteRule "^/(.*)/filter?subcat_filter_depth=(.*)" "^/$1/filter?type=All&subcat_filter_depth=$2"
mod_rewrite is on and there is an AllowOverride pathed/to/the/.htaccess file under <Directory> inside <VirtualHost> in the 000-default.conf running on Debian Apache ..pretty much latest versions.
I know mod_rewrite is working because if I add a bad rule or rewrite with a syntax error I get error 500 response page.
I've searched and tried just about everything I can think of and nothing seems to work.
Any help getting this sorted and functioning would be greatly appreciated!
Both the RedirectMatch (mod_alias) and RewriteRule (mod_rewrite) directives match against the URL-path only, which notably excludes the query string.
To match against the query string you need to use RewriteRule with an additional condition (RewriteCond directive) that checks against the QUERY_STRING server variable.
This rule also needs to go near the top of the .htaccess file, before any existing rewrites.
For example:
RewriteCond %{QUERY_STRING} ^subcat_filter_depth=\d+$
RewriteRule ^vid/category/filter$ /$0?type=All [QSA,R=302,L]
This redirects example.com/vid/category/filter?subcat_filter_depth=<number> to example.com/vid/category/filter?type=All&subcat_filter_depth=<number>.
In .htaccess there is no slash prefix on the URL-path matched by the RewriteRule pattern. ie. ^vid/category (not ^/vid/category).
$0 is a backreference that contains the matched URL-path from the RewriteRule pattern.
QSA causes the original query string (eg. subcat_filter_depth=125) to be appended to the query string stated in the substitution string, ie. type=All becomes type=All&subcat_filter_depth=125.
Test with 302 (temporary) redirect to avoid potential caching issues. Only change to a 301 (permanent) redirect - if that is the intention - once you have confirmed it works as intended.
You will likely need to clear your browser cache before testing.
I was able to come up with a solution that worked prior to the answer (above) I marked as correct as my solution isn't quite as elegant.
I did realize after a bit of more research I needed to use a RewriteCond with %{QUERY_STRING} for what I was trying to achieve.
Again not as elegant as the answer above...but here is my code:
RewriteCond %{REQUEST_URI} ^/vid/category/filter [NC]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)
RewriteRule (.*) /vid/category/filter?type=All&subcat_filter_depth=%2 [R=301,L]
This actual kept the url in the browser as it was input (/var/category/filter?subcat_filter_depth=) but showed the page as if it was opened with the proper filter for type=ALL and produced no errors both in browser and in Drupal dblog.
The above solution is more useful in explanation alone and gave me much better understanding of how to break up a URL and put it back together with modifications especially when dealing with strings that have variables that change like unique ID's.
I need help creating a rewrite rule/301 redirect for the following link structures in the .httaccess folder.
Source URL: www.example.com/sub_directory/product_name_1.html
Destination URL: www.example.com/prodcut-name-1.html
The requirements for the redirect then are as follows:
Remove /sub_directory/
Change all underscores '_' to hyphens '-'
Unfortunately my regex isn't very good. I've tried searching around and but the solutions from other post with similar issues where not working for me (such as here)
Any help on a solution for this would be much appreciated. Also if you could please explain the why/how of it. I'd like to be able to better understand this.
Answer from #Walf is close but requires some changes e.g. regex anchors and DPI flag.
You can use these rules on top of your site root .htaccess:
RewriteEngine On
# remove /sub_directory/ when there is no _ left
RewriteRule ^sub_directory/([^_]+)$ /$1 [R=301,NC,NE,L]
# use recursion based rule to replace _ by -
RewriteRule ^(sub_directory/[^_]*)_+(.*)$ $1-$2 [NC,N,DPI]
# rest of your rules go here
Something like this
RewriteEngine on
RewriteBase /
RewriteRule ^(sub_directory/[^_]*)_+(.*) $1-$2 [DPI,N]
RewriteRule ^sub_directory(?:$|/(.*)) http://%{HTTP_HOST}/$1 [R=301,L]
It first loops through URLs that are in that subdirectory, to replace consecutive underscores with a single hyphen. It's done first so it doesn't interfere with other URLs that may contain an underscore. It then externally redirects (the cleaned) requests for that subdirectory to the root. The ugly grouping makes sure it only applies to exactly that folder.
I finally got around to using htaccess to properly redirect my www pages to non-www urls, but I can't seem to get it to work in any way I've tried. I believe something that was included in the htaccess before I changed it is causing the problem.
I added
#www redirect
RewriteCond %{HTTP_HOST} www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301]
in this http://pastebin.com/vAQ45Mky, but it seems to ignore it completely. My night spent searching for answers has turned up nothing useful, so thanks for any help you can give!
That's a big file, and I don't know what you've tested, but the first thing I'd try would be adding an L flag:
RewriteRule (.*) http://%1/$1 [L,R=301]
Without that flag, it'll keep trying the other rules, and apply those that hit. At which point I'm not sure if it is even defined what happens when first applying an absolute-URL rewrite, then (one or more) path rewrites, but I would not be surprised to see the hostname part of the first rewrite lost.
I need a little help with my .htaccess before I deploy it!
I want to 301 redirect almost everything from elementalthreads.com to ethreads.com, excluding blog/wp-content/uploads, and /pommo.
Am I doing this right?:
RewriteEngine on
#exclude old uploads folder and /pommo
RewriteCond %{REQUEST_URI} !^/(blog/wp-content/uploads|pommo) [NC]
RewriteRule (.*) http://ethreads.com/$1 [R=301,L]
Will that transfer canonical pagerank?
Here's where I know I need help:
The old site has a wordpress blog, which I've cloned on the new domain. I'd love to preserve the permalinks, which are almost 1:1, eg:
http://www.elementalthreads.com/blog/ethreads-now-on-amazon-com/ redirects to
http://ethreads.com/ethreads-now-on-amazon-com/ (note /blog/ is missing here)
And the blog index http://www.elementalthreads.com/blog/ should redirect to http://ethreads.com/blog/, which seems like an exception to the above rule, since "/blog/" should only be preserved here?
I'm stumped about how to regEx or otherwise define these last two conditions/rules. Any help would be most appreciated!
That looks correct to me. However, you should not put this live without checking it, there really is nothing preventing you from being able to test it. One thing to bare in mind is that browsers can cache 301 response codes so when testing you should use [R,L] as your flags. Once you are happy add the [R=301,L] back in before deployment.
OK for points (1) & (2)
# only redirect the blog direcotry
RewriteRule ^blog/?$ http://ethreads.com/blog/ [NC,R=301,L]
# redirect all sub folders of blog to the new domain
RewriteRule ^blog/([\w-])/?$ http://ethreads.com/$1/ [NC,R=301,L]
I’ve written some code on my .htaccess file which allows the use of SEO friendly URLs instead of ugly query strings. The following code rewrites the SEO friendly version in the browser to the query string version on the server.
RewriteEngine On
RewriteRule ^seo/([^/]*)/$ /directory/script.php?size=large&colour=green&pattern=$1 [L]
So that the ugly
http://www.mysite.com/directory/script.php?size=large&colour=green&pattern=striped
Is now beautiful
http://www.mysite.com/directory/seo/striped/
Just to explain the code a bit; seo is there to add more keywords to the URL, /directory/ is the directory in which the .htaccess file is located, parameters size=large and colour=green never change, while pattern=$1 can be many different values.
The above code works perfectly. However, the problem is I am now stuck with two URLs that point to exactly the same content. To solve this, I would like to 301 redirect the old, ugly querystrings to the SEO friendly URLs. What I have tried so far does not work - and Google is not being particularly friendly today.
Can anybody offer working code to put in my .htaccess file that redirects ugly to new URL, while retaining the rewrite? Thanks!
This should do the trick:
RewriteEngine On
## Redirect to pretty urls
# The '%1' in the rewrite comes from the group in the previous RewriteCond
RewriteCond %{REQUEST_URI} !seo
RewriteCond %{QUERY_STRING} ^size=large&colour=green&pattern=([a-zA-Z]*)$
RewriteRule (.*) /directory\/seo\/%1\/? [L,R=301]
## Rewrite to long url, additional parameter at the end will cause
## the internal redirect not to match the previous rule (would cause redirect loop)
RewriteRule ^directory\/seo\/([^/]*)/$ /directory/script.php? size=large&colour=green&pattern=$1&rewrite [L]
You can also match the size and colour if needed, by changing those to regex groups as well, and using the corresponding %N
Hope this helps.
Not tested, but this may work...
RewriteRule ^directory/script.php?size=large&colour=green&pattern=(.*)$ /seo/$1/? [R=301,NE,NC,L]