.htaccess "?" Sign in file names trouble - apache

There are static html files called blahblah.html?somestring. I'm always get 404 error.
When I try blahblah.html%3fsomething - it semi-works (shows html source as text), but ` need URLs to be exactly with question sign.
How to make Apache show to this files with exact URL containing "?".
I tried to rename file to "blahblah.html-somestring" + rule in .htaccess
RewriteRule ^/(.*).html\?(.*)$ /$1.html-$2 [NE]
it doesn't help
THIS HELPED ME:
I renamed all files to blahblah.html-somestring.html + added this to .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^[^.]+\.html/?$ %{REQUEST_URI}-%{QUERY_STRING}.html? [L,NC]

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^[^.]+\.html/?$ %{REQUEST_URI}-%{QUERY_STRING}? [L,NC,R]
This will rewrite blahblah.html?somestring to blahblah.html-somestring. However how the content will be served from blahblah.html-somestring, you will have to figure out.

Actually I can see two problems here:
1) You want to use the ? character as part of file names
I cannot see a plain solution of this problem. The question mark is part of the HTTP protocol and you can rewrite URLs but the desired solution is not possible with these tools. If you can live with the %3f approach this will be the most simple way to solve this.
2) You want to set correct MIME types beside from file suffixes
Apache detects the correct MIME type (e.g. text/html) depending on a files name. If the file is ending to .htm or .html Apache delivers the content with the MIME type text/html.
As your file names do not end with the expected suffixes and regular expression do work for the MIME table the only way to make this working is some regex within the mod_rewrite module. Here you can detect the suffix in the middle of the filename and are able to set the MIME type:
RewriteEngine On
RewriteRule ^(.+\.html.*)$ $1 [T=text/html]
Overall solution:
You can solve #1 and #2 with a simple Web application, checking the origin request string and delivering the matching file (as well as setting the correct MIME type). You can do such things for example with PHP.

Related

.htaccess rewrite rule ignoring string, multiple GET variables in url

I am trying to make .htaccess rewrite rule to map 4 different get variables and exclude one string. String is unchangeable ie. always will remain same.
Current url is:
/car.php?make=bmw&model=z4&year=2006&color=black_metallic
It should be like this:
car/bmw-z4-2006-black_metallic-for-sale
This is I've done so far
RewriteRule ^car/^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ car.php?make=$1&model=$2&year=$3&color=$4
Now I need to ignore string -for-sale at the end of the pretty url.
Any help greatly appreciated!
Try this
RewriteRule ^car/([^/-]+)-([^/-]+)-([^/-]+)-([^/-]+)/?$ car.php?make=$1&model=$2&year=$3&color=$4 [QSA,NC,L]
Your delimiter is hyphen not forward slash hence your RewriteRule should also handle that accordingly:
Options -MultiViews
RewriteEngine On
RewriteRule ^car/^([^-]+)-([^-]+)-([^-]+)-([^-]+) car.php?make=$1&model=$2&year=$3&color=$4 [L,QSA,NC]
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

Apache - rewrite images to php file with .htaccess

I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
So,
example.com/folder/img/test.jpg
would be rewrited as something like
example.com/folder/some.php?img=img/test.jpg
(is this the best approach?)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
Make sure:
.htaccess is enabled
mod_rewrite is enabled
Your URL is http://example.com/folder/img/test.jpg
It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:
RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1
Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]
If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.
Hope this helps.
Bye

How can I add an additional word to a URL using mod_rewrite

I have an application with URLs such as these:
https://www.domain.com/example/public/subscription
https://www.domain.com/example/public/subscription/source
https://www.domain.com/example/public/test/subscription
I'd like to use mod_rewrite (or some other method) to create shorter "aliases" of the above URLs by removing the /public/ part so that I can provide my client with these shorter versions of the URLs:
https://www.domain.com/example/subscription
https://www.domain.com/example/subscription/source
https://www.domain.com/example/test/subscription
In other words, when browsing to https://www.domain.com/example/subscription, for example:
The server must send back the same response that one would get when opening https://www.domain.com/example/public/subscription directly
The browser must still display the shorter version of the URL (without the /public/) in the address bar
Is this even possible, and how would such a RewriteRule look like?
Thank you in advance.
put this is .htaccess file in your DocumentRoot.
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^(example)/(.*)$ $1/public/$2 [NC,L]
You just want to "remove" the public keyword?
Try this rewrite rule:
RewriteRule ^/example/public/(.*) /example/$1 [L]

mod_rewrite for friendly URL's

I'm trying to implement Permalinks into my content management system and I seem to be stuck at a problem with mod_rewrite. I have a PHP file (single.php) which will display a single post based on the Permalink name that gets passed through to it. i.e. post.php?permalink=name-of-post-here.
Here are the rules I've set up:
RewriteRule ^([0-9]{4})/([a-z]+)?$ $1/ [R]
RewriteRule ^([0-9]{4})/([a-z]+)?$ post.php?permalink=$1
Also, how do I get the date/year (i.e. 2012), and assign that as a parameter for the PHP file. (i.e. post.php?year=2012?permalink=name-of-post-here) & can all of this be done using mod_rewrite?
Many Thanks.
Want to change
http://www.website.com/post.php?year=2012&permalink=post-name-here
to
http://www.website.com/2012/post-name-here
You should use this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^([0-9]{4})/(.*)$ post.php?year=$1&permalink=$2 [L,R,NC,QSA]
If you don't want external redirect (change URL in browser) then remove R flag.

How to catch all characters with slashes after domain address with mod_rewrite as one parameter?

How to catch all characters after domain name as one parameter with mod_rewrite?
I have script which load other page content and displays that like standard page.
example script address look like that:
example.com/script.php/?www='/some/paths/etc'
I want to mask that URL to look like this:
example.com/some/paths/etc
If no paths exist it should look like
example.com/
There can be different amount of URL parts (a/b/c/.../X/)
I red about mod_proxy but I can do it with only .htaccess so it doesn't solve my problem.
I can't redirect one address to other, they have to be separated.
You can proxy via mod_rewrite (as long as mod_proxy has been compiled into the server), and this can be done via .htaccess files. Note that RewriteEngine On requires Options FollowSymLinks to be overridable in your .htaccess.
The following in your .htaccess file:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/script.php/?www=
RewriteRule ^/script.php/?www='([^']+)' http://example.com/$1
RewriteRule ^/(.*) /script.php/?www='$1' [P]
The RewriteCond matches your script path with a ?www= argument (if it fails to match no rewrite will take place). The first RewriteRule will match the www= argument and rewrite the visible path of the request. The second RewriteRule will then proxy the request (due to the [P] flag) to the script and will get the "content".
Note: this has not been tested, but hopefully will get you heading in the right direction to solve the problem. Also if script.php expects optional args &var2=whatever you will probably need to tweak the RewriteCond to match that and store it using parentheses. You can then recall those args using %1. More details can be found in the documentation.
Also to help you debug your rewriting you can use:
RewriteLog /full/path/to/your/log/file
RewriteLogLevel 3
EDIT: Added the RewriteLog stuff an clarified that the snippet at the top is to be put in the .htaccess file