I want to pass variable in .html extension pages and .html pages are made up of mod rewrite so these are not html files but php script which are made .html through mod rewrite.
any clue?
EDIT: My .htaccess file*:
RewriteEngine on
RewriteRule ^watch/(.*)/(.*).html$ video.php?tag=$1&video=$2 [L]
RewriteRule ^(.*)/([0-9]+).html$ index.php?tag=$1&page=$2 [L]
RewriteRule ^(.*).html?([a-zA-Z=]+)$ index.php?tag=$1 [L]
* As supplied in the comments. Spacing may be different than the original.
Add the [QSA] flag to your rewrites. That will take the original query string and add your new params to it.
RewriteRule ^(.*).html$ index.php?tag=$1 [L,QSA]
This will rewrite whatever.html?orderby=views into index.php?tag=whatever&orderby=views.
The only catch is, with PHP anyway, whatever.html?tag=somethingelse will give you some weirdness. ($_GET['tag'] will have two values, but only the one that shows up last will be the real value.) But that's generally fine; you just have to be sure not to provide URLs like that, and you can just not care what people trying those wacky URLs see. (Assuming, of course, that you validate $_GET['tag'] properly.)
Related
I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.
I'm trying to implement a SEO friendly URL using .htaccess by using the RewriteRule below
RewriteRule ^n/article/([a-zA-Z0-9]+)/$ article.php?title=$1
The actaul URL looks like this
http://localhost/n/article.php?title=this-is-the-first-news-article
but I want it to look like this :
http://localohst/n/article/this-is-the-first-news-article
When I applied the RewiteRule above it does not change to the desired URL
This should do it. You are missing the n. Not sure why you need the word title though.
RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1
You have to capture the query string first. You can't do that with a RewriteRule because they ignore the query string. Here we're using [R] to redirect. If this is working for you and there is the potential that the old URLs are being stored somewhere as links, then you may want to specify [R=301]. Be sure to remove all old-style links from your site though (that contain the previous link format we're rewriting), that way you're not penalized for not updating your links.
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} title=([^&]+)
RewriteRule ^n/article.php n/article/%1? [R,L]
If your site needs the formatting to function from the original, you might also need this after the first rule. This rule quietly redirects the URL back to the original without showing it to the end user:
RewriteRule ^n/article/(.*) n/article.php?title=$1 [L]
it will be RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1
I'm wondering if multiple entries on htaccess will work for 301 redirects.
The problem I see is that old site files have 'html' extension and are named named differently so a simple global redirect won't work, it has to be one rule per file name.
This sites receives 1000 visits daily so need to be careful not to penalize search engine.
RewriteEngine On
RewriteBase /
RewriteRule ^file1\.html$ http://www.domain.com/file1.php [R=301,NC,L]
RewriteRule ^file2\.html$ http://www.domain.com/file2.php [R=301,NC,L]
RewriteRule ^file3\.html$ http://www.domain.com/file3.php [R=301,NC,L]
RewriteRule ^file4\.html$ http://www.domain.com/file4.php [R=301,NC,L]
A php header rewrite will not work as the old files are html type.
I suppose you could use some regex to reduce the number of different RewriteRules you are using, as those are all looking the same way.
In your case, using only this one might be OK :
RewriteRule ^(file1|file2|file3|file4)\.html$ http://www.metaboforte.com/$1.php [R=301,NC,L]
This way, you specify exactly what you want to rewrite ; but only have 1 RewriteRule.
Or, a bit more generic :
RewriteRule ^file([0-9]*)\.html$ http://www.metaboforte.com/file$1.php [R=301,NC,L]
Which allows you to define that you want to rewrite every fileXYZ.html, with XYZ a number. (As I used '*', no number at all would be taken into account by that rewrite rule ; if you want at least one number, you should use '+')
You could also do something even more generic -- not sure you want that, but something like this might do :
RewriteRule ^(.*?)\.html$ http://www.metaboforte.com/$1.php [R=301,NC,L]
Here, you are redirecting everything that end with .html
Why not just...
RewriteRule ^file([0-9]+)\.html$ http://www.metaboforte.com/file$1.php [R=301,NC,L]
Or, if you want to rewrite everything on the old site...
RewriteRule ^(.*?)\.html$ http://www.metaboforte.com/$1.php [R=301,NC,L]
Maybe you can try the RewriteMap Directive
With the new Diggbar, you can put http://digg.com in front of any URL that you are currently at and it will create a Digg short URL. I am only assuming they do this by modrewrite (though I am not sure since I am new at this all).
How is this done? It seems to me when I try this with a website I am working on, it bombs out.
I want to be able to do the following:
http://example.com/http://stackoverflow.com/question/ask
and have a modrewrite that will allow this to go to
http://example.com/index.php?url=http://stackoverflow.com/question/ask
But when I use this modrewrite:
RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)$ /message.php?id=$1 [L]
it doesn't work. What am I doing wrong?
You have to take the value from the request line because Apache removes empty path segments. The initially requested URI path /http://foobar/ becomes /http:/foobar/. But the request line (THE_REQUEST) stays untouched:
RewriteCond %{THE_REQUEST} ^GET\ /(https?://[^\s]+)
RewriteRule ^https?:/ index.php?url=%1 [L]
You're only looking for letters and numbers in that regular expression, so it won't pick up the colon and slashes. You're also using index.php in the example and message.php in the htaccess ;)
You'll probably want something like this:
RewriteEngine on
RewriteRule ^http://(.+)$ /index.php?url=$1 [L]
This makes sure you only catch URLs here, and you can still have regular pages! (Think about what would have happened if you went to example.com/index.php, you'd end up in an infinite loop.)
i am working for a site,which is in php.....i want to rewrite url
e.g www.3idiots.co.in/stories.php?id=17
if i want to rewrite it as
www.3idiots.co.in/stories/17.html
can any one tell me the code for this to write in .htaccess file.?
I'm assuming you're using Apache with mod_rewrite. Something like
RewriteEngine On
RewriteRule ^/stories/([0-9]+)\.html /stories.php?id=$1
should do the trick. Of course you'll need to make sure that RewriteRule is allowed in that directory. See this wiki page for more information.
mod_rewrite can only rewrite/redirect requested URIs and not those that are in your HTML documents. So you should first make sure, that your PHP application is printing the correct URIs, so /stories/17.html instead of /stories.php?id=17.
After that, you can use the rule suggested by José Basilio:
RewriteRule ^stories/([0-9]+)\.html$ stories.php?id=$1
Though redirecting requests of /stories.php?id=17 externally to /stories/17.html and then internally back to /stories.php?id=17 is possible, it’s not good practice as that would result in twice as many requests. But here’s the rule for that:
RewriteCond %{THE_REQUEST} ^GET\ /stories\.php[?\s]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&*([^&].*)?$
RewriteRule ^stories\.php$ /stories/%3.html?%1%4 [L,R=301]