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.
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).
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.
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).
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?
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.
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
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)?
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
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
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!
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?
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