I'm trying to use modrewrite to change my urls from /foo.php to /foo and from /foo.php?lang=en to /en/foo. The problem I have is that I think I need to use 301 redirect to move the .php-less address to the .php address, or else my ranking will be splitted among these 2. But if I use [R=301] the address in the bar changes to the .php one making my pretty url efforts quite useless. What should I do?
I've looked around for any question\tutorial i could find but I can't fully understand modrewrite. The main issue I have is that if I change my .htaccess file then revisit an already visited page the new .htaccess is not working but is somehow caching the old result making correction a real pain. Do you know a workaround?
Thank you,
Mokuchan
Sounds like MultiViews would be a better solution than rewriting the URL. Just put this in your .htaccess. What that will do is essentially, if i type in stackoverflow.com/pages it will first look for a directory named pages, if it can't find it, a file. You can then have stackoverflow.com/pages/view/some/page where /view/some/page is the query.
Options Indexes FollowSymLinks Includes MultiViews
Edit To get the /view/some/page/ use $_SERVER['PATH_INFO'];
You definitely must look at Apache modul mod_rewrite and general idea of rewriting urls.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Related
I have always understood (unless im mistaken) that Apache's modrewrite engine requires
Options +FollowSymLinks
in order to work.
We have used modrewrite to hide the .php extension in addresses on a particular system in order to not reveal the chosen technology - PHP. We understand that one can still learn the server technology but you'd at least need to know how web servers work etc.
The problem is, the server tech's have brought up the risk in using +FollowSymLinks which i completely understand and agree with.
https://serverfault.com/questions/195570/htaccess-security
Aaron Copley: Symlinks aren't necessarily bad but you have to have a clear understanding of your implementation of Apache. To a non-chrooted
Apache, symlinks certainly pose a significant risk to exposing files
outside of your document root.
At the moment the system parses REQUEST_URI as such:
All rewrite rules are written to index.php
URL domain.com/request
REQUEST_URI = /request (trimmed as "request")
Using PHP switch () we check case 'request' : inlclude xyz.php;
exit;
This is a fairly common technique, but how would i implement the same result without the need for +FollowSymLinks and without having to go through every script in the system and change navigation links?
modrewrite will also work if you enable the following:
Options +SymlinksIfOwnerMatch
This causes Apache to check the owner of the link and the target, and only follows the link if the owners match.
Perhaps your server guys would accept that as a reduced risk?
More info here: http://onlamp.com/pub/a/apache/2004/02/19/apache_ckbk.html
The Apache documentation states
If your administrator has disabled override of FollowSymLinks for a user's directory, then you cannot use the rewrite engine. This restriction is required for security reasons.
Check this link:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Ok I know im answering my own question, but im going out on a limb...
I should probably have mentioned before that the site will NOT be public as it is an administrative system so we don't care about search engines
Would i be able to do this instead of the existing implemented modrewrite:
.htaccess file:
ErrorDocument 404 /index.php
index.php
header("Status: 200 OK");
header("HTTP/1.0 200 OK");
I know this is messy, but we do not have time and the server tech guys will not budge, the $_SERVER['REQUEST_URI'] should still contain the same info???
Please feel free to comment and down/upvote, but please remember i know this is extremely cowboy and it's merely a temporary workaround
Important Note
POST requests do NOT work this way because Apache redirects to index.php (losing the POST data) you could still use GET info
I am building a php+mysql site which will have numerous articles. I am pretty ok with html php jquery etc. I need to now what are the steps I need to take in order not have http://www.mysite.com/articles.php?id=123 but to have http://www.mysite.com/articles/123-title-of-article?
Thanks
Well, you need, for instance, to store the token for each article (to optimize), like your example "123-title-of-article". Next step is to use .htaccess and mod_rewrite from Apache (if is your case).
If you will do it only on this articles "page folder" (articles/), I suggest to you make this folder valid and make the .htaccess inside that and redirect to articles.php?id={POST_ID_FOUND_BY_TOKEN}.
Take a look too on $_SERVER["REQUEST_*"] variables from PHP.
See: this page, on end of article have some examples.
The usual way to do this is by using mod_rewrite.
You create a .htaccess file which, behind the scene, redirects the latter request to the former.
You can read about it here: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
You'll need something called mod_rewrite, which is an apache module.
The configuration looks like this (stolen from a client's drupal install):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule articles/(.*)$ articles.php?id=$1 [L,QSA]
</IfModule>
I haven't tested this, but it should work.
I have a website that passes some GET variables to different pages in PHP. My issue is that now I have a url with variables i.e. index.php?category=categoryname and that's not very memorable.
Is there any way I can change the URL to something like /categoryname instead without duplicating the page and storing in folders? But also allow users to type in /categoryname and be redirected to the correct page?
.htaccess Apache mod_rewrite, almost every professional dynamic website uses this method (like stackoverflow).
The method is fully explained in this article far better then I could ever explain it in this answer box.
You should look into writing some apache Mod_Rewrite rules in a .htaccess file.
The solution is discussed here:
this is done by the rewrite module of apache and this handles regular
expressions. You have to put a rule
like this in your .htaccess file on
the root of your website:
RewriteRule ^cat/([0-9]+)$
/index.php?category=$1
^ means the start of the url after
www.example.com/ $ means the end of
the page.
www.example.com/cat/123
will be converted by the server to:
www.example.com/index.php?category=123
In PHP you use the normal $_GET['id']
variable. The rewrite module must be
enabled by apache... This is mostly
used to make the url format
independent of the serverside
scripting language so the .php in the
url is not logical. Thats why i
changed it to product/ . The .htaccess
starts with
RewriteEngine On Options
+FollowSymLinks RewriteBase / Here all the rewrite rules.. ...
What is the best way to dynamically redirect a friendly looking url on Linux to dynamic on Windows?
ex.
domainone.com/dir/one/two
redirects to
domaintwo.com/index.aspx?a=one&b=two
and
domainone.com/dir/three/four
redirects to
domaintwo.com/index.aspx?a=three&b=four
The HTTP header called Location is how you redirect a user across hosts/domains.
Depending on your server configuration and the mechanism used to generate the HTTP headers, the specific implementation will vary. An example in PHP (as your question appears to be tagged) is to include the following code:
header('Location: http://domaintwo.com/index.aspx?a=one&b=two');
The string above is like any other string, so apply the appropriate logic to provide the desired redirection URL.
The same effect is also possible in the domain configuration files (the precise path differs across server software and operating system) or more conventionally in .htaccess files. If you provide more information about your hosting environment, someone will be able to help you devise the rewrite rule you need. I prefer to put this level of smart rewriting in a PHP script, since I think .htaccess files tend to be harder to manage and "read".
From within Apache:
Either in a server configuration file, or more likely in an .htaccess file.
You can use mod_rewrite to do this, but as you want a redirect, it would be more appropriate to use mod_alias and the RedirectMatch statement.
RedirectMatch 301 ^/a/([^/]+)/([^/]+)$ http://domaintwo.com/index.aspx?a=$1&b=$2
Rewrite variant:
RewriteEngine On
RewriteBase /
RewriteRule ^/a/([^/]+)/([^/]+)$ http://domaintwo.com/index.aspx?a=$1&b=$2 [R=301,L]
Note the use of 301, that is a permanent redirect, use 302 for temporary, or when you always want people to redirect rather than going directly on future accesses.
A pretty standard way to approach this on Linux is to use Apache with mod_rewrite (how to install and configure Apache and mod_rewrite, if it's not already set up, will depend on your Linux distribution)
Then, in your Apache configuration, you can add a line like:
RewriteEngine On
RewriteRule ^/a/([^/]+)/([^/]+) http://www.domaintwo.com/index.aspx?a=$1&b=$2
Is it possible to associate /upload/picture to /upload.php?what=picture with htaccess and when I submit a form to /upload/picture it is taken by the upload.php? If it is, how?
The question is slightly awkwardly worded, but if I understand you correctly, what you're looking for is called mod_rewrite.
mod_rewrite is a plug-in for the Apache web server which allows you to change your old-style query URLs into other formats. You will need to have mod_rewrite installed to be able to use it, obviously. Most Apache installs these days do include it by default, though.
mod_rewrite commands are entered into your htaccess file. Something like this should do it for you (but note that I haven't tested this! you may need to tweak it!):
RewriteEngine on
RewriteRule ^upload/([^/\.]+)/?$ upload.php?what=$1 [L]
Give this a go :)
RedirectMatch 301 /upload/(.*)$ http://www.yoursite.com/upload.php?what=$1
I think that should do it.