How to make Apache rewrite blog URLs? - apache

I've installed WAMP, made an alias, and put into that folder a ".htaccess" file. My goal is to have a URL such as "foo.com/blog/bar-baz" internally call "display.php?name=bar-baz". Among various things I tried the following:
RewriteRule ^blog/(.+)$ display.php?$1
However, this gives a "The requested URL /Users/.../public/display.php was not found on this server". Playing around I was able to get this to work:
RewriteRule ^blog.php$ /personal/display.php
Advice greatly appreciated.

You can try this rule from root .htaccess:
RewriteEngine On
RewriteRule ^blog/(.+)$ /personal/display.php?name=$1 [L,QSA,NC]

Related

Redirect urls with query string to seo-friendly directory like urls

I know this question is asked by many users, also I gone through many questions to find the solutions as I am unable to understand the code used in htaccess file. So please kindly help me to solve this issue. I just want to convert a link like:
www.abc.com/project.php?proj-cat=some+project+name
to url like:
www.abc.com/project/some+project+name
OR to like:
www.abc.com/project/some-project-name/
I googled and found a website which creates a htaccess rule for you, using this site I got this:
RewriteEngine on
RewriteRule project/proj-cat/(.*)/ project.php?proj-cat=$1 [L]
but this doesn't redirect to the links above.
Please help me to find the solution for this and help me understand how it works. Thank You!
You can use this code in root .htaccess:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+project\.php\?proj-cat=([^\s&]+) [NC]
RewriteRule ^ project/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^project/([^/.]+)/?$ project.php?proj-cat=$1 [L,QSA,NC]
Reference: 1. Apache mod_rewrite Introduction
2. http://askapache.com

htaccess mod_rewrite giving me 404

SO I have this in my .htaccess file
RewriteEngine On
RewriteBase /core/
RewriteCond %{QUERY_STRING} ^page=([0-9]+)$
RewriteRule ^page page/%1/? [L]
my url is
http://localhost/core/page.php?page=8
with the rules applied I'm getting..
Not Found
The requested URL /core/page/8/ was not found on this server.
This is running on wampserver 2.2
the file structure looks like
c:/wamp/www/core
the .htaccess is inside the /core/ directory.
What is it that I'm missing.. i've checked my apache.conf file and it looks fine.
I think you got it the wrong way around. When logically thinking of rewriting you don't rewrite original URL to new URL (for example page.php?page=8 to page/8/) you actually rewrite page/8/ to page.php?page=8. You tell the server how it should interpret the unfamiliar URL.
So if I understood correctly what you want to achieve is:
User visits localhost/core/page/8/
User is served (under the hood) localhost/core/page.php?page=8
I believe the following RewriteRule will do the trick (The query string condition is not necessary):
RewriteRule ^page/(\d+)/$ page.php?page=$1 [L]

Apache Rewrite: Simple exception not working for image types

This question seems simple to me though I can't seem to get it to work.
I'm trying to do a simple rewrite for all requests except defined image types.
Three very important clarifications:
This is being done with the .htaccess file in the image directory that accessed from a browser is located at example.com/images/ so the .htaccess file is located at public_html/images/.htaccess locally.
This solution needs to play nicely with the awesome help I received from other people at this question: Apache Rewrite: image directory based on HTTP host
I'm using shared hosting, not VPS or dedicated so my access is limited; I know this isn't going to be complicated anyway.
I've tried the following (along with modifications of it) without success:
public_html/images/.htaccess
RewriteEngine on
RewriteRule !\.(gif|jpg|png)$ index.php?q=$1 [L]
I think I'm probably just missing something simple here.
I presume you are trying redirect everything to index.php when the request to images folder is not a request to an image file.
Try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$ [NC]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Rewriting url using mod-rewrite and apache

I'm trying to add some rewriting on my site, but it seems to not work, I'm using apache and .htaccess.
The code in my .htaccess file is:
RewriteEngine On
RewriteRule ^/?os_framework/?$ /os_framework/index.php?module=home [L,NC,QSA,PT]
This should send http://localhost/os_framework/ to http://localhost/os_framework/index.php?module=home
But it seems not to.
Any help would be appreciated.
In advance, thanks
Edit: Fixed the above, shouldn't have the os_framework/ in the search pattern, however now i cant get this one to work:
RewriteRule ^/(.[^/]*)/?$ /os_framework/index.php?module=$1 [L,NC,QSA,PT]
And what is wrong with
RewriteRule ^(.[^/]*)/?$ /os_framework/index.php?module=$1 [L,NC,QSA,PT]
Why does that throw a error 500? it should work
Try this:
RewriteEngine On
RewriteRule ^os_framework/?$ os_framework/index.php?module=home [L,NC,QSA]

How do I add .gif to pathnames in Apache httpd?

This is a simple htaccess question for experts but I have been trying to get this sorted for a while. This was something a developer did before my time with this code. He truncated the image file extension from the requests. As an example,
/images/btn/Find a bear should get the URL changed internally to /images/btn/Find a bear.gif
All the images in the folder are .gif extensions.
I tried this URL rewrite at the root folder but it did not help.
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^images/(.*)$ images/$1.gif
I know that RewriteRule is enabled on the server, etc. Please help.
You’re probably having an infinite loop. Try this:
RewriteCond $1 !.*\.gif$
RewriteRule ^images/(.*)$ images/$1.gif