Mobile app version of vmapp.org
Login or Join
Eichhorn148

: .htaccess rule fails when including $ at end The Basic Situation We have a server. It has some static files, some simple, "flat" .php and .html pages, but it also has a WordPress install

@Eichhorn148

Posted in: #Htaccess #ModRewrite #RegularExpression #Wordpress

The Basic Situation

We have a server. It has some static files, some simple, "flat" .php and .html pages, but it also has a WordPress install at the document root, meaning that visiting / takes the user to the WordPress site.

The static files are at /lp/; however, I was recently tasked with rewriting the index of this folder as a WordPress page, such that its URL is preserved when accessed through WordPress. WordPress auto-adds the following lines to .htaccess so that it handles any URLs that don't match existing files or folders:

# 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


The Problem at Hand

The WordPress page at /lp/ is written and published and set up with the proper link, but now it needs to be accessed through WordPress instead of as a flat file. It would be fine and dandy for me to simply delete the content at /lp/, but some of it needs to be preserved, ideally with the existing links intact save for the very specific /lp/ URL. Thusly, I created my own section above the WordPress section:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^lp/?$ /index.php [L]
</IfModule>


The problem is with this expression right here:

^lp/?$


This doesn't seem to work. It gives me the old page. This, however does work:

^lp/?


The issue with this fix is that using it redirects any URL beginning with lp/, including those that might otherwise return flat or static content.

My Question

I figure there's something about the way WordPress or mod_rewrite works that I'm missing... but what is it, exactly?

Feel free to let me know if there's additional information I should include.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Eichhorn148

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ann8826881

RewriteRule ^lp/?$ /index.php [L]


This fails to match because your actual URL is probably /lp/index.php (or whatever your DirectoryIndex is), not simply /lp/. (That is after Apache has internally mapped the URL to a filesystem path). So, try changing your RewriteRule pattern:

RewriteRule ^lp/index.php$ /index.php [L]


Specifically, there is a conflict with mod_dir. It is common to run into these sorts of problems when trying to rewrite URLs that also exist as a physical directory on the filesystem.

When the request maps to a physical directory, mod_dir does a couple of things before mod_rewrite gets a look in.


If the request is missing a trailing slash then mod_dir appends the slash and externally redirects. (This depends on DirectorySlash being On - the default.)
It will then internally rewrite the request to include the DirectoryIndex (if present).




Aside...


There's no need to specify RewriteEngine more than once in the file. Ideally, it should only occur once at the top. But it can occur anywhere.
It's strictly invalid to include RewriteBase more than once. The last rule wins and controls the whole file. (Although in your current .htaccess file it's not required anyway. RewriteBase only affects relative path substitutions.)
You probably don't need the <IfModule> wrappers either. These are only required if the directives they contain are optional. Should your site function normally if mod_rewrite is not available? If this was installed on a server where mod_rewrite was not available then it will simply fail silently and try to continue (and maybe break later). Without the <IfModule> container it will fail immediately with an error and you'll know quickly what the problem is. WordPress is designed to work on systems without mod_rewrite, but you don't get the same pretty URLs.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme