This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Url Rewriting Help
How do I get a url ending in /index.php?p=2 to display as /2 instead using .htaccess (pretty urls)?
I currently am working on a webcomic displaying site but I have run into a problem with my .htaccess file. I am trying to rewrite /index.php?p=2 to /2 but for some reason it is not working.
So yeah any help would be much appreciated!
.htaccess can't rewrite /index.php?p=2 into /2. You are approaching the problem in reverse.
Rewrite rules tells Apache to do something when fetching a specific URI. What you really need to do is tell Apache to "rewrite" a request to /2 so that it fetches /index.php?p=2 instead. You can do so with the following RewriteRule:
RewriteRule ^([0-9]+)$ index.php?p=$1 [L]
and then change, in your HTML, every single link to point to the rewritten URI instead of the canonical one. Apache will not "rewrite" your links for you. It will however, with the help of the rule above, fetch the proper resources when the client queries for /2.
Make an .htaccess file similar to this one... (the flags may need to be modified/varied per mod rewrite flag documentation: http://httpd.apache.org/docs/2.3/rewrite/flags.html)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([A-Za-z_0-9-]+)/?$ index.php?p=$1 [QSA,L]
Related
I know there are already a ton of these questions but I don't get it...
I'm trying to rewrite my /register.php to /register but I can't.
.htaccess code
RewriteEngine on
RewriteBase /
RewriteRule ^register$ register.php [L]
Does any of you know the answer?
Sorry for the stupid question but I'm kinda new to the whole .htaccess stuff...
Do you have enabled the Apache module mod_rewrite?
I use this pattern to create user-friendly URLs. It's pretty variable. It means, that every address in format letters-numbers with a certain file extension (PHP or HTML) will be translate into letters-numbers.php (for example).
RewriteRule ^([a-z]+)-([0-9]+)\.(php|html)$ /$1-$2.$3 [L]
How to rewrite this url "www.domain.com/index.php?route=custom/static/page" to "www.domain.com/page" in htaccess file, basically just want to take out index.php?route=custom/static/ from urls.
I don't know regex so I tried http://www.generateit.net/mod-rewrite/, but it only generates
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
which doesnt remove 'custom/static' from URLs, I tried a few other examples as well but only removes index.php? and doesnt pass variable, any help is appreciated.
Do you know the concept of using mod-rewrite?
In your question you have mentioned to use mod-rewrite to redirect
"www.domain.com/index.php?route=custom/static/page",
Here $_Get['route']="custom/static/page"] $url_parameter=$_Get['route']
to
"www.domain.com/page" [here $_Get['route']="page"],
So now you can mannually add "custom/static/" to the obtained value of $_Get['route']. as $url_parameter="custom/static"+$_Get['route'] //For PHP
Using your mod_rewrite you can fulfill your demands,
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
But if you need out of box solution using .htaccess then I suggest learning "rewrite-engine" instead of using generating tool
i have a little issue that some of you may be able to sort please
in my htaccess i have
RewriteRule apply /index.php?option=com_loans&view=apply&Itemid=102 [R=301,L,QSA]
basically meaning any get requests to apply will be sent on
however i have a page apply.html which with the above is no longer accessible as it creates a redirect loop
anyone know how to change the htaccess declaration so as only apply (and not apply.html) forwards on ?
tnx
Try to add a rewrite condition as well as limiting the matching pattern in the regex:
The condition will make sure that the rule will only be applied if the request isn't for an existing file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^apply$ /index.php?option=com_loans&view=apply&Itemid=102 [R=301,L,QSA]
Please help me to redirect using .htaccess like below.
http://info.domainname.com/blog/?Tag=somedynamictag
to
http://domainname.com/tag/somedynamictag
Thanks!
First of all I don't understand the purpose of rewriting http://info.domainname.com/blog/?Tag=somedynamictag to http://domainname.com/tag/somedynamictag when it had to be the other way round. People rewrite URLs to clean them (ie. remove characters like ?,&,=, etc.) but you are adding all these and making the URLs cumbersome.
I think you didn't properly understand the concept of URL rewriting. Let me explain a little.
When any URL is accessed on your website, the URL that the USER types or clicks (in your case http://domainname.com/tag/somedynamictag) is rewritten. But your question tells me that you think the other way. Your understanding is that the visitor clicks http://info.domainname.com/blog/?Tag=somedynamictag and will be rewritten to http://domainname.com/tag/somedynamictag. THIS IS WRONG!!. If you set up your website this way, each and every URL at http://domain.com/ must exist as a separate file or directory which a dynamic website like you seem to be developing is not expected to have. So I assume you have understood that you have either misformed the question or you have you have misunderstood the concept of URL rewriting. Following is the .htaccess code to redirect http://domainname.com/tag/somedynamictag to http://info.domainname.com/blog/?Tag=somedynamictag.
RewriteEngine On
RewriteCond %{HTTP_HOST} domainname.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9]+)\/(.*)$ http://info.domainname.com/blog/?$1=$2 [L,R=301]
If you think you have correctly typed the question and understood url-rewriting right and are sure what you are trying to do is right, then here's the htaccess code (if you want to redirect http://info.domainname.com/blog/?Tag=somedynamictag to http://domainname.com/tag/somedynamictag).
RewriteEngine On
RewriteCond %{HTTP_HOST} info.domainname.com
RewriteRule ^blog\/\?([A-Za-z0-9]+)=(.*)$ http://domainname.com/$1/$2 [L,R=301]
If that doesn't work, in index.php at http://info.domainname.com/blog/, place the following code:
<?php
header("HTTP/1.1 301 moved permanently");
header("Location:http://domainname.com/blog/tag/".$_GET['Tag']);
?>
And thank you for asking this question. While answering it, I learned many things.
Hope that answers your question,
Peace...
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
.htaccess redirect
How exactly, with a .htaccess file i'm presuming, would i redirect people from this URL: http://blog.twostepmedia.co.uk/win-traffic-with-charlie-sheen-and-the-yahoo-boss-api/ to something like http://www.twostepmedia.co.uk/blog/win-traffic-with-charlie-sheen-and-the-yahoo-boss-api/
Thanks :)
You're better off using a rewrite rule in your .htaccess of the blog.twostepmedia.co.uk domain.
Something like
RewriteEngine on
RewriteOptions MaxRedirects=10 # this will just stop and bugs in the code causing a infinite loop
RewriteRule ^(.+)$ http://www.twostopmedia.co.uk/blog/$1 [R=301,NC]
The rewrite rule grabs whatever follows 'blog.twostepmedia.co.uk/' and rewrites to the new url. The [R=301,NC] sets this to be a permanent redirect, which means you won't get penalised from duplicate content by search engine spiders.
Hope that helps :)