htaccess mod_rewrite - rewrite folder and file to parameters - apache

I have the follow folder structure for images:
http://www.localhost/memes/01/blaa.jpg
http://www.localhost/memes/02/blaa2.pg
etc.
Now I want to move the structure but the old one must still be available for PHP file.
So it should be rewritten like:
http://www.localhost/memes/01/blaa.jpg
to:
http://www.localhost/memes/?folder=01&pic=blaa.jpg
or to:
http://www.localhost/memes/?pic=blaa.jpg (ignoring the subfolder of memes)
yes it's www.localhost for what ever reason and I don't mind it so far :D

Could you please try following, written and tested with your shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^memes/([^/]*)/(.*jpg)$ memes/?folder=$1&pic=$2 [NC,L]

Related

Apache mod_rewrite path to query string for specific path format

I've been working on this for weeks. I want to be able to use a friendly URL to pass variables to a PHP script. For example, if someone uses this URL:
https://example.com/foo/bar/who
I would like my PHP script to receive this:
https://example.com/index.php/?var1=foo&var2=bar&var3=who
The catch is that I ONLY want to do this rewrite when there are three vars in the path. If there are fewer or more than three, I do not want to rewrite the URL.
I've seen several other explanations related to this type of rewrite but nothing quite like this.
This is almost working, but not quite. I only want the rewrite done if there's something present for those first three variables. I know this is incomplete, but with this method at least the REQUEST_URI contains the values I can parse. But again, I only want to do this when there are three vars in the path.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^index\.php/([^/]*)/([^/]*)/([^/]*)$ index.php [L]
Thank you.
You were pretty close, have it this way. Please make sure you clear your browser cache before testing your URLs. You had already created back references only thing you needed to use them, which I have added those now.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ index.php?var1=$1&var2=$2&var3=$3 [L]

Redirect to full URL with htaccess RewriteRule

I want to load images from production website if they don't exist on my local environment, for that, I have this htaccess rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule uploads/(.*)$ http://www.productionserver.com/uploads/$1 [R=302,L,NC]
The problem:
Original URL:
http://localhost:8080/myproject/uploads/image.jpg
Output:
http://localhost:8080/myproject/http://www.productionserver.com/uploads/image.jpg
Desired output:
http://www.productionserver.com/uploads/image.jpg
What must I change in order to achieve that?
The code was correct.
I moved it to the top of my .htaccess file and it worked. Some other rule was interfering with it.
I believe the tool I was using to debug the "Output" was wrong. The tool is: http://htaccess.madewithlove.be
Thanks everyone who replied on the comments.

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

Forwarding all htm file extensions to a a specific file

I have been given a task to change 100's of .htm files on a site to only show the contact page. As appose to changing the files I am thinking to just use a .htaccess file and force all requests which includes a ".htm" or ".html" to the contact.htm page.
I have very limited knowledge about .ht_access files and the one I have which forces all requests through a index.php isn't working because one of the rewrite conditions is to check whether the file is real or not.
Any advice or tips will greatly be appreciated:
// My .htaccess rules at the moment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ contact.htm [NC,L]
You can try something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^\.]*\.htm$ contact.htm [NC,L]
This will replace everything that ends with .htm to contact.htm
EDIT: Made a mistake, checked it and fixed it
If you want to maintain your SEO and prevent link rot (which is bad), you'll want to let the client know that the old HTML file is no longer valid:
RedirectMatch 301 ^.*html?$ http://example.com/contact.html
Edit: as an interesting side note, returning a 410 might be a more appropriate response.

.htaccess display /base1/folder/ using the content of /base2/folder.html

When I open the url http://mysite.com/base1/folder/, I want it to display the contents of http://mysite.com/base2/folder.html. There is an index.html file in the folder folder.
To be clearer, the html file folder.html resides in the folder base2.
My .htaccess file does not work as expected
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/base1/([^/]+)/(.+)$ /base2/$1.html [L]
Could anyone please tell me is wrong with my .htaccess file?
Thanks!
Your regular expression looks wrong:
^/base1/([^/]+)/(.+)$
^^^ requires one or more characters
This should work (to accept anything following /folder/)
^/base1/([^/]+)/
Or this (to accept only /folder/)
^/base1/([^/]+)/$
Furthermore I would remove the two RewriteCond as your rule. (Though they shouldn't do any harm if /base1/ doesn't exist)