I don't know how to make a rewrite condition to obtain the right behaviour.
This is the link i have:
http://www.example.com/variable/Known/Uri/ecc
or
http://www.example.com/variable
I'd like to ignore the "variable" part and consider only the rest of the uri.
I will consider "variable" only when reading $_SERVER["REQUEST_URI"].
I think what you want to do is:
RewriteEngine On
RewriteRule ^[^/]+(.*)$ $1 [L]
Related
I have .htaccess code to maintain my URLs:
RewriteRule ^2014/?(?:([^/]+)/?|)(?:([^/]+)/?|)$ /data/2014/index.php?section=$1&subsection=$2 [L]
I need to modify it for cases, when i run form with get method. It makes output as (for ex.)
myweb.com/2014/about/?person=1&page=2
which I want URL to understand, I mean to get in the end hidden
myweb.com/data/2014/index.php?section=about&person=1&page=2
Thank you for any help.
You need the QSA flag:
RewriteRule ^2014/...$ /data/...&subsection=$2 [L,QSA]
^^^ here
That will append / combine the original query string to the rewritten url.
I'm trying to wrap my tiny brain around how the .htaccess can convert my somewhat undesirable URL into a cleaner eye candy link.
This is my current, scruffy URL.
expand.php?category=Mods&subcategory=Wrestlers&faction=WWE
And what I would like it to be render as, would be something perhaps like this?
expand/Mods/Wrestlers/WWE/
Am I right in thinking this is correct syntax to perform this? Because it doesn't seem to do anything right now!
RewriteRule ^([^/]+)/([^/]+)$ expand.php?p=$1&sp=$2 [L]
I would appreciate it if some bright chap might be able to help me out of this pickle!
You can do it like this. You will need to make sure the rewrite matches the URL you wish to internally redirect too, which is expand.php?category=Mods&subcategory=Wrestlers&faction=WWE
So to take care of that you should be able to use this.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^expand/([^/]+)/([^/]+)/([^/]+) expand.php?category=$1&subcategory=$2&faction=$3 [NC,L]
Try
^expand/(\w+)/(\w+)/(\w+)?$ expand.php?category=$1&subcategory=$2&faction=$3
To internally rewrite expand/Mods/Wrestlers/WWE/ to expand.php?category=Mods&subcategory=Wrestlers&faction=WWE, you'll need to make a regex that matches that first url. Your current regex would match an url with 2 parts, but your example url has 4 parts. I am pretty sure that doesn't fit ;-)
So... how do we fix it? Well, we make a regex with 4 parts. In fact, we know that the first part needs to be equal to expand, so we end up with this:
RewriteRule ^expand/([^/]+)/([^/]+)/([^/]+)/?$ /expand.php?category=$1&subcategory=$2&faction=$3 [QSA,L]
If you add this to the .htaccess in your www-root and we now go to http://example.com/expand/Mods/Wrestlers/WWE/, we should see whatever expand.php outputs with those parameters.
Is there a way I can use PHP (and/or .htaccess) to rewrite the URL of a page.
For example if a user goes to www.mysite.com/french they are actually accessing the page which is www.mysite.com/index.php?lang=fr
But not a redirect.
You want to use mod_rewrite and an .htaccess file to achieve this.
RewriteEngine On
RewriteBase /
RewriteRule ^french/(.*)$ /index.php?lang=fr [L,QSA]
Yes, using Apache mod_rewrite and appropriate rules in an .htaccess file.
The docs on mod_rewrite are here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
on the Apache site you can find several examples of URL rewriting flavors, by the way it's enough to use something like this in an .htaccess file:
RewriteEngine on
RewriteRule ^french(/|)$ /index.php?lang=fr [flag]
Where [flag] can be one of the following:
[PT]
[L,PT]
[QSA]
[L,QSA]
You may want to have a look at the PT (passthrough) flag docs or RewriteRule flags docs.
Also, pay attention to what your links are pointing to: in fact, the RewriteRule first argument is a regular expression that will be used to match the URLs to be rewritten. At the moment,
^french(/|)$
matches "french", right after the domain name,followed either by a slash (/) or nothing (that's the meaning of (/|) ); that is, it'll match www.mysite.com/french and www.mysite.com/french/ but nothing else. If you need to parse query string arguments, or subpaths, then you may need a more complex regex.
I have the following rule:
RewriteRule ^(image/[0-9]*/(.*))?$ image.php?prettyUrl=true&nav=$1 [QSA,NC,L]
This is supposed to replace
http://mysite.com/image/12345/imagename.png => http://mysite.com/image.php?pretyUrl=true&nav=image/12345/imagename.png
For the particular case where the pretty url contains '/image/' the $_GET array is empty. If I use a different name like 'images' or 'asdfg' the $_GET array will contain both pretyUrl and nav.
Any idea why?
Thanks!
If the rewrite rule must work for every URL in image/, (and the /1234/ digits part is not required), then this may be what you are looking for:
RewriteRule ^(image(/.*)?)$ image.php?prettyUrl=true&nav=$1 [QSA,NC,L]
If you can phrase your question a bit more specifically, a better solution can be refined.
I've just deployed a new site using Zend Framework. Due to the popularity of my tutorials I'd like to redirect any request for a tutorial to the relevant page on the new site. So far this is what I've got:
URL before Rewrite: http://neranjara.org/tutorials/?tid=56
URL after Rewrite: http://neranjara.org/article/id/56
The .htaccess file I'm attempting to use looks like this:
$ cat html/.htaccess
RewriteEngine on
RewriteRule tutorials/\?tid=(.*)$ /article/id/$1 [R=301]
RewriteRule !\.(js|ico|gif|jpg|png|css|xml|phps)$ index.php
But this rule is not matching any URLs ... :'(
Does any one see a problem here?
Based on your previous entry:
$ cat html/.htaccess
RewriteEngine on
RewriteRule tutorials/\?tid=(.*)$ /article/id/$1 [R=301]
RewriteRule !\.(js|ico|gif|jpg|png|css|xml|phps)$ index.php
I'd suggest using this instead:
$ cat html/.htaccess
RewriteEngine on
RewriteCond %{QUERY_STRING} ^tid=([^&]*)
RewriteRule tutorials/ /article/id/%1 [R=301, L]
RewriteRule !\.(js|ico|gif|jpg|png|css|xml|phps)$ index.php [L]
BTW, this is just an example of the many things you could do using the QUERY_STRING variable in mod_rewrite. My vote goes to 'lpfavreau' since this is option #2 from their answer.
The query string (the parameters passed to your file) won't be in the RewriteRule.
Taken from http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule:
The Pattern will not be matched
against the query string. Instead, you
must use a RewriteCond with the
%{QUERY_STRING} variable. You can,
however, create URLs in the
substitution string, containing a
query string part. Simply use a
question mark inside the substitution
string, to indicate that the following
text should be re-injected into the
query string. When you want to erase
an existing query string, end the
substitution string with just a
question mark. To combine a new query
string with an old one, use the [QSA]
flag.
You have two possibilities here:
Remove your first RewriteRule and do the verification in your index.php instead before continuing to your framework. The initial query should be available in $_SERVER['REQUEST_URI'] or something like that. So verify if it's tutorials, take the tid parameter and then go on with a redirection:
header("Location: http://http://neranjara.org/article/id/$id");
exit();
Use RewriteCond with %{QUERY_STRING} instead as stated in the Apache documentation. This solution is discussed in thread like this one.
// Edit:
Have a look at Chris' answer who was kind enough to detail the solution using QUERY_STRING. This is probably what you'll want to use. Thanks Chris.
Zend uses all the htaccess power that htaccess can deliver so theres a very handy(chainable and interesting and not very well documented) method to achive this in the bootstrap!
You must use the Zend Router in your bootstrap (index.php). Probably something like: (this would be foo.com/article/23
$router =
$frontController->getRouter();
$route = new
Zend_Controller_Router_Route(
'article/:id',
array('id' => 1) ); $router->addRoute('article', $route);
More info here