For several days i'm trying to figure out how symbolic links work with htaccess rewriterule
and for that I've created a new project to help me to apply these rules in one website
for which I work so the context is:
In my xampp folder on local-host I have a main page index.php that I want to call "home"
and in this page I have some url links for two pages called objectives.php and news.php where I wish to load dynamic content in future website
here is my code for index.php
<p><a href="objectives.php?menu=corporate-objectives">
Corporate objectives - load page content for this item from objectives table</a></p>
<p>Functional objectives ...</p>
<p>Unit objectives ...<p>
<p><a href="news.php?article=meeting-news">
Meeting news - load content from news table for meeting</a></p>
<p>Press release news ...</p>
<p>Other news ...<p>
and here is the code for the other two pages:
objectives.php
$pages = $_REQUEST['menu'];
echo "This is Corporate objectives -objectives.php here I load content
according to this menu item value"."<br>";
echo "Menu item value: ".$pages;
news.php
$pages = $_REQUEST['article'];
echo "This is Meeting news - news.php here I load news content
for this article value from news table "."<br>";
echo "Article value: ".$pages;
What I 've read before is that with RewriteRule in htaccess I can change the url to become symbolic or friendly for example:
/MyTest_proj/objectives.php?menu=corporate-objectives
I want to transform in
/MyTest_proj/corporate-objectives
and
/MyTest_proj/news.php?article=meeting-news
in
/MyTest_proj/meeting-news
so for that I have completed in my .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([-a-zA-Z0-9]+)?$ objectives.php?menu=$1
RewriteRule ^([-a-zA-Z0-9]+)?$ news.php?article=$1
The problem is... nothing happens so I've tried to change something for testing
url links from main page:
...
...
and it work BUT only for objectives.php for wich I have rewrite rule, the second page
it's load in the same page instead of news.php
I have tried in many ways to change the rules but nothig work..for several times,
the most common error was like error 401
I apologize for the long content and please, tell me that something can be done after all
thanks !
Related
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.
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
We have several different size images for products. We want to return the correct size default image whenever the product image is missing.
The path to the images are different for each product.
path /www/docroot/images/brand/product/product1_l.jpg
path /www/docroot/images/differentbrand/differentproduct/someotherproduct1_l.jpg
The size of the images look something like the items on the left. We would want to chose the corresponding default image on the right.
product1_l.jpg large noimage_l.jpg
product1_m.jpg medium noimage_m.jpg
product1_s.jpg small noimage_s.jpg
I have seen some examples of people achieving something similar using mod_rewrite. Can anyone give any guidance on how I can use that to fit my scenario?
You need an intelligent 404 handler. If Apache throws a 404 (File not found) error the intelligent handler should try to salvage the event by looking at the pattern of the URL, and locating a known resource to serve instead. You should also handle the possibility that the fall back image might also not be there.
This page can show you how to check if a file is present, but the logic will need to be in the last line.
http://www.phpriot.com/articles/search-engine-urls/5
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
Or you can go the .htaccess route (I think that's cleaner)
http://httpd.apache.org/docs/2.2/mod/core.html#errordocument
Assuming you could use .htaccess and PHP here is one solution:
Add this line to your .htaccess:
ErrorDocument 404 /error.php
Then add this code to you error.php like this
<?php
if(preg_match("/^/images/thumbnail/", $_SERVER['REQUEST_URI'])) {
//do a 302 redirect
header( 'Location: /images/defualtImage.jpg' ) ;
} else {
//You need to go somewhere to handle actual 404 errors
header( 'Location: /static/404.html' ) ;
}
?>
302 redirect directs would work best here.
Thanks for all your comments. We ended up going with a different solution. We used the onerror js tag to point to the appropriate noimg location. This was cleaner and easier for our environment
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]
Currently urls are like this http://example.com/?p=2, so if I link to another page in my site, it will be
A database contains a list of pages, with 3 columns, id, title and content. So for example the page with id 2 has a title of foo.
I want the user to be able to enter the url: http://example.com/foo and have the user see what is on http://example.com/?p=2. So I will be able to link to pages like: but the URL in the address bar will remain the friendly (foo) version.
I'm not sure if this is the right syntax, but logically you should have in your .htaccess file something like this:
RewriteEngine on
RewriteRule ^(.*)$ index.php?title=$1
then inside index.php you GET title and do what you want with it.