Rewrite a URL variable into an HTML file - mod-rewrite

I've seen many cases of these rewrite questions but I don't get all the regular experessions and browser errors and such so I was hoping that someone could help me.
Ok, so I have a file with a url variable 'pages.php?p=foo' and I would like to rewrite it so that it appears as 'foo.html' in the same directory.
I was thinking along the lines of:
RewriteRule (.+).html pages.php?p=$1
The trouble is, the browser displays 400 Bad Request errors.
I'm hoping there's a fix for this but I can't get my head around it.
Any help is much appreciated :)

Try this:
RewriteRule (.*)\.html pages.php?p=$1

Related

URL Rewrite on the basis of two parameters

i am new to URL Rewrite. i want to rewrite my URL like given below.
www.mywebsite.com/q/1st-parameter/2nd-parameter
to
www.mywebsite/1st-parameter/index.php?q=2nd-parameter
Please note that the 1st-parameter/2nd-parameter is a string and it also contain "-". the Rule which i used in .htaccess file is
RewriteEngine On
RewriteRule ^.+/q/([a-z,A-Z,0-9]+)/([a-z,A-Z,0-9]+) /$1/index.php?q=$2 [NC,L,R]
Your quick reply will be highly appreciated. kindly also look at the Flags, whether i am using the correct flags or not. infact i copied it from a forum but failed to get the correct results.
Thanks in Advance

Simple mod_rewrite producing unexpected redirect

Well I've driven myself crazy enough that I finally had to break down and ask for some help.
I have a simple rewrite rule in my .htaccess file in a folder called "myfolder"
RewriteEngine On
RewriteBase /myfolder/
RewriteRule ^([^/.]+)/?$ index.php?page=$1 [L]
If the url is /myfolder/home
In the index.php file located in "myfolder" I simply call
if (isset($_GET['page'])){$page = $_GET['page'];}else{$page = 'home';}
//then I include the content for requested page using
include('pages/'.$page.'/content.php');
//NOTE: I have checks to see if the page exists in the final code, but for now I am keeping it simple trying to track down this issue
If I print_r($_GET); It shows Array ( [page] => home )
So it works as expected and the page content loads, but if I look in the browser error console I see a php error and when I click on the error I see Array ( [page] => df )Warning: require(pages/df/content):
For some reason it's thinking that page = df and I can't for the life of me figure out why. Does it have to do with a rewrite loop or something?
Any help is much appreciated. Thanks in advance.
Ok I finally figured it out. I had a page.js file I was including that I thought was blank (long story why I'm including it) but it actually had "df" in it. I guess the combination of the file being named page.js and garbage javascript caused all kinds of havoc.
Oh the joys of coding never cease do they haha.
Thanks anyway guys.
PS I'll make this answered after the two day limit is up.

How can I rewrite a URL, splitting the path into separate querystring arguments?

I want to rewrite an URL such as: mysite.com/Section/Subsection/SubSubSection into
mysite.com/index.php?s=Section&sub1=SubSection&sub2=SubSubSection
I wrote this rule:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /index.php?s=$1&sub1=$2&sub2=$3
But the problem is that Section, SubSection and SubSubSection are not mandatory at all, but if I insert some '?' character it goes on Internal Server Error...
(I tried this:
RewriteRule ^([^/]*)(/([^/]*))?(/([^/]*))?/?$ /index.php?s=$1&sub1=$3&sub2=$5
but it doesn't work)
Another problem is that there are other URLs that should not be edited, such as /res/img/logo.png.
The site in question is dynamic, so I don't know the section names beforehand. I know URLs that should be excluded, of course.
Any solutions? Thanks in advance.
I think you should do something like that ...
RewriteRule ^/([a-zA-Z]*)/([a-zA-Z]*)/([a-zA-Z]*)/?$ /index.php?s=$1&sub1=$2&sub2=$3
regards,
Mimiz

mod_rewrite rule to replace a sub-string from a URL

I need to do this:
I have an old URL:
http://www.mysite.com/dev-site/some-content-part-here
That I want to make into:
http://www.mysite.com/live/some-content-part-here
Edit
I found somebody voted down on my question. I dont know what thought drove him/her to that but actually the question really was not that silly! May be the downer did not understand why I asked this... it could be anything in his mind. But I want all readers, if you dont like question - please always try to communicate through comments and then if the question poster does not agree your changes, then down vote. Thanks.
*And guys, thanks for editing.
Untested:
RewriteEngine On
RewriteRule ^/dev-site/(.*)$ http://www.mysite.com/live/$1 [R=301,L]
Note that it might work if you only use /live/$1 on the right hand side, but I'd do this to be sure.
I know you know this (since you've mentioned mod_rewrite), but for those that don't, you generally put this into the .htaccess file in the directory - either dev-site or root should work.

Rewrite URL to HTML file causing rewrite to .html/

I'm currently working on an overhaul of my blog site, and have found a way to convert all my current pages into static html pages. They are currently using friendly url's which remap to a central index.php page with GET parameters attached on.
The change I am trying to make is have those same friendly URL's map to their html counterparts. I am currently using this rule:
RewriteRule ^archives?/([^/]+)/([^/.]+)/?$ archives/$1/$2.html
The error log is reporting that it cant find blah.html/ which means it's looking for the .html directory, instead of the .html file. So a better example:
/archives/2009/original-name
should be getting mapped to
/archives/2009/original-name.html
but is really getting mapped to
/archives/2009/original-name.html/
What am I missing here?
don't you need to use it the other way around? I didn't test the code but it should be something like this:
RewriteRule ^archives/(.*)/(.*).html archives/$1/$2
I can't see anything obviously wrong with your regex.
At a guess I'd say you might have a rule somewhere following this, which is redirecting anything without a trailing slash to its equivalent with the slash (a common thing to do to avoid duplicate content issues).
You didn't escape your period in the 2nd statement. Try this.
RewriteRule ^archives?/([^/]+)/([^/\.]+)/?$ archives/$1/$2.html

Resources