Mod_rewrite: if / else type RewriteRule

The following rewrites the line starting at number 4 as the process.php variable:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(4[^/]*)$ /process.php?variable=$1 [L]

So this is

http://www.domain.com/4shopping

displayed on

http://www.domain.com/process.php?variable=4shopping

But I want to extend this last rewrite rule to basically indicate:

if word begins with 4, map to /process.php?variable=$1
else map to /index.php

The second (yet) part of this statement is the basic rule of rewriting WordPress. For example:

http://www.domain.com/shopping

which does not have 4, will be directed to

http://www.domain.com/index.php?shopping (I believe this is how WordPress permalinks work!)
+3
source share
3 answers

Here's the solution:

RewriteRule ^(4[^/]*)$ /feedback.php?sms_code=$1 [L]
#
# BEGIN wordpress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END wordpress

I left the default Wordpress rules and added my own conditional rule above, making sure to finish processing [L] if the condition was met

+3
source

Try the following:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^(4[^/]*)$ process.php?variable=$1 [L]
RewriteRule ^([^4/][^/]*)$ index.php?$1 [L]

, . - ( RewriteCond). , URL- 4.

+3

I would add two lines to the end of what you already have. Additional rules convert to index.php everything that has not yet been converted to process.php.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(4[^/]*)$ /process.php?variable=$1 [L]

RewriteCond %{SCRIPT_FILENAME} !process\.php
RewriteRule ^([^/]*)$ index.php?$1
0
source

Source: https://habr.com/ru/post/1718570/


All Articles