How do I go about writing the conditions for mod_rewrite to make this example happen?
Have a URL this like...
http://domain.com/recent/5
Render this page...
http://domain.com/index.php?view=recent&page=5
And if there is no page number specified (note I want it to work with or without trailing-slash)...
http://domain.com/recent/
It will default to page 1...
http://domain.com/index.php?view=recent&page=1
But when someone just goes to the root domain...
http://domain.com
I want this page to render...
http://domain.com/index.php?view=popular&page=1
Thanks for any help.
Put this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+) /index.php?view=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?view=$1 [L,QSA]
RewriteRule ^$ /index.php?view=popular&page=1 [L,QSA]
Related
I have the following problem. I have a domain with SEO pages, generated in html each night. This URLs to the pages could look like this:
https://www.mypages.com/seo/florida/miami/somesuburb
but it's possible that "somesuburb" doesn't exist anymore. So it could be, that:
https://www.mypages.com/seo/florida/miami/somesuburb leads to a 404.
also
https://www.mypages.com/seo/florida/miami could lead to a 404.
So I tried to do this in my htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^(.*[\\\/])) /seo/$1 [L,R=301]
this works, for example:
https://www.mypages.com/seo/florida/miami/doesntexist
goes to:
https://www.mypages.com/seo/florida/
but if the Dir florida also does not exist, it breaks and gives a 404. I think it is because of the trailing slash.
so my next approach was:
RewriteRule ^(.*)/+$ /seo/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^(.*[\\\/])) /seo/$1 [L,R]
This leads into a loop :(
Could anyone help out here?
Greets!
found a solution:
RewriteEngine on
RewriteCond %{REQUEST_URI} /+[^\.]+$/
RewriteRule ^(.+[^/])$ %{REQUEST_URI} [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /seo/$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^(.*[\\\/])) /seo/$1 [L,R]
this goes back as long as a dir is valid
I have a .htaccess file that creates a SEO URL. For example, let's say the ugly URL is: example.com/stories?url=hello-world&page=5 the URL becomes example.com/stories/hello-world/5
This works perfectly, but for the first page I want the URL to not display the page number. For example, I want a URL like this example.com/stories/hello-world/1 to be example.com/stories/hello-world How do I do this?
Current .htaccess
RewriteEngine On
RewriteBase /stories/
RewriteCond %{THE_REQUEST} /\?url=([^&\s]+)&page=([0-9]+) [NC]
RewriteRule ^ %1/%2? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1&page=$2 [L,QSA]
Try this it like this,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# for hello world first page we are giving static value 1 to page.
RewriteRule ^([\w-]+)/?$ index.php?url=$1&page=1 [QSA,L]
# for others we are getting dynamic value.
RewriteRule ^([\w-]+)/([\d]+)$ index.php?url=$1&page=$2 [QSA,L]
I am trying to use mod-rewrite in .htaccess for implementing clean/pretty URLs.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ details.php?id=$1 [L,QSA]
RewriteEngine On turns the engine on.
RewriteCond %{REQUEST_FILENAME} !-f does not rewrite anything if the
request filename exists, and is a file.
RewriteCond %{REQUEST_FILENAME} !-d does not rewrite anything if the
request filename exists, and is a directory.
RewriteRule ^([^/]+)/?$ details.php?id=$1 [L,QSA] This is the actual
rewrite rule. It takes anything after the domain name (anything
other than forward slashes), and rewrites it to details.php, passing
it as the id parameter.
RewriteRule ^([^/]+)/?$ details.php?id=$1 [L,QSA] is working when request for http://www.domain.com/texas
This is well and good. What I need is that my request URL looks like this:
http://www.domain.com/location/texas
and details.php has the below code
<?php
$id = $_GET["id"];
echo "new id is ".$id;
?>
Problem: I couldn't write a valid .htaccess RewriteRule to identify this request http://www.domain.com/location/texas.
Please help.
What all I tried?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^location/?$ details.php?id=$1 [L,QSA] - not working
RewriteRule ^location/[a-z][-a-z0-9]*?$ details.php?id=$1 [L,QSA] - working, but id is displayed blank.
You can just tweak your existing rule by making starting location/ optional:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:location/)?([^/]+)/?$ details.php?id=$1 [NC,L,QSA]
I am trying to create mod_rewrite rules that rewrite based on the first level directory name plus a failover to rewrite to a standard file in case none of the directory names are matched.
Example:
I have units.php, models.php and other.php. The other.php file should handle all non-assigned requests.
http://www.mydomain.com/units/4435
Should redirect to /units.php?id=4435
http://www.mydomain.com/models/594
Should redirect to /models.php?id=594
http://www.mydomain.com/anything
Should redirect to /other.php?id=anything
http://www.mydomain.com/anything/893
Should redirect to /other.php?id=anything/893
Let me know if this makes sense. I am unsure of how to structure the rules and conditions to achieve what I want.
This is what I have tried to sfar. It works for URLs starting with 'units' or 'models' but I get a 500 Error if I try any other URL:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(units)/(.+)$ /units.php?id=$2 [L,NC]
RewriteRule ^(models)/(.+)$ /models.php?id=$2 [L,NC]
RewriteRule ^(.+)$ /other.php?id=$2 [L,NC]
I would suggest this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(units|models)/([0-9]+)/?$ /$1.php?id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(units|models)/[0-9]+/?$
RewriteRule ^/[^/]+/([0-9]+)/?$ /other.php?id=$1 [L,QSA]
I'm trying to rewrite a url of this:
http://www.foo.com/bar/baz
to
index.php?q=$1&d=baz
Where bar is not a fixed value, but baz is.
RewriteRule ^(.*)\/baz$ index.php?q=$1&d=baz [L,QSA]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
What i have above kinda works but unfortunately breaks all the includes in the site (css/javascript) but strangely all the pages work :/
This is a drupal install, (so the second line needs to remain).
UPDATE
This might help actually, i forgot to include
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
**RewriteRule ^(.*)/details index.php?q=$1&details=true [L,QSA]**
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
It seems to be doing my rewrite correctly, the only problem is it's ignoring the other conditional statements now, i.e. it's still attempting to rewrite files that exist (i.e. css,js) when it's mean to avoid them.
site is fine without my line (The one with the stars), but with it, the variables and pages work, but static files like css etc are also being rewritten....need to stop that!
Thanks in advance.
Shadi
Conditions only apply to the first rule immediately following. So try duplicating the condition lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/details index.php?q=$1&details=true [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteRule ^(.*)/details index.php?q=$1&details=true [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This is what finally worked. it just required the rearranging of the rules...