.htaccess redirect a clean image url - apache

I am trying to do clean URLs for images, where the url also contains a description of the image (for seo purposes). The only important part of the file name is the start and the extension.
I am trying to get like image URLs such as:
mywebsite.com/seo-images/123-some-description.jpg
to do a clean url rewrite where the image is actually located at
mywebsite.com/uploads/123.jpg
file types can be .jpg or .png or .gif etc.
I also have thumbnails:
mywebsite.com/seo-images/t123-some-description.jpg
to do also do a clean url rewrite in the same way and place
mywebsite.com/uploads/t123.jpg
I have tried putting the following .htaccess file in the seo-images directory
RewriteEngine On
RewriteBase /seo-images/
# match all numbers upto - then get the extension after .
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([_0-9a-zA-Z-]+/)?-([_0-9a-zA-Z-]+/)?([_0-9a-zA-Z-]+/)\.$ /uploads/$1.$3 [R=301,L]
so far, no good.
I have a .htaccess file in the root that does other clean mod_rewrites.
The "some-description" can be one word or many words separated by - so basically everything after the - and before the . should be disregarded and the new filename used to rewrite using the new filename in a different directory

I think you would use rather this simpler rules set in /seo-images/.htaccess, we rewrite only if %{REQUEST_FILENAME} is neither a file nor a directory, not the inverse:
RewriteEngine On
#RewriteBase /seo-images/
# match all numbers upto - then get the extension after .
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]+)-.*\.([[:alnum:]]+)$ /uploads/$1.$2 [L]
Without redirect, we use only an internal rewrite, Google will always think about mywebsite.com/seo-images/123-some-description.jpg instead of mywebsite.com/uploads/123.jpg .
Or you prefer absolutely rewriting:
RewriteEngine On
#RewriteBase /seo-images/
# match all numbers upto - then get the extension after .
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]+)-.*\.([[:alnum:]]+)$ /uploads/$1.$2 [R=301,L]
It does this:
/seo-images/123-some-description.jpg => /uploads/123.jpg
/seo-images/t123-foo-bar.png => /uploads/t123.png

Related

How can I make my site load example.html when somebody visits example.com/example

I have a file in my public_html folder called example.html.
When somebody visits example.com/example.html, they get a 404 response.
How can I make Apache serve example.html instead?
I have a bunch of files like that, so I'd prefer to create some kind of general rule instead of redirecting them all individually.
#Turning RewriteEngine On
RewriteEngine on
#Making sure a directory or file does not exist under that name
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule {WHAT THE USER SEES} {WHAT IS CALLED}
RewriteRule ^example$ example.html
With this, loading example.com/example would show whatever example.html loads.
If you have a bunch of HTML files that you want to load in that format, use this:
RewriteRule ^(.*)$ $1.html
You can put whatever regex you want in the first argument.

Mod Rewrite & CheckSpelling/CheckCase redirect solution

I have a number of pages setup, to be accessed by clients' guests. The problem being, case sensitive URLs.
Currently I have in my htaccess file (to remove the .php)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
for example: (the target file is ClientName.php)
website.com/rsvp/ClientName <-this works, and the file is ClientName.php
website.com/rsvp/clientname <-this serves a Internal Server Error
-- edit/update --
Adding both CheckSpelling on & CheckCaseOnly on does not work, unless the .php is in the url. No combination of the two [mod_spelling & mod_rewrite] would work. I also found out, I do not have RewriteMap
based on this thread/post can I redirect to a php file rather than the 500 error page if the file does not exist? (or edit my 500 error page?)
from the post;
RewriteCond %{REQUEST_URI} !^([a-z0-9/]*)\.html
RewriteRule ^(.*)\.html$ redir.php?p=$1 [L]
Will examine the {REQUEST_URI} string and EXCLUDE (!) everything that's lowercase (or directory -- see the "/"?) .html then rewrite EVERYTHING.html to the redir script. Ahhhhh! I just added the "0-9" in there to handle your digits, too. Remember, these "excluded" strings are the ones you want to PASS through to your pages and NOT rewrite.

.htaccess excluding directory issue

I have a website consisting of a single index.html file. I have several menus, and need to go 3 levels deep, so I want a php-like structure as such: index.html?main=$1&secondary=$2&tertiary=&3. This has to be reduced to www.example.com/$1/$2/$3. Overall pretty simple I would think, using following rules for in .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2&tertiary=$3
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2
RewriteRule ^([A-Za-z0-9-]+)?$ index.html?main=$1
Now, I also have several folders in my root folder that shouldn't be affected, otherwise my include's wont work. Looking at this answer I've already tried exluding these folders using RewriteRule ^(bower_components|photos)($|/) - [L] before the other rules, but it didn't work. I've also tried this answer, making a .htaccess with contens RewriteEngine Off and putting it in my folders, also without success. So obviously I'm doing something wrong somewhere. To show my folder layout, here's a quick snapshot of it:
Here's my current .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2&tertiary=$3
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2
RewriteRule ^([A-Za-z0-9-]+)?$ index.html?main=$1
Now, if I go to http://localhost/myProject/activities, so 1 level deep as the index.html file is located in myProject, it does work and all includes are included correctly. However, when going to http://localhost/myProject/activities/test, I get to the basic index.html page, but my includes point to http://localhost/myProject/activities/bower_components/platform/platform.js, so the activities is too much.
Keep your rules like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /myProject/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.html?main=$1&secondary=$2&tertiary=$3 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.html?main=$1&secondary=$2 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.html?main=$1 [L,QSA]
Then for css/js/image inclusion just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can also try adding this in your page's HTML header: <base href="/myProject/" /> so that every relative URL is resolved from that URL and not the current URL.

mod_rewrite - Removing index.php from URL (in subfolder)

I'm trying to get an API working (locally, for now), which lives in a subfolder. I've been trying every example I can find, but all are ending up in 404 errors.
I have this URL:
http://127.0.0.1/~owner/personal/api/v1/index.php/tasks
I want to be able to use this:
http://127.0.0.1/~owner/personal/api/v1/tasks
Eventually, it will become this:
http://api.mydomain.com/tasks
I just can't seem to get my .htaccess rules setup correctly. This keeps getting suggested, but does nothing:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
What am I missing?
Update
I have setup my virtual hosts to use http://api.local/ instead of the previously used addresses.
Accessing http://api.local/index.php/tasks works.
Accessing http://api.local/tasks does not.
Maybe part of your problem is your development environment. You are using a local server to do this on with several subfolders deep and I think it's messing with your .htaccess depending on the location.
If your .htaccess is in the root of your website http://127.0.0.1/.htaccess then it's not going to work properly.
Make sure it's in the same directory as your index.php file and make sure /~owner/personal/api/v1/ is your document root for your dev environment as specified in your apache config file.
Then you can test out your htaccess rules and see how they work. Your current rule should be ok providing /tasks won't be a real directory in the root.
On another note,
I always suggest using your real domain name so you could see how it works as if it were in production. You can do that by modifying your HOST file on your PC and the site will only available to you and you can access via yoursite.com. This quick easy guide will show you.
Try specifying the rewrite base.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
You are using the parameters of RewriteRule backwards. You need this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/index.php(.+)?$ /$1$2
This means:
if it's not a file
and it's not a directory
and it looks like / (optional), possibly some more characters, then /index.php, then possibly some more characters, go to the URL without the /index.php part.
Note that I deleted the [L] for now -- it means "if the URL matches this rule, don't apply any more rules." You say you want to keep transforming it into http://api.mydomain.com/tasks, so you shouldn't use [L].

.htaccess Rewrite for PDFs from root to subdirectory

I'm trying to automatically redirect all .PDF and .pdf files from the website root to a subdirectory called docs.
Note that there aren't any actual PDFs on the website root, which is why I'm trying to use the !-f bit.
This is what I came up with so far, but it isn't working. I would appreciate any help.
# redirect PDF files requested on the root '/' to /docs/<filename>.(pdf|PDF)
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(.(pdf|PDF))$ /docs/$1
Try changing your rule to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/docs%{REQUEST_URI} -f
RewriteRule ^/?(.*\.pdf)$ /docs/$1 [L,NC]
The second condition may not be needed, it's there in case a request is made that ends with ".pdf" but the pdf isn't actually there, thus the first condition is true and there's a rewrite loop (resulting in a 500 server error).