Wordpress default .htaccess

Can someone explain the meaning of wordpress default .htaccess?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

Here’s my htacess

RewriteRule ^m([0-9]+)$ $1/
RewriteRule ^m([0-9]+)/$ ?page_id=45&mobile-page&mid=$1

so that mydomain.com/m123 will read as mydomain.com/page_id=45&mobile-page&mid=45

This works fine if wordpress permalink is set to be default, but if it set to different kind it won’t work, because I think wordpress has its own redirection.

How can I solve this? I need to use the non default permalink.

I also try using [L] like:

RewriteRule ^m([0-9]+)$ $1/ [L] 
RewriteRule ^m([0-9]+)/$ ?page_id=45&mobile-page&mid=$1 [L] 
&lt;/IfModule&gt;
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond &#37;{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

This will create 404 error page not found.

if I use [R=301,L], it will work but it is redirected to mydomain.com/page_id=45&mobile-page&mid=45

Hi gm!

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteCond &#37;{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

That’s WP’s default mod_rewrite (when WordPress is installed in the wordpress subdirectory). If it’s going to work (meaning mod_rewrite is enabled), you should get rid of the <IfModule> brackets (to avoid making that test for every file request).

The RewriteBase directive counters the effect of any mod_alias directive to shift the URI.

The !-f is NOT file exists; !-d is NOT directory exists.
The RewriteRule then redirects EVERYTHING ELSE to the wordpress directory’s WP handler (which reads the original request and determines which WP modules are to be called to deal with the request).

RewriteRule ^m([0-9]+)$ $1/
RewriteRule ^m([0-9]+)/$ ?page_id=45&mobile-page&mid=$1

That will redirect m{one-or-more digits} to {same}/ AND then redirect it to the DirectoryIndex with the query string fixed except for the value of the mid key (WHY do this in two steps?). BTW, it will redirect there but it will NOT display the new URI in the browser’s location box.

IF you add this to your .htaccess BEFORE WP’s mod_rewrite, then it will redirect correctly (and WP’s -f will not further redirect to wordpress/index.php because the DirectoryIndex should be found).

RewriteEngine on

RewriteRule ^m([0-9]+)/?$ index.php?page_id=45&mobile-page&mid=$1 [L] 

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . [COLOR="Red"][B]wordpress[/B][/COLOR]/index.php [L]
# END WordPress

That should work for you. Note that the first two rules are combined by the optional / and that I’ve specified the DirectoryIndex.

LEAVE WP’s mod_rewrite ALONE!

Tip: Use the R=301 flag to demonstrate to yourself that your redirection is working properly then remove for the web.

Regards,

DK

Hi David, thanks for the reply. It is a good answer and does make sense. But it still showing 404 not found page. Any ideas?

gm,

WHOOPS! It looks like I ignored my own advice about leaving WP’s code alone!

Because you don’t want ALL your redirections to be to the wordpress directory, I’d suggest leaving RewriteBase / alone (or deleting it - which is probably better) but I had removed the WP redirection to its index.php file IN THE wordpress SUBDIRECTORY IN ERROR! Please reinstate 'wordpress" in the redirection as above (in red).

THEN, please confirm that index.php is the DirectoryIndex in the site’s root directory (first RewriteRule).

Apologies for such a grievous error!

Regards,

DK

The .htaccess is fine. Here is it:

RewriteEngine on

RewriteRule ^m([0-9]+)/?$ index.php?page_id=45&mobile-page&mid=$1 [L] 

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

I think the problem relies on wordpress permalink. If it show 404 error page, that meansit does not found the files right, how can I know which file is it. Is there an apache log that I can see?

I try

RewriteRule ^m([0-9]+)/?$ http://google.com [L]

This also return 404. But when i change the wordpress permalink to default it will be redirected to google

gm,

Did you go back and change the RewriteRule to specify the wordpress subdirectory?

What EXACTLY do you mean when you refer to the wordpress permalink?

What is the test URI that you’re using (which is failing)?

To see the effect of your redirects, change the [L] to [R=301,L] (remember to remove the “R=301,” before you upload to the production server). That’s a great tip for testing!

As a last resort, you may need to reinstate the /wordpress/ in your RewriteBase statement.

Regards,

DK

I did not use any subdirectory.

If you use wordpress, there is a setting for permalink link where you have those cool url name, like mydomain.com/title-name . I think this permalink overwrite the htaccess

I’m testing mydomain.com/m100

If I use [R=301,L] it will be redirected to a new url and it is working. If I only put [L] it is not working

On the 404 error page, I have this variable

[QUERY_STRING] => page_id=45&mobile-page&mid=100 [REDIRECT_QUERY_STRING] => page_id=45&mobile-page&mid=100 [REDIRECT_STATUS] => 200 [REDIRECT_URL] => /m100 [REDIRECT_file_gzip] => /ramdisk/cpud/status [REQUEST_URI] => /m100

gm,

Okay, that clarifies a few things:

No subdirectory for WP means that you’ll NOT be able to use any of your own pages within the website, i.e., m100 will be handled by WP’s index.php where it will be parsed and the appropriate WP modules called to handle it. The only way to avoid that is to have m100 be a file (or directory) within your domain.

If you’re using the same code but only changing for the R=301 then the redirect IS working. The problem is that WP is very greedy about handling EVERYTHING that is requested (of its directory).

Worse, your attempts to add a query string will be ignored by WP. WHY are you trying to do something within the WP directory (root of the website)?

Regards,

DK

the /m100 will be dynamic. I will show product with the ID of 100. So there will be m1 , m2, and so on. I have other people code that is working for this condition. I’m looking at it now. I’ll let you know the solution

gm,

Okay, then I’d use

RewriteEngine on

RewriteRule ^m([0-9]+)$ index.php?page_id=45&mobile-page&mid=$1 [L]

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond &#37;{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
# END WordPress

That SHOULD work … provided you change the first index.php to the script which is handling your “special” redirections. THAT will change the {REQUEST_URI} to the handler’s filename and add the query string - which WP should leave alone!

Regards,

DK

Still not working DK. Thanks for trying. I think I need to overwrite wp redirection

gm,

If you “overwrite wp redirection” you will be disabling WP, i.e., NOT a good idea.

May I suggest the Skip flag on the m= RewriteRule? Instead of [L], use [S=1,L] which should cause mod_rewrite to skip the next RewriteRule. The only question in my mind is how mod_rewrite will respond on the next pass although the -f should find your handler file and NOT redirect.

What is the name of your handler script (for the m= RewriteRule) and does it exist (can you call it directly with your browser?

Regards,

DK

What I mean is, then it first execute the file. It will check weather it has mid of not. If it has mid, then I will use my own template and die(); so wordpress redirection will not run