.htaccess rewrite help needed

Hi

I need to implement SEO friendly URL. Currently my website has URL like http://domainname.com/mybignightout/activities/?title=afternoon-tea but I want them to be it like http://domainname.com/mybignightout/brighton-hen-activities/afternoon-tea to . I have been trying to get it work but no success. Thanks for your help in advance.

Following is my .htaccess file content.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mybignightout/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mybignightout/index.php [L]
RewriteRule ^brighton-hen-activities/([0-9a-zA-Z]+) activities/?title=$1 [NC, L]
</IfModule>

# END WordPress

Many Thanks for your help

dav,

That looks like it might be okay with a few changes:

  1. Delete the wrapper. That’s a sure sign of a “not ready for prime time” webmaster. You should know whether mod_rewrite is available and should not waste machine cycles repeating a test you already know the answer to - are you expecting it to be disabled any time soon?

  2. I avoid RewriteBase comments as they can change the directory level unexpectedly. I simply install my mod_rewrite code where I know it is required and specifically handle the subdirectories within the code. Simply delete this, too.

  3. The first RewriteRule is, IMHO, a stupid addition by the WP staff as it’s already covered by the first RewriteCond statement (index.php exists). Just delete it.

  4. The second RewriteRule simply redirects everything (except files and directory requests which exist) to the mybignightout/index.php script. Why? Are you expecting a request which will not match the third RewriteRule?

  5. The third RewriteRule has major problems:

  • If this code is in the DocumentRoot, you’ll need to specify mybignightout/ after the start anchor.
  • Your character range definition will NOT match the hyphens in your title string. Add - as the first character.
  • The No Case flag had NO PLACE in a RewriteRule because the {REQUEST_URI} is case sensitive! Delete the 'NC, ’ from the flags.

Regards,

DK

Hi

Thanks for your response.

Here is the current .htaccess file content.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mybignightout/index.php
RewriteRule ^brighton-hen-activities/([-0-9a-zA-Z]+) activities/?title=$1&type=hen [L]

I still have problem. When I try to access http://domain.com/mybignightout/brighton-hen-activities/DONATELLOS it’s showing 404 error.

Please note I have other URLs like http://domain.com/mybignightout/hen-activities/, http://domain.com/mybignightout/night-club-deals/ and these are working. In my wordpress settings the permalinks is set to “Post name”.

By the way the .htaccess file is under DocumentRoot/mybignightout folder.

Thanks again

Hi dav,

This time you’ve introduced capital letters and the /. You already have caps included but you must add the / to the character range definition for it to match (and then redirect). Do NOT escape the /, it doesn’t need it.

Good place for the mybignightout mod_rewrite code! Gudonya.

Regards,

DK

Hi

Thanks again, I really appreciate it.

Now the current .htaccess file content is

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mybignightout/index.php
RewriteRule ^brighton-hen-activities/([-0-9a-zA-Z/]+) activities/?title=$1&type=hen [L]

I am still having 404 error while trying to access http://domain.com/mybignightout/brighton-hen-activities/DONATELLOS

Any ideas?

dav,

Yes, two:

  1. Add R=301, to the Last flag so you can see the redirection. Obviously, this is best done on a test server but use it on your production server if you have to. Seeing the redirection working (or NOT working) is a good step in troubleshooting.

  2. I put my mod_rewrite code in the DocumentRoot because I’ve forgotten whether Apache does look for the / after the anchor IN A SUBDIRECTORY or not. Of course, you could try with ^/?brighton-hen-activities/… and see if the missing (optional in this code) / makes a difference. Sorry, my bad as I should know that one (but I’m guessing only the DocumentRoot’s leading / is to be avoided in Apache 2+).

Regards,

DK

Hi DK

I am afraid it is still not working and I really cannot work out why :frowning:

The .htaccess file now contains

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mybignightout/index.php 
RewriteRule ^/?brighton-hen-activities/([-0-9a-zA-Z/]+) activities/?title=$1&type=hen [R=301]

When I access http://domain.com/mybignightout/brighton-hen-activities/DONATELLOS I see no redirection on browser it just shows 404 error.

Can you please point out what’s going wrong here?

Thanks for all your help.

Hi dav,

Aw, @#$%! It’s the order of the RewriteRule sets! The first RewriteRule redirects EVERYTHING to mybignightout/index.php so the {REQUEST_URI} is changed and the second RewriteRule is never matched. Go figure.

Move the second RewriteRule to just after RewriteEngine on (it IS supposed to be on, not On, but it doesn’t really make any difference - just my being pedantic).

Sorry for not THINKING before this. :bulb:

Once that’s confirmed to be working, delete the flag on that RewriteRule (the one you moved) so that visitors won’t see the activities/?title={whatever}&type=hen URI.

Regards,

DK

Hi

Still cannot get it to work :frowning:

Current .htaccess file has this

RewriteEngine on
RewriteRule ^/?brighton-hen-activities/([-0-9a-zA-Z/]+) activities/?title=$1&type=hen [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mybignightout/index.php 

It’s still showing me 404 no redirection on browser :frowning:

What could it be?

dav,

Have you established the DirectoryIndex in the activities subdirectory?

Even if you have (it should have inherited from the parent directories), try putting the redirection file before the ? in the redirection (it may be that Apache is not looking up the DirectoryIndex before going on to the next RewriteRule block (I can’t imagine why not but I’m not a fan of not redirecting to a file like you’re trying to do; the redirection will be hidden so relying on the DirectoryIndex seems a silly thing to do (to me, anyway, because it’s not wrong, just not to my taste).

It’s nearly 1AM so I’m off for a night’s sleep and MAY be a better help tomorrow.

Regards,

DK

Hi

“activities” is not a directory. It’s actually a wordpress page.

Thanks

dav,

If that’s the case, it’s no wonder Apache can’t serve it! I just hate Options MultiViews and attempting to add a query string to the URI SHOULD (IMHO) confuse Apache (albeit Apache is much smarter than I am).

Again, since the redirection will not be shown, redirect to the actual script you want to use.

Regards,

DK

Hi

Thanks again. This is where I am at now,

Current .htaccess file contains

RewriteEngine on
RewriteRule ^/?brighton-hen-activities/([-0-9a-zA-Z/]+) ?page_id=139&title=$1&type=hen [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mybignightout/index.php 

When the wordpress parmelink setting is set to “Post name” and I try to access http://domain.com/mybignightout/brighton-hen-activities/DONATELLOS the url on address bar changes to http://domain.com/mybignightout/activities/. In this scenario page url http://domain.com/mybignightout/stag-activities/ is working. Please note “activities” is a page with id 139

But when the wordpress parmelink setting is set to “Default” and I try to access

http://domain.com/mybignightout/brighton-hen-activities/DONATELLOS

the url on address bar changes IS NOT changing and its showing the redirected page properly. No problem here, except as the permalink is set to default the page url like http://domain.com/mybignightout/stag-activities/ is showing the home page.

Any ideas?

Many Thanks

Hi dav,

That’s my idea. I guess I don’t really understand the difference between brighton-hen-activities/DONATELLOS and stag-activities. IMHO, you must be trying to get too fancy with your redirections to ? and expecting Apache to guess which file its supposed to serve. Okay, it’s more likely that I’m just confused. For my own peace of mind, would you please specify the redirections (requested URI to served URI) you want to make BEFORE WP simply redirects everything (not a file or directory) to its specified index.php? Thank you!

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.