Htaccess friendly SEO rewrite made images stop loading? - .htaccess

I used a generator to make my SEO friendly htaccess rewrite, here it is.
RewriteRule ^([^/]*)/([^/]*) /detail.php?type=$1&?id=$2 [L]
The output should be www.site.com/type/id
Now, the rewrite works and pages redirect fine, BUT the images on the site no longer show up, they're all broken... :( The URL for the images is right but seems it just doesn't want to load anymore, any help? Should there be another Rewrite rule to cancel out this one from doing other stuff? If so, what?

Your existing rule could affect images. To prevent that, use a RewriteCond directive that will exlcude extensions for images etc. being affected by the rule You can add other extensions as necessary.
#if its not an image, css, js etc
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png|css|js|etc)$ [NC]
RewriteRule ^([^/]*)/([^/]*) /detail.php?type=$1&?id=$2 [L]

I'm not sure how your .htaccess looks like, but here is handy rules that I'm using for my projects, it's pretty simple and covers most of the problems:
<IfModule mod_rewrite.c>
############################################
# Enable mod_rewrite
RewriteEngine On
RewriteBase /
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|assets)/
############################################
# never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .*$ index.php [L]
</IfModule>

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

redirecting with htaccess causes too many redirects

The website I'm working on is using some cms. I need to add a static website to this. When I put mypage.html in the main directory and go to www.website.com/mypage.html it works. I would like the page to be accessible without '.html' ending. I experimented with editing htaccess files but always end up with error of too many redirections.
What I entered were various combinations, for example
Redirect 301 http://website.com/mypage http://website.com/mypage.html
The htaccess file I'm using looks like this:
:Location /*.php
Use php54
:Location
RewriteEngine On
DirectoryIndex index_prod.php
Options -Indexes
RewriteRule ^.*\.(css|png|swf|js|gif|jpeg|jpg|flv|pdf|doc)$ - [L]
RewriteRule ^net2ftp - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/?$ plug.html [L]
#RewriteCond %{REQUEST_URI} !=/
RewriteRule ^/?.* index_prod.php
I'm looking for tips or to be explicitly told what and where to put in htaccess file to make it work (if it's possible)
Could you please try following, considering that you want without extension file URLs to be served by html extension files. Also since you didn't mention any specific condition before RewriteRule hence that redirection errors are coming to it, because its keep on redirecting in lack of any condition/check's presence(till its maximum redirection limit is crossed).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [NC,L]

redirect .html to non html pages wordpress

I had old html website which had .html pages and i want to redirect to non .html pages which wordpress has.
for eg.
Old site
http://myweb212.com/About-Us/jony-test.html
wordpress site.
http://myweb212.com/About-Us/jony-test
There are many pages in website so is this possible do this with one rule?
I tried using a rule but it breaks some of the images of upload folder and shows 404.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule . /index.php [L]
</IfModule>
Inserting the new rule between another rule (RewriteRule) and its associated conditions (RewriteCond) breaks this other rule.
So when you move the rule to the beginning or to the end, it should work as is.
One minor nitpick, never test with R=301! When everything works as it should, you may replace R with R=301 (permanent redirect).

.htaccess RewriteRule seems not to work

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 /.

htaccess and URL rewriting

I have a list of URLs such as,
http://www.mywebsite.com/page.php?genus=A_GENUS&species=A_SPECIES&id=12345.
I would like to write a .htaccess which permanently redirects visits to this form of URL to the following URL,
http://www.mywebsite.com/species/A_GENUS/A_SPECIES.
Is it possible to do this without having to manually list each species in the database?
I've tried to look it up but my head is in WordPress-Custom-Post-Type land and as such my brain isn't functioning properly. Any help would be greatly appreciated.
EDIT: Clarification
Currently my .htaccess is completely empty. I am re-writing my website to use an entirely new CMS and this new URL format. The old URL format will cease to exist but all of the information will still be used.
We are quite highly ranked for a lot of species on Google and I would like visitors from there to be able to view the information they require despite the URL format changing.
These changes haven't occurred yet (still using a Sandbox environment for the new version of the site) and I'd like to make the URL changes just before I "go live" with the new version.
EDIT 2: New site .htaccess
The contents of the new site's .htaccess looks like this in its entirety:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sandboxfolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sandboxfolder/index.php [L]
</IfModule>
# END WordPress
EDIT for Garmen's answer
RewriteEngine On
RewriteBase /sandboxfolder/
RewriteCond %{QUERY_STRING} genus=([a-zA-Z0-9-]+)&species=([a-zA-Z0-9-]+)
RewriteRule ^profile.php$ /species/%1/%2 [R=302]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sandboxfolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sandboxfolder/index.php [L]
</IfModule>
# END WordPress
Not sure if I've used RewriteBase correctly there, but it doesn't appear to function with or without it.
Regards,
RewriteEngine On
RewriteCond %{QUERY_STRING} genus=([a-zA-Z0-9-]+)&species=([a-zA-Z0-9-]+)
RewriteRule ^page.php$ /species/%1/%2? [L,R=302]
This one assumes a very specific order of query string parameters. So be warned. Also it assumes the names only contains plain letters, numbers, or dashes.
Change 302 to 301 when you are done testing. I used 302 because 301's are aggressively cached by browsers, making debugging very difficult.
EDIT: You should add this above the other rewrite rules you have, or it will not work.
EDIT 2: added a ? at the end to remove the querystring. And L flag to prevent further execution.

Resources