URL rewriting, using friendly urls - mod-rewrite

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.

Related

Magento custom module with custom url with add .html extension

I have created an module and its front end name is test .
Now i want a URL like
www.exmpale.com/test/red.html (original structure is like 'test/index/index/valuid/1/').
www.exmpale.com/test/green.html (original URL is like 'test/index/index/valueid/3/').
Wheremy crontroller is indexCrontroller and action is indexAction and where red /green ,will coming dynamic from a table.
The Table is structure is like
tab_id value
1 red
2 blue
3 green
Please help me.
You need to implement the Router for these URLs. please follow this link
http://inchoo.net/ecommerce/magento/custom-router-in-magento/
Use 301 redirect in htaccess file as follows:
Paste the below code in your .htaccess file in root folder:
Redirect 301 /test/index/index/valuid/1/ www.exmpale.com/test/red.html
Redirect 301 /test/index/index/valuid/3/ www.exmpale.com/test/green.html

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.

symbolic links not work with htaccess in xampp

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 !

Remove encoded question mark from URL

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

RewriteRule doesn't work as I expect

I want the following url:
http://www.mydomain.com/search/string
to go to
http://www.mydomain.com/dir/search.php?param=string
This is my rewrite rule:
RewriteRule ^search/(string)$ /dir/search.php?param=$1 [NC,L]
What's driving me crazy is that it sort of works. All the links within the result page have 'dir' within the a elements. For example, I hover the mouse over a link that shows
http://www.mydomain.com/search/myawesomescript.php
in the status bar. The correct URL is
http://www.mydomain.com/myawesomescript.php
The .htaccess file is at the root. Why does 'search' insert itself into the rewrite? What is it that I don't understand?
Your RewriteRule is telling apache that if requests come in to search/(string) they should be handled by /dir/search.php, but it does not do anything to change the URL as being displayed.
What you're talking about would be accomplished by something like this:
RewriteRule .* /dir/search.php?param=$1 [NC,L]
Keep in mind this wold redirect everything to search.php, so you'd want to either modify the regex or use some RewriteCond rules to limit the scope.

Resources