Remove encoded question mark from URL - mod-rewrite

Hi I started a fact check wiki where each fact check page page ends in a question mark, for example:
http://wecheck.org/wiki/Did_Mitt_Romney_ever_work_as_a_garbage_collector%3F
But when I share this link on many sites including Facebook by pasting it into a comment box, it strips the %3f (thinking it's the start of a query string I guess) making the link unreachable. I have to use bit.ly to connect to the link which is inconvenient and a problem for novice users.
I think I may be able to use mod-rewrite to take the %3F off. My current rewrite rules are:
RewriteEngine On
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]
RewriteRule ^/?$ %{DOCUMENT_ROOT}/w/index.php [L]
How would I modify them to strip out the %3F ?

It doesn't look like you want to strip out the %3F. Mediawiki has its own routing so if you mess with the title names, you're more likely break something than fix anything. You need to modify your media-wiki to either disallow pages with a ? at the end, or add a module or wiki bot to go through all the pages, and if there's a page that ends with ?, create a #REDIRECT [[]] page without the ? and point it to the page with the ?.

The answer is to create pages that do not have question marks at the end and then set
$wgRestrictDisplayTitle = false; in LocalSettings.php
and use the following magicwords in the page markup:
{{DISPLAYTITLE:{{PAGENAME}}?}}
You can see an example here: http://wecheck.org/wiki/Question_Mark_Problem

Related

Htaccess direct linking image to page

I'd like to prevent direct image viewing without the entire page.
If someone goes to
mysite.com/images/image1.jpg forward to mysite.com/image1.htm
mysite.com/images/image2.jpg forward to mysite.com/image1.htm
mysite.com/images/image3.jpg forward to mysite.com/image2.htm
mysite.com/images/image4.jpg forward to mysite.com/image2.htm
Is Htaccess the best way to do this and how would I set it up?
You can check for the "Referer" request header. Browsers sometimes use it to tell the server what URL told the browser to load the file. However, not all browsers will use the referer header and it can easily be forged, so this is not a sure way to prevent direct linking to your images.
You can add this at the top of your htaccesss file:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://mysite\.com/
RewriteRule ^images/(.+)\.jpg$ /$1.html [L,R]
Reading the comments, I'd recommend to avoid maintaining N RewriteRules for N images. Such a setup will turn into a maintenance nightmare for the site admin who will need to copy and paste a new rule for every new image on the site. And if you ever need to change the rules, well... good luck with that.
If you have the flexibility to choose a better directory structure, it can simplify your .htaccess file down to a single rewrite rule. In that case, you can choose a directory structure where given a path to an image you can easily determine the path to the page it belongs to. For example:
root/
.htaccess
page1/
index.html
image/
image1.jpg
image2.jpg
page2/
index.html
image/
img.jpg
Here the rewrite definition within root/.htaccess is simple (adapted from #jon-lin's answer):
# Rewrite any image URL `/<page>/images/*` to `/<page>/`.
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://mysite\.com/
RewriteRule ^([^/]+)/image/ /$1/ [L,R]
Pros
One RewriteRule to rule them all.
Your images do not have to be .jpg, as long as they reside in /<page>/image/.
Cons
Need to reorganize existing files.
Directory structure very tailored to the rewriting problem. It would be awkward for a page to access an image which "belongs" to another page.

redirect image to different image htaccess

I have tried cpanel redirect but it is not specific. I made a change to my site where anyone using a now old image needs to show a new image. I would like to redirect the link to the old image to the url to the new image.
I have an image
http://mysite.com/images/image.png
When the image is called I want to show
http://mysite.com/images/image.gif
What do I add to htaccess to create this specific rule?
Note that image1 is a png and I am replacing it with a gif so I cant just overwrite with the new image. This is why I am looking for a redirect solution.
EDIT: I can give the image the same name, it will just have different extension.
EDIT 2: change to show image would have the same name, different extension.
A simple URL rewrite should do the trick!
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^images/([a-zA-Z0-9]+).png$ images/$1.gif
Whatever is entered here '([a-zA-Z]+)' will be entered here '$1'. Hope that makes sence.
Let me know how you get on!
(Note: there is no rewrite condition to check if the file exists because you want the url rewritten whether there is a file there or not)
Edit:
Probably best to put a .htaccess in the images directory, in this case you can miss out the file paths as you are already in the directory.
If you're looking to replace this one specific image1.png to image2.gif, then this rule should work:
RewriteRule ^images/image1.png$ images/image2.gif [NC,L]
If you're looking for something more general-purpose, then it depends on your image naming.

htaccess rewrite AJAX url to pretty link

I am working with a Wordpress theme that is driven on AJAX. A nice way to load content, but not so much for SEO purposes.
The URL's all and width the 'same' string like for example: #menu-item-44 (the only difference will be the number at the end).
For it is AJAX driven, I can not make use of Wordpress' permalink structure so my question is really, can I fix this with a rewrite in my htaccess file?
For example: www.somesite.com/#menu-item-44 becomes www.somesite.com/contact
Your help will be much appriciated!
Thanks
You can't rewrite the # part of a url as it does not get sent to the server.
Look into the javascript pushstate() and googles ajax solution using hash bangs (#!)

Fairly basic mod Rewrite issue

My site filters everything through the index.php script, and in reading the docs for mod_rewrite, it seems pretty darn easy, but I'm a bit stuck. I figured I would begin a page at a time to see if i could get it to work, and got stuck pretty quickly.
I have user profiles, the longform of which is basically:
http://www.mysite.com/index.php?content=profile&id=2172
So i added one Rewrite rule to my .htaccess file that sits in the root folder:
RewriteRule ^profile/([0-9]+)$ index.php?content=profile&id=$1
The idea is now to be able to enter mysite.com/profile/2172
The redirect does bring up the proper page, but what is happening is that every CSS file, image, etc is getting /profile/ added in the middle, which is of course not where the image and CSS files are located. I use relative pathnames in the code so an example of an image in the code might be: images/userimage.jpg
What is happening is that the relative link shown above gets turned into:
mysite.com/profile/images/username.jpg
To me that makes no sense as the image path does not match the rewrite pattern (/profile/*), so why does the bogus path add /profile/ to all of my internal links?
I tried adding RewriteCond %{SCRIPT_FILENAME} !-f to the htaccess just before the rewriterule just to see what happened, no change.
Sorry if this is a simple and basic question, but can anyone with real mod rewrite expertise give me some pointers so that I can make this simple case work and bring up the page with the proper references in my code to my included css and image files? I didn't use any flags to the Rewriterule since I only have that one line after the engine is turned on (and the followsymlink line is there as well).
To me that makes no sense as the image path does not match the rewrite pattern (/profile/*), so why does the bogus path add /profile/ to all of my internal links?
Your images are relative to the page being served, which does have /profile/ e.g. mysite.com/profile/2172. You may want to consider changing your image/css links to be absolute from root i.e. /images/imag.jpg.
If that is not possible you could use another rewrite to correct the issue (just for this case, not really recommended in general) as below.
RewriteEngine on
RewriteBase /
#rewrite to remove profile for jpg, css etc, must go above existing rule below
RewriteRule ^profile/(.+\.(jpg|css|js|png))$ $1 [L,NC]
#existing rule
RewriteRule ^profile/([0-9]+)$ index.php?content=profile&id=$1 [L,NC]

Image .htaccess styling [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to take a link of a screenshot image from my domain (example: http://shot.byarr.ws/PSH.png) and dress it up a little by wrapping it up with a styled landing page, much like cloudapp (example: http://cl.ly/CP1i) for mac does when linking to a file/image.
I know this can be done with .htaccess but I have very little knowledge of such mystical practices so I'm in the need of a little guidance.
Thanks in advanced.
When you load an image like http://shot.byarr.ws/PSH.png, it is just the image. No html, scripts, code, css, or anything else gets loaded, so there's no inherent way to stylize the image that is being view by the browser. It's just an image and it's completely up to the browser how to render it, you have no control.
On the other hand, you could redirect or rewrite requests for images, e.g. all requests ending with png, jpg, gif, jpeg, etc. to a script that loads the image and renders HTML so that when the browser views it, it looks stylized. I don't know of anything offhand that will allow you to do this, you may have to write your own script or just do a google search on a simple image gallery/viewer php script.
For example, say you've found a script called view_image.php, as a parameter, it takes img and the value is the path to that image. So if you want to view the http://shot.byarr.ws/PSH.png image using the view_image.php script, you go to http://shot.byarr.ws/view_image.php?img=PSH.png (this is just an example, that link won't work). In order to make all images behave this way, you can use .htaccess and mod_rewrite to rewrite requests for image and send them to the view_image.php script:
RewriteEngine On
# Make sure it is an image
RewriteCond %{REQUEST_URI} \.(png|gif|jpe?g)$ [NC]
# Make sure image exists
RewriteCond %{REQUEST_FILENAME} -f
# rewrite the URI so that it goes to the script
RewriteRule ^(.+)$ /view_image.php?img=$1 [L]
Note that if you do this, and try to link to http://shot.byarr.ws/PSH.png in a page, it won't load the image, it will load the script instead and your page will appear to have a broken image.

Resources