mod_rewrite rule (dynamic to static URI) not working - wordpress

I am trying to rewrite the following dynamic URI:
http://domain.com/dictionary/?h=%E8%AF%91
To this:
http://domain.com/dictionary/%E8%AF%91
Note that %E8%AF%91 is the Chinese character 译.
I am using the following mod_rewrite in .htaccess:
RewriteEngine on
RewriteRule ^dictionary/([^/\.]+)/?$ index.php?h=$1
I have also tried:
RewriteEngine on
RewriteRule ^dictionary/([^/\.]+)/?$ dictionary/index.php?h=$1
I am not sure why it is working. Some theories that I have:
Chinese characters need a particular workaround for mod_rewrite
WordPress's rewrite rules are messing with mine
I have checked and mod_rewrite is definitely activated.

I suggest capturing against %{THE_REQUEST} with a rewritecond which will give you the original, client-encoded form of the URL in e.g. %1.
You should then find it much easier to re-substitute that capture into the new URL.

Related

Apache rewrite to map # based URL into proper URLs

Long version (you can skip to TL;DR if you want to):
I am working with a Wordpress site that was set up by someone else. The website has multiple pages where page has tabbed content which is accessible through #. For eg:
www.example.com/services/category1/#tab-service1
www.example.com/services/category1/#tab-service2
www.example.com/services/category2/#tab-service1
www.example.com/services/category2/#tab-service2
www.example.com/services/category2/#tab-service3
Now, when search engines index they are indexing only www.example.com/services/category1/ and www.example.com/services/category2/. This creates a problem where we cannot have search engines point directly to the content within a given tab. What we want is for search engines to show links that takes users directly to (say) www.example.com/services/category2/#tab-service3.
Now, I don't think google can index such # based content on its own. So, I am thinking of using apache rewrites to try to resolve this issue. I have access to .htaccess file only (from a config perspective).
TL;DR
How to redirect www.example.com/services/category1/service3/ to www.example.com/services/category1/#tab-service3 using apache redirects (I have access to .htaccess file)?
This is what I am trying but it's not working:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9])/? [NC]
RewriteRule .* /services/category1#tab-%1 [R,NE,L]
Someone also adviced to look into pushState server config to fix this. I am not sure how to use pushState.
UPDATE:
I have updated the rewrites to the following but it still doesn't work. It keeps showing Wordpress' 404 page
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /domainfolder/
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9]+)/?$ [NC]
RewriteRule ^/services/category1/([a-z0-9]+)/?$ /services/category1/#$1 [NE,R,L]
</IfModule>
Your %{REQUEST_URI} regex is wrong. The pattern ^/services/
category1/([a-z0- 9 ])/? matches /services/category1/{any 1 char of a-z or 0-9} format followed by an optional slash. So this does not match your request /services/category1/service3 but matches /services/category1/a/ .
You should be using
^/services/category1/([a-z0-9]+)/?$

I cant seem to find how to redirect a trailing url

I cant seem to find how to redirect a trailing url from:
www.domain.com/index.php?option=com_k2&view=itemlist&task=user&id=*
To:
www.domain.com/
Any suggestions?
You can use a RewriteRule in apache configuration. Enable mod_rewrite and .htaccess through httpd.conf. Then, in .htaccess file in root directory place the following
RewriteEngin On
RewriteBase /
RewriteCond %{QUERY_STRING} ^option=com_k2&view=itemlist&task=user&id=.*$
RewriteRule ^index\.php.*$ '' [R,L,QSD]
You have to use RewriteCond to parse the query string, then the RewriteRule uses the flag R to redirect, L to stop processing additional rules, and QSD avoids appending the query string onto the URI. Since you are in web root, you can probably get away without using RewriteBase, but I used it for testing this code because I tested it with .htaccess in a subdirectory. So for someone who wants to do something similar in a subdirectory, you would change the RewriteBase to /sub1/sub2/ to rewrite the url
www.domain.com/sub1/sub2/index.php?option=com_k2&view=itemlist&task=user&id=*
to
www.domain.com/sub1/sub2/
If you do not want the browser to report the change, then use the passthrough flag PT instead of redirect R. There are other helpful flags for case-insensitve matching and more; you can view those in apache docs.

Rewrite Rules OR Redirects using percentage Signs and hyphens

I attempted to look this up in various forums but couldn't find anything that actually worked for me. I'm trying to redirect or force rewrite rules on a set of URLs that have spaces OR '%20' and essentially break when trying to resolve a redirect. Old site is a statis site to new site which is Wordpress which shouldnt matter.
This is what I have :
Redirect 301 http://www.exampledomain.com/pdf/PA%20article%20-%20Cross%20Selling.pdf http://www.exampledomain.com/wp-content/uploads/2014/12/PA_article_-_Cross_Selling.pdf
Which doesnt work and ends up just going back to the root location. Alternatively, I tried to do a RewriteRule that didn't make any difference either below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^pdf/PA%20article%20-%20Cross%20Selling\.pdf$ /wp-content/uploads/2014/12/PA_article_-_Cross_Selling.pdf?&%{QUERY_STRING} [B,r=301,nc]
Cant anyone else look at this and tell what I am doing wrong
The following RewriteRule uses \s to match any whitespace character:
RewriteRule ^pdf/PA\sarticle\s-\sCross\sSelling.pdf$ wp-content/uploads/2014/12/PA_article_-_Cross_Selling.pdf

HTACCESS - Change Directory Structure

I recently changed our URL structure for several different pages but I'm having some trouble with the HTACCESS rewrites. I've included the examples below and I'm hoping that someone can help me with the correct rule to use!
http://www.tintworld.com/ny/albany-022/home-window-tinting/ <-- Original URL
http://www.tintworld.com/albany-ny-022/home-window-tinting/ <-- New URL ('ny' is moved)
I'm currently trying to use the following rule but it's not redirecting the original URLs...
RewriteCond %{HTTP_HOST} ^www\.tintworld\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ny/albany-022/$ http://www.tintworld.com/albany-ny-022? [R=301,NE,NC,L]
Please help. :-)
The problem is likely due matching ^ny/albany-022/$ in your RewriteRule. The $ in regular expressions denotes the end of the line. Try removing the $, and adding the remaining part onto the rewritten URL
RewriteRule ^ny/albany-022/(.*) http://www.tintworld.com/albany-ny-022/$1
The $1 here puts in the part that's matched by the (.*).
if you have several different pages, I would recommend using a plugin like
http://www.weberz.com/plugins/404-redirected/
to keep track of 404 pages and redirect them

Using htaccess to rewrite special characters for Wordpress url aliases

I recently migrated a fairly large site (~6,000 posts) from Drupal to Wordpress. As part of the process, I migrated the Drupal-created url aliases to Wordpress for SEO and link retention purposes.
An example of a url alias that Drupal created that worked great in Drupal:
/stories/will-this-be-another-la-niña-year
That url in Wordpress returns a 404. However, this works:
/stories/will-this-be-another-la-nina-year
It seems then my best bet is to write a generic international character to english character set rewrite rule in htaccess, before the url is passed to Wordpress.
Any idea how I might do this?
Thanks a lot for whatever help you can give.
Matt.
It seems like there might be a better way to do this within wordpress, you may want to do a quick browser through the wordpress Trac tickets, there maybe some patch or temporary fix for the problem. But if you need to go to an htaccess/redirect method, you can either use a RewriteMap to sanitize and redirect-if-needed or explicitly redirect on non-ascii characters.
A RewriteMap requires access to either server or vhost config to setup the map. It could be as simple as a list of /stories/will-this-be-another-la-niña-year URIs mapped to http://yourdomain.com/stories/will-this-be-another-la-nina-year (the all ascii URL, the http:// is significant because it tells mod_rewrite to redirect the browser). Or you can write a script to look for non-ascii characters and replace them with the appropriate ascii character.
Text mapping:
RewriteMap sanitize txt:/path/to/uri_mapping.txt
Script mapping:
RewriteMap sanitize prg:/path/to/sanitize_script.php
Then in your htaccess file, you can invoke this mapping like this (these rules will need to be above the wordpress rules, since you want the URI sanitized before wordpress gets a hold of them.
RewriteRule ^(.*)$ /${sanitize:$1|$1} [L]
If you don't have access to server/vhost config, you'll have to enumerate the possibilities in your htaccess file, again putting these rules above the wordpress rules:
# replace ñ
RewriteRule ^(.*)ñ(.*)$ /$1n$2 [R=301,L]
# replace ú
RewriteRule ^(.*)ú(.*)$ /$1ú$2 [R=301,L]
etc.
I just added the following lines at the beginning of my .htaccess file and it works:
RewriteRule ^(.*)é(.*)$ /$1e$2 [R=301,L]
RewriteRule ^(.*)è(.*)$ /$1e$2 [R=301,L]
RewriteRule ^(.*)ê(.*)$ /$1e$2 [R=301,L]
RewriteRule ^(.*)î(.*)$ /$1i$2 [R=301,L]
RewriteRule ^(.*)ô(.*)$ /$1o$2 [R=301,L]
RewriteRule ^(.*)û(.*)$ /$1u$2 [R=301,L]
RewriteRule ^(.*)â(.*)$ /$1a$2 [R=301,L]
RewriteRule ^(.*)à(.*)$ /$1a$2 [R=301,L]
RewriteRule ^(.*)ï(.*)$ /$1i$2 [R=301,L]

Resources