I have been tearing my hair out trying to solve an issue with htaccess on wordpress network site and i've gained so much information from stackoverflow in the past that i thought this would be the best place to ask. And apologies up front if im not posting correctly, it my first time.
here are the specifics of my setup (i cannot show or allow access to the site as i have agreed to an NDA of sorts)
there are 2 sites. the first is the root site "/" and the second is "/mythoughts/" as shown in network admin.
the "/mythoughts/" site is a replacement for an old custom blog someone built that uses variables in the url (custom)
the themes i am using are twentyten and roots (obviously roots doesnt do tidy url rewrites as its on a network setup)
the problem is this.
first "index.php is being removed from the url (its not a problem, but i think it might cause problems when i try to do other rewrites.)
second, the old site has variables in the urls in 2 instances.
the first instance is this
www.thesite.com/mythoughts/index.php?year=2010&month=9
which i need to rewrite as
www.thesite.com/mythoughts/2010/9
first of all the index.php is automatically removed
i have tried so many different things like
RewriteRule ^mythoughts/index.php?year=(.*)&month=(.*)$ mythoughts/$1/$2 [R=301,L]
does not work
the second instance is this
www.thesite.com/mythoughts/index.php?thought=101
which should rewrite to
www.thesite.com/mythoughts/title-of-the-post
i have a script that can match the url variable to the title of the post (and replace remove illegal characters) but the rewrites just do not work.
here is the htaccess i currently have (a bare wordpress)
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
#END WordPress
if anyone can help in pointing me in the right direction on this whole htaccess thing (which i have been struggling with for the last few weeks) i would be totally greatful.
thanks in advance guys and girls.
In the first instance, you have:
RewriteRule ^mythoughts/index.php?year=(.*)&month=(.*)$ mythoughts/$1/$2 [R=301,L]
But you can't match against the query string in a rewrite rule. Also, since you've got a rule to rewrite it back, you'll just create a redirect loop. You need to match against the actual request and not the URI.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /mythoughts/index\.php\?year=([^&]*)&month=([^&\ ]*)
RewriteRule ^ mythoughts/%1/%2 [R=301,L]
As for:
i have a script that can match the url variable to the title of the post (and replace remove illegal characters) but the rewrites just do not work.
I don't see any script in your htaccess, maybe if you include the rule or the script.
after continuing my search even though i posted the question on stack (the great) overflow
here is what i ended up with.
in the first instance with the double url string the answer was
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /mythoughts/index\.php\?year=([0-9\-]+)&month=([0-9\-]+)\ HTTP/ [NC]
RewriteRule ^index\.php$ http://site.co.uk/mythoughts/%1/%2? [R=301,L]
so the initial url would have been
www.site.co.uk/mythoughts/index.php?year=2010&month=12
and the rewrite result is
www.site.co.uk/mythoughts/2010/12/
PERFECT !!!!
in the second instance, first to answer Jons question.
the script is written with php, it scans through the old database concatin strings from the id of the url variable, as example the variable in the url below
www.site.co.uk/mythoughts/index.php?though=21
this post or database entry has a title, and in wordpress the title is used in the permalink
so it would be
www.site.co.uk/mythoughts/sublime-and-powerful-outcomes
the script takes the id for the first condition of the rewrite, and puts the id in its place. The second part takes the title of the post and swaps spaces with dashes and special characters removed.
which results in the new url.
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /mythoughts/index\.php\?thought=21
\ HTTP/ [NC]
RewriteRule ^index\.php$ http://site.co.uk/mythoughts/sublime-and-powerful-outcomes? [R=301,L]
thanks to Jon for taking the time to answer this.
hope this helps someone
Related
I know there is many questions like this, but i’ve been trying this for over 3 days now and still no success.
I have wordpress site which i want to redirect entirnely to another site besides one url: www.oldsite.com/adv
If i setup normal redirect for entire site it works but when I try to excluce certain folder from redirecting it refuses to work.
I have tried following code in my htacess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/adv
RewriteRule (.*) http://www.newsite.com/$1 [R=301,L].
but this redirects all pages from oldsite including oldsite.com/adv
also I have tried:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}!^/my-folder/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
but this one gives 500 error.
do you have an idea what am i doing wrong?
RewriteEngine on
RewriteRule ^adv - [L,NC]
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
It will match any URI beginning adv.
The - means do nothing.
The L means this should be last rule; ignore everything following.
The NC means no-case (so "ADV" is also matched).
I have my wordpress installed on www.site.com/blog/ but i would like to add language codes in front of the blog directory (www.site.com/us/blog/, www.site.com/es/blog/ and so on). Is there a way achiving this? I need to keep those virtual directories inside URI so when visiting www.site.com/us/blog i want to show www.site.com/us/blog in the address bar.
This is my .htaccess file from www.site.com/blog/ directory.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteRule ^(.*)/blog/$ blog/?language=$1
it's been a while since i've written .htaccess, let me know how this works out.
edit: here's a helpful tutorial on how it works :)
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
This should work for you:
RewriteEngine on
RewriteRule ^[a-z]{2}/(.*)$ /$1 [QSA,L]
It checks for any 2 letter prefix at the start of the URL, followed by anything else, then treats that as a request for the bit after, so /us/blog will be treated as a request for /blog, just as /es/page/123 would be treated as a request for /page/123. So long as a visitor goes to the prefixed URL, the address won't be redirected.
If you want to be more specific about what language prefixes to use, do this instead:
RewriteEngine on
RewriteRule ^(es|fr|us)/(.*)$ /$2 [QSA,L]
Making sure you add the ones you want between the first set of parantheses (and separated by pipes (|) as shown above.
I know this is not the first time someone has asked this, but I tried other solutions and they don't seem to work.
So I have a website with a SSL certificate and a redirect rule in my .htaccess file to point visitors to https. However, there is one specific page I want to exclude from SSL.
This is the code in my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
So my question is, how do I create a rule that excludes www.example.com/shop from SSL?
Most likely you can use another RewriteCond on the redirect to block redirecting for that particular URL. However, how are you going to handle the resources loaded by that page? It will load stuff like images, CSS, Javascript, etc. Should those be loaded over plain old HTTP too? And what happens when something clicks on a like to view their shopping cart, will it go to another URL (and thus go over HTTPS again)?
I'm not entirely sure what you want to achieve, but it sounds a bit silly to me. Why would you want to exclude only one particular page from a safe & secure connection? (And why wouldn't you serve the whole site over TLS?) But maybe you tell us the reason for this so maybe we can help you find a different solution?
I am using Wordpress Multisite, and some of my pages I have built using javascript. So naturally, my SEO sucks. So I have created a phantom.js prerender.io service hosted on myurl.com:3000
I am trying to redirect google bots to use the prerender service. I need to pass the full url to the prerender service like this: http://myurl.com:3000/http://sub.myurl.com/cats/are/cool
As you can see, I am using subdirectory wordpress multisite, so the subdomain and domain need to be passed.
First Problem
This is the original wordpress .htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
I have tried to use this:
RewriteEngine On
RewriteCond %{QUERY_STRING} _escaped_fragment_
RewriteRule ^(.*) http://service.prerender.io/http://example.com [P,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
...
Just for testing, I run http://myurl.com?_escaped_fragment_= in my browser and I get a page like this:
Not Found
The requested URL / was not found on this server.
It doesn't even seem to redirect to http://service.prerender.io. I'm guessing there is some kind of conflict with the wordpress rules.
Update: This issue was fixed by turning on mod_proxy, thanks to #Jon Lin
Second Problem
Once I pop in the right url (http://myurl.com:3000), I have no idea how to grab the entire URL to give to the service! I have googled and google, and I can only find information about how to get url parameters after the domain.
First problem first though, second is only bonus. I can discuss that in another question when I am able to tested it better because the first problem is fixed.
Thanks to #Jon Lin for the mod_proxy answer.
For the second answer, check the following gist for the rewrite rule for grabbing the entire URL to give to the service. It does a quick check to make sure you are most likely trying to view an html file, then adds the entire URL on the end of the request.
https://gist.github.com/thoop/8072354
Basically:
RewriteRule
^(?!.?(.js|.css|.xml|.less|.png|.jpg|.jpeg|.gif|.pdf|.doc|.txt|.ico|.rss|.zip|.mp3|.rar|.exe|.wmv|.doc|.avi|.ppt|.mpg|.mpeg|.tif|.wav|.mov|.psd|.ai|.xls|.mp4|.m4a|.swf|.dat|.dmg|.iso|.flv|.m4v|.torrent))(.)
http://service.prerender.io/http://%{HTTP_HOST}%{REQUEST_URI} [P,L]
I'm doing SEO optimization for a website and I need to transform a part of the url into a parameter. The URL format can still be changed if needed.
Basically I'm working with Wordpress and want to create a single page that would work for all products, but I need to get a specific product number from the URL.
The URL looks something like this:
www.example.com/product/[product-id]-brand-name-and-some-other-stuff
I want to transform the URL behind the scenes (meaning the URL won't change) to this:
www.example.com/product/?pid=[product-id]
I don't have much experience with htaccess and any help would be appreciated.
Here's the current htaccess file's content:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /downloads/www.bqwatches.com/
RewriteRule ^watch/([^-]+)- watch/?bqn=$1 [L,QSA,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /downloads/www.bqwatches.com/index.php [L]
</IfModule>
# END WordPress
Try this rule just below RewriteBase line of your regular wordpress .htaccess:
RewriteRule ^watch/([^-]+)- watch/?pid=$1 [L,QSA,NC]
Update: Altered the solutions to account for the fact that a product id consists of two letters, then a number.
You do the following:
RewriteRule ^product/([a-zA-Z][a-zA-Z][0-9]+).* product/?pid=$1
To explain what's happening, it matches the part after the URL that begins with product/, followed by two letters and several numbers, and then any other characters. The letters-and-numbers get put into the pid parameter of the requested URL, and the rest is discarded.
If you pass it http://www.example.com/product/af1234-some-product, it converts behind-the-scenes to http://www.example.com/product/?pid=af1234.
You could also accept any two characters followed by a number, by doing the following:
RewriteRule ^product/(..[0-9]+).* product/?pid=$1
If the product id could be any character that isn't a -, then the following should achieve the same:
RewriteRule ^product/([^-]+).* product/?pid=$1