This Is the .Htaccess Code in Wordpress. Can Someone Explain How It Works

This is the .htaccess code in WordPress. Can someone explain how it works?

^index\.php$ - [L] prevents requests for index.php from being rewritten, to avoid an unnecessary file system check. If the request is for index.php the directive does nothing - and stops processing rules [L].

This block is all one rule, and it says that if it is not a real file and not a real directory, reroute the request to index.php.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

index.php itself interprets the URL that was requested by the client (PHP can see the requested URL using $_SERVER['REQUEST_URI']) and it calls the correct code for rendering the page the user requested.

Redirect url of wordpress site on the basis of string using htaccess

Immediately after the RewriteEngine On and RewriteBase directives in the "existing" WordPress code try this:

RewriteRule xyz/ http://www.domain.com/intermediate.php [L,R=301]

This will search for "xyz/" anywhere in the requested URL and redirect when found. Note that in .htaccess files, the directory prefix (/ in this case) is removed from the URL path before pattern matching. So, a pattern that starts / will never match at the start of the URL.

External redirects should generally come before internal rewrites, which is what the default WordPress directives do.

The alternative method you mention is less efficient:

RewriteCond %{REQUEST_URI} /xyz/
RewriteRule .* http://www.domain.com/intermediate.php [R=301,L]

This will effectively do the same thing but will result in every request being processed because of the generic .* pattern. Note that the REQUEST_URI does start with a / (directory prefix).

.htaccess load different web app depending on route under HTTPS

Given the following requirements:

  • /app subdirectory contains the "handmade code"
  • /wp subdirectory contains the WordPress site.
  • Neither /app or /wp should appear in the visible URL.

Should I have a single .htaccess file in the webserver root deleting the Wordpress one?

You could, but I wouldn't. Keep the WordPress .htaccess file in the /wp subdirectory. Everything WordPress is in the /wp subdirectory.

I would use 3 .htaccess files:

  • One in the document root. This manages the routing to either the "handmade code" in /app or /wp (WordPress). This should also manage the canonical redirects (ie. HTTP to HTTPS and www vs non-www)

  • One in the /app subdirectory that manages the routing within your "handmade code".

  • One in the /wp subdirectory that manages the routing within WordPress.

This allows you to keep the "handmade code" and WordPress entirely separate (in terms of development).

Your 3 .htaccess files would then look like this:

/.htaccess

# /.htaccess

RewriteEngine On

# HTTP to HTTPS redirect
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Rewrite specific URLs to "/app" (handmade code)
RewriteRule ^app-route-1$ app/$0 [L]
RewriteRule ^app-route-2$ app/$0 [L]
etc.

# Rewrite everything else to WordPress
RewriteRule (.*) wp/$1 [L]

The "specific rewrites to /app" can be combined if there is a pattern. See below regarding static assets.

/app/.htaccess

# /app/.htaccess

RewriteEngine On

# Redirect any direct requests to "/app" back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]

# Front-controller
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

/wp/.htaccess

# /wp/.htaccess

# Redirect any direct requests to "/wp" back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress

Note the RewriteBase directives and slash prefix on the substitution strings are specifically excluded, to avoid having to specify /app or /wp in the .htaccess file itself. (Although this might mess with WordPress, that likes to (unnecessarily) use RewriteBase and will try to overwrite the WP code block.)

You do not need to repeat the RewriteEngine directive, that already occurs later in the WP code block.

I don't know how you want to handle your static assets/resources (CSS, JS, images, etc.)? Currently, the above assumes that you will link directly to the assets within /app, ie. By including the /app path segment in the asset link. eg. <image src="/app/assets/images/myimage.png">. With WordPress you could link directly (ie. include /wp prefix) or omit /wp, since everything else is rewritten to /wp anyway.

Ideally, it would probably be preferable to omit both /app and /wp from your asset links, since you don't want to unnecessarily expose these to your users and it would otherwise make the sites dependent on these parent directories.

If your "handmade code" uses /assets for all the assets then you can rewrite these in the parent .htaccess file in the root, before your custom route rewrites:

# Rewrite "/app" assets
RewriteRule ^(assets)(?:/(.*)|$) app/$1/$2 [L]

This allows your "handmade code" to refer to assets using root-relative URLs, as if the app was installed in the document root.

wordpress htaccess http www redirect to https non-www

according to this post you can change your .httaccess file to redirect:

http://example.com
http://www.example.com
https://example.com

to

https://example.com

using

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

simply add this code after #end wordpress in your .htaccess file.

after saving the .htaccess file remember to save your permalink structure in settings->permalink.



Related Topics



Leave a reply



Submit