Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

Wordpress RewriteRule problem

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
fionafenton
Asked:
fionafenton
  • 8
  • 5
  • 3
  • +1
1 Solution
 
Ess KayEntrapenuerCommented:
simple, create a redirect page

look up the widgets, i seen a few free ones out there
0
 
fionafentonAuthor Commented:
Sorry, don't understand?
0
 
DzynitCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Jason C. LevineNo oneCommented:
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
 
fionafentonAuthor Commented:
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
 
Jason C. LevineNo oneCommented:
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
 
fionafentonAuthor Commented:
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
 
Jason C. LevineNo oneCommented:
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
 
fionafentonAuthor Commented:
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
 
Jason C. LevineNo oneCommented:
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
 
fionafentonAuthor Commented:
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
 
Jason C. LevineNo oneCommented:
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
 
fionafentonAuthor Commented:
Already tried that earlier today :-(
Still get a 404
0
 
Ess KayEntrapenuerCommented:
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
 
fionafentonAuthor Commented:
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
 
fionafentonAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 8
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now