Solved

Wordpress RewriteRule problem

Posted on 2013-06-14
18
373 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
ID: 39248019
simple, create a redirect page

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

Author Comment

by:fionafenton
ID: 39248045
Sorry, don't understand?
0
 
LVL 14

Expert Comment

by:Dzynit
ID: 39248205
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
ID: 39248248
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
ID: 39248263
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
ID: 39248275
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
ID: 39248301
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
ID: 39248324
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:fionafenton
ID: 39248358
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
ID: 39248368
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
ID: 39248423
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
ID: 39248444
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
ID: 39248482
Already tried that earlier today :-(
Still get a 404
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39248509
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39248514
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
ID: 39249854
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
ID: 39429704
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Who says nothing in life is free? WordPress.com is a freebie. WordPress.org's downloadable publishing platform is free. Heck, even WordPressMU is free. WordPress is an open source project, which means it can be used on any personal or commerc…
Transferring a wordpress site from a host or local dev server to another host can be a pain. So I've included my steps on how I have accomplished this task. Steps include an assumption that you have Cpanel access or Ftp access.. If you do not hav…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

932 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

11 Experts available now in Live!

Get 1:1 Help Now