Solved

Wordpress RewriteRule problem

Posted on 2013-06-14
18
370 Views
Last Modified: 2013-08-22
I'm not very familiar with Wordpress but can't understand why I can't get this to work.

I've created a page in Wordpress with url www.mysite.com/property/region

Now I want any url with anything after region to go to the above page.
So www.mysite.com/property/region/anything will go to www.mysite.com/property/region

Having Googled I decided easiest solution would be to amend the .htaccess file.

I've tried adding

RewriteEngine On
RewriteRule ^property/region/(.*)$ property/region [L]

and

RewriteEngine On
RewriteRule ^property/region/(.*)$ /?page_id=69695 [L]

both before and after the # BEGIN WordPress section and each time I get a 404

What am I doing wrong?

Here's the .htaccess file contents before my additions:
DirectoryIndex index.html index.htm index.php
AddType application/x-httpd-php5 .php .htm .html



# BEGIN W3TC Browser Cache
<IfModule mod_deflate.c>
    <IfModule mod_headers.c>
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
        AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/json
    <IfModule mod_mime.c>
        # DEFLATE by extension
        AddOutputFilter DEFLATE js css htm html xml
    </IfModule>
</IfModule>
<FilesMatch "\.(css|htc|js|js2|js3|js4|CSS|HTC|JS|JS2|JS3|JS4)$">
    <IfModule mod_headers.c>
         Header set X-Powered-By "W3 Total Cache/0.9.2.8"
    </IfModule>
</FilesMatch>
<FilesMatch "\.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|HTML|HTM|RTF|RTX|SVG|SVGZ|TXT|XSD|XSL|XML)$">
    <IfModule mod_headers.c>
         Header set X-Powered-By "W3 Total Cache/0.9.2.8"
    </IfModule>
</FilesMatch>
<FilesMatch "\.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip|ASF|ASX|WAX|WMV|WMX|AVI|BMP|CLASS|DIVX|DOC|DOCX|EOT|EXE|GIF|GZ|GZIP|ICO|JPG|JPEG|JPE|JSON|MDB|MID|MIDI|MOV|QT|MP3|M4A|MP4|M4V|MPEG|MPG|MPE|MPP|OTF|ODB|ODC|ODF|ODG|ODP|ODS|ODT|OGG|PDF|PNG|POT|PPS|PPT|PPTX|RA|RAM|SVG|SVGZ|SWF|TAR|TIF|TIFF|TTF|TTC|WAV|WMA|WRI|XLA|XLS|XLSX|XLT|XLW|ZIP)$">
    <IfModule mod_headers.c>
         Header set X-Powered-By "W3 Total Cache/0.9.2.8"
    </IfModule>
</FilesMatch>
# END W3TC Browser Cache
# BEGIN W3TC Page Cache core
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteRule .* - [E=W3TC_ENC:_gzip]
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} =""
    RewriteCond %{HTTP_COOKIE} !(comment_author|wp-postpass|w3tc_logged_out|wordpress_logged_in) [NC]
    RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index.html%{ENV:W3TC_ENC}" -f
    RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index.html%{ENV:W3TC_ENC}" [L]
</IfModule>
# END W3TC Page Cache core
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Open in new window

0
Comment
Question by:fionafenton
  • 8
  • 5
  • 3
  • +1
18 Comments
 
LVL 15

Expert Comment

by:Ess Kay
Comment Utility
simple, create a redirect page

look up the widgets, i seen a few free ones out there
0
 
LVL 1

Author Comment

by:fionafenton
Comment Utility
Sorry, don't understand?
0
 
LVL 14

Expert Comment

by:Dzynit
Comment Utility
Are you adding those rewrite rules after the wordpress rewrite rule? They look correct and should work for you.

If you are, another thing might be that you're stuck in cache. I see you have a lot of caching setup in the htaccess also.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
The initial comment refers to using a plugin to redirect to the desired page. I wouldn't do this, though.

You're in the right ballpark with using the .htaccess file, what may have been throwing you is the [L] flag.

Try:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^property/region/(.*)$ http://yoursite.com/property/region
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
0
 
LVL 1

Author Comment

by:fionafenton
Comment Utility
I've tried adding rule before and after wordpress rewrite rule. They look correct to me too, which is why I don't understand why it's not working.

As I originally said, I'm not very familiar with Wordpress (and haven't actually done the install and setup for this site). How do I clear the cache?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
1) Did you try what I posted above?  It's subtly different that what you tried to do.

2) Your caching shouldn't be an issue but if you want to disable the W3 Total Cache plugin while you work on this, do so.
0
 
LVL 1

Author Comment

by:fionafenton
Comment Utility
Jason, if I understand correctly, what you're suggesting is altering the code between the
# BEGIN WordPress and # END WordPress. Won't this just get overwritten later?
I've tried removing the [L].
I have also tried using the full url - though I want to avoid this as it's currently on a test domain.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
Won't this just get overwritten later?

Shouldn't, unless you are changing permalink structure around for some reason.

I've tried removing the [L].
I have also tried using the full url

As I have it above or the way you were trying it?

though I want to avoid this as it's currently on a test domain.

So you change it when you move from test to live.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:fionafenton
Comment Utility
I've tried it exactly as you posted and I'm afraid I still get a 404.
If I paste the link directly into the browser i.e. http://yoursite.com/property/region/anything it goes to the correct page.

So, I'm guessing that somewhere my RewriteRule is getting ignored.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
Wait, what?

I've tried it exactly as you posted and I'm afraid I still get a 404.
If I paste the link directly into the browser i.e. http://yoursite.com/property/region/anything it goes to the correct page.

If it goes to the correct page when you paste in the browser, that indicates it is working.  Where is the 404 coming from?
0
 
LVL 1

Author Comment

by:fionafenton
Comment Utility
Sorry, my error. Had a complete blond moment there. I blame it on fact that it's Friday afternoon and I should be down the pub by now :-)

If I paste into http://yoursite.com/?page_id=69695 browser it goes to http://yoursite.com/property/region
But rewriting http://yoursite.com/property/region/anything to http://yoursite.com/?page_id=69695 or to http://yoursite.com/property/region results in a 404
0
 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
Okay, let's not use Rewrite Rules for this and instead switch to RedirectMatch at the top of the .htaccess file (above the caching stuff)

RedirectMatch 301 /property/region/.* http://example.com/?page_id=69695
0
 
LVL 1

Author Comment

by:fionafenton
Comment Utility
Already tried that earlier today :-(
Still get a 404
0
 
LVL 15

Expert Comment

by:Ess Kay
Comment Utility
0
 
LVL 15

Expert Comment

by:Ess Kay
Comment Utility
you can also try to insert somehting like this in the 404 page



<?php
$Referrer = wp_get_referer( ) ;
if ( $Referrer = 'http://www.domain.com/' ) {
  echo '<script type="text/javascript">window.location ="http://www.domain.com/about/";</script>' ;
}
elseif ( $Referrer = 'http://www.domain.com/about/' ) {
  echo '<script type="text/javascript">window.location ="http://www.domain.com/about www.domain.com/category/";</script>' ;
}
elseif ( $Referrer = 'http://www.domain.com/about www.domain.com/category/' ) {
  echo '<script type="text/javascript">window.location ="http://www.domain.com/category www/domain.com/about/sub-about/";</script>' ;
}
else {
  echo "404 Message" ;
}
?>
0
 
LVL 1

Author Comment

by:fionafenton
Comment Utility
The custom category link is to a forum discussion where the suggestions don't work and isn't really applicable here.
I have already created a custom template. What I'm trying to achieve is to have just one page (property/region)  that delivers content for all sub pages e.g. property/region/anything, property/region/somethingelse and use a simple RewriteRule to achieve it. I could create a page for each subcategory, but why have dozens of pages when one will do.

I'm not going to even try the amending 404 page suggestion - it's not the solution, just a possible way of getting around the problem. Also, the client has already installed the Smart 404 plugin which is bound to result in some conflicts.
0
 
LVL 1

Accepted Solution

by:
fionafenton earned 0 total points
Comment Utility
Sorry I haven't closed this sooner.
I couldn't get anything in the htaccess file to work so gave up and ended up creating my own plugin containing:

<?php
/*
  Plugin Name: Fiona's URL Rewrite
  Description: Custom Rewrites - Use this to add custom RewriteRules
 */
 
add_action( 'init', 'ff_add_rewrite_rule');
function ff_add_rewrite_rule() {
  add_rewrite_rule( 'property/region/([^/]+)/([^/]+)/?', 'index.php?&page_id=69695&mls=$matches[1]&pageno=$matches[2]', 'top');
      add_rewrite_rule( 'property/region/([^/]+)/?', 'index.php?&page_id=69695&mls=$matches[1]', 'top');
//  flush_rewrite_rules();
}
add_filter( 'query_vars', 'ff_query_vars' );
function ff_query_vars( $query_vars )
{
    $query_vars[] = 'mls';
            $query_vars[] = 'pageno';
    return $query_vars;
}
?>
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

What's this? As a volunteer Page Editor for Experts-Exchange.com, I have noticed that many authors also have blogs, and pull articles from their blogs to post at Experts-Exchange. I appreciate each author sharing their blog content with our site …
WordPress is constantly evolving, and with each evolution appears to get better and better.  One of the big drawbacks prior to version 3 was that there was no way to be able to set up a custom menu from the backend. The Old Way Adding menus is…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now