.htaccess RewriteRule seems not to work - .htaccess

I got a problem with my .htaccess here and altough I searched the web and tried many things, I could not find any solution...
I have a wordpress-installation with enabled permalinks. Additional to that, I need to rewrite another URL on this wordpress-installation, which does not belong to WP.
If a user browses to http://www.URL.com/?page_id=30&tag=all&filterCategory=6\%23Jackets, I'd like to show http://www.URL.com/jackets.html as URL. There should also be the possibility to directly browse to http://www.URL.com/jackets.html.
My .htaccess looks like this
# 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]
RewriteRule ^jacket\.html$ http://www.URL.com/?page_id=30&tag=all&filterCategory=6\%23Jackets [L]
</IfModule>
# END WordPress
Does anyone have any idea why this does not work?

The WordPress rewrite rules are redirecting any requests to non-existant files through WordPress. The rewrite rule responsible for this, RewriteRule . /index.php [L], is marked with [L], indicating that no more rules are to be processed after it. In order for your rule to work, it must be placed before the WordPress rules, but after the line RewriteBase /.

Related

Yii2 app in a subdirectory of a Wordpress website: cannot enable pretty urls

I’m trying to deploy a basic webapp on a shared environment where Wordpress is on the root. The Yii2 app is in /subfolder.
I’m following this guide. In root’s .htaccess I added:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subfolder
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_URI} !^/subfolder/web
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ /web/css/$1 [L]
RewriteRule ^js/(.*)$ /web/js/$1 [L]
RewriteRule ^images/(.*)$ /web/images/$1 [L]
RewriteRule (.*) /web/$1 [L]
RewriteBase /subfolder
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php
</IfModule>
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<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
But with these rules added all Wordpress’ pages are handled (or attempted) through Yii, so this breaks the blog installation. It’s the first block of rules capturing all the pages, but I don’t understand why as the two RewriteCond should intercept only the Yii app URIs. I checked mod_rewrite docs but couldn’t understand what’s wrong. Any help is appreciated. Thanks
RewriteBase /subfolder
You cannot set multiple RewriteBase directives in the same .htaccess file. The last instance "wins" and controls the entire file. So, in the .htaccess file you posted, RewriteBase / set in the WordPress code block, is what is actually set for the file.
However, none of the directives actually make use of the RewriteBase directive anyway - so none of the RewriteBase directives are actually doing anything. The RewriteBase directive only applies where you have set a relative path (not starting with a slash) in the RewriteRule substitution string.
but I don’t understand why as the two RewriteCond should intercept only the Yii app URIs.
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_URI} !^/subfolder/web
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
Presumably it's these two RewriteCond directives you are referring to... in which case these two conditions aren't really doing anything. RewriteCond directives only apply to the first RewriteRule directive that follows, so it only applies to the directive that rewrites your assets.
However, this RewriteRule is matching /assets in the document root, not /subfolder/assets, which is presumably the requirement - so these rules will fail to match.
But with these rules added all Wordpress’ pages are handled (or attempted) through Yii, so this breaks the blog installation.
The rules will certainly "break the blog installation", however, they don't appear to get as far as handling the request "through Yii". There's nothing that actually rewrites the request to /subfolder. However, the following directive unconditionally rewrites everything to the /web directory in the document root (which presumably does not exist) - so this will certainly "break" all the WordPress URLs.
RewriteRule (.*) /web/$1 [L]
In fact, I would have expected this to have created a rewrite-loop (500 Internal Server Error response)?! Unless you have a subdirectory /web off the document root which also contains an .htaccess file containing mod_rewrite directives? But that seems unlikely, since the /web directory should be inside the /subfolder directory?
Try the following instead:
RewriteEngine On
RewriteRule ^(subfolder)/(assets|css|js|images)/(.*) $1/web/$2/$3 [L]
RewriteRule ^(subfolder)/((?!web).*) $1/web/$2 [L]
RewriteRule ^subfolder/web/index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(subfolder)/. $1/web/index.php [L]
# BEGIN WordPress
:
No need for the <IfModule> wrapper. Or the RewriteBase directive.
Alternatively
However, it would be preferable to move these directives into their own .htaccess file in the root of the project, ie. /subfolder/.htaccess - which I believe is what the linked "guide" is suggesting. This keeps the two projects entirely separate. And avoids having to explicitly include the /subfolder in the directives.
In addition, creating a another .htaccess file in the web subdirectory, ie. /subfolder/web/.htaccess. This is again, suggested in the linked "guide". However, this also negates the need for the additional directives to route the request in the parent .htaccess file.
For example, putting these changes together, the /.htaccess file in the document root should only have the WordPress directives. And then...
/subfolder/.htaccess
RewriteEngine On
RewriteRule ^((?:assets|css|js|images)/.*) web/$1 [L]
RewriteRule ^((?!web).*) web/$1 [L]
/subfolder/web/.htaccess
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Again, no need for the RewriteBase directive here - in fact, using RewriteBase here arguably complicates things. When in the /subfolder/web/.htaccess file, all relative URL-paths are relative to that directory.
So, requesting /subfolder/foo gets internally rewritten by the /subfolder/.htaccess file to /subfolder/web/foo. Which is then caught by the /subfolder/web/.htaccess file (preventing a rewrite loop) and internally rewritten to /subfolder/web/index.php (providing foo does not exist as a physical file).

.htaccess rewrite page with alias

I need to rewrite an url like /services/rental/faq-2/ so that the user (and robots) see /services/rental/faq/
This is my current .htaccess (it's Wordpress) with no successful rewrite:
# 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]
RewriteRule ^/services/rental/faq/?$ /services/rental/faq-2/ [NC,L]
</IfModule>
# END WordPress
Any of the hints I searched actually worked, maybe rule order is important or (most probable) I have no idea on how to do it
Your external URL is /services/rental/faq[/]. This is the one produced by links in your pages, and is the one robots and users see. It is NOT produced by .htaccess -- .htaccess only sees incoming URIs.
Try moving your new RewriteRule up before all the other WordPress RewriteRules (after the RewriteBase).

How to use rewriterules in .htaccess to change url?

I'm using this rewriterule:
RewriteRule ^page/([^/]*)$ http://example.com/page?q=$1 [QSA,L]
If I go to example.com/page/somePage I get redirected to example.com/page?q=somePage
But I don't want a redirection, what I want is the URL to always be example.com/page/somePage
How to do this?
Thank you
I removed http://example.com but it doesn't work, I get Page not Found.
I am using Wordpress for my site, this is my complete .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^page/([^/]+)/?$ page?q=$1 [L]
# 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
Whenever you specify the http:// at the beginning of the path to be rewritten to, Apache will always force a 301 redirect to the new URL, whether the URL is on the same website or not. Simply removing the http://example.com part should fix your problem.
As for the page not found, is there another RewriteRule somewhere that tells just 'page' to be processed as 'page.php' or something of the sort? Do you have your PHP files saved without extensions?
Well then your problem is you definitely need to remove the [L] flag because you're telling Apache not to process any more RewriteRules for that request, so it never looks at the WordPress rewrites because that rule was already executed and Apache was told that should be the final rule. I would recommend leaving the [QSA] in the line though, that would not affect the overall outcome of your script.

htaccess rewritecond for url with asterisks

Some... fine person... has set up an incoming link to our site that looks like:
http://www.site.com/*our*-*services*/
I'd like to redirect it so it points it to:
http://www.site.com/our-services/
Ours is a Wordpress site so there's some rewrite stuff in our root htaccess file already. A rule that simply removes asterisks from the URL would do, but I can't figure out how to do that, so I tried the following - loosely based on copying the existing Wordpress rules - which isn't working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (/\*our\*-\*services\*/)
RewriteRule . /our-services/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
It looks like this rule is being ignored - the errant URL currently redirects you to our default Wordpress 404 page. Clearly, I am fail: what should I have put as the rewrite condition?
I'd remove your first RewriteCond and then just use this rule:
RewriteRule \*our\*-\*services\*\/ /our-services/ [L]
Well get them to change there incoming link.
Asterisks are reserved in urls, so in this case it shouldn't be used:
W3 Url Recommendation
Allowing asterisk in URL

How to redirect requests to example.com to www.example.com with mod_rewrite?

This is my .htaccess generated by wordpress that sits in my root directory.
I need to modify/add to it, to direct all incoming traffic to www.example.com, instead of example.com.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
If someone wants to explain what all the above does also, that would be appreciated as well.
To do what you requested, you want to add:
RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
You can place that inbetween the <IfModule mod_rewrite.c> brackets after RewriteBase
As for what Wordpress' htaccess code does (didn't test it): It seems to test for any direct links to files and directories and not pass them through the RewriteRule which will normally take your link and send it to index.php
So if your link is for www.mysite.com/some/page, it makes sure /some/page isn't a direct file link or an actual web directory and if it isn't, then it passes the request to index.php which parses it to display the correct Wordpress page.

Resources