I'm trying to get pages that call a variable (from my OLD site) to point to the root level of my blog... using my htaccess file. My knowledge of the htaccess syntax, isn't very good...
For example, any of these pages...
www.example.com/blog/?paged=100
www.example.com/blog/?paged=84
www.example.com/blog/?paged=3
... will ALL be rewritten as ...
www.example.com/blog/
I tried...
RewriteEngine on
RewriteRule ^blog/paged=?$ $1/blog/$2 [R=301,L]
... but that only works for paged=1. Any other variable didn't work (i.e. 2, 100). Can someone help with the correct way to do this?
To be able to match query string you need to use RewriteCond like this.
If /blog/.htaccess doesn't exist:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^paged=\d+
RewriteRule ^blog/?$ /blog/? [R=302,NC,L]
If /blog/.htaccess exists:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^paged=\d+
RewriteRule ^/?$ /blog/? [R=302,NC,L]
Related
i need change the site url path
from
site.net/open.php?cat=22&book=2285
to
site.net/book/2285
i think this code need edit and complite
RewriteEngine On
RewriteRule ^book/([^/]*)$ /books/open.php?cat=22&book=$1 [L]
how to do it by .htaccess and RewriteRule?
i need code work in all categorys also not 22 only
This code should work for you -
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)cat\=22($|&)
RewriteCond %{QUERY_STRING} (^|&)book\=2285($|&)
RewriteRule ^open\.php$ /book/2285? [L,R=301]
I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!
My current HTACCESS looks like this, however it doesnt do what I want.
RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L]
You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.
If you want people to type the php script in the URL bar you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/
Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}
For both ways and 301 redirect to pretty URL:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]
I need to get the following URL rewrite syntax correct and am struggling.
xxxxx.com/public_html/food/
Needs to be rewritten as:
xxxxx.com/food/
I tried this and it doesn't work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/public_html/food/(.*)$
RewriteRule ^(.*)$ http://xxxxx.com/food/%1 [R=301,L]
My client is using Joomla (which I am not familiar with), so I need to do so using the .htaccess file per everything I have researched so far. I am just struggling getting the syntax to work correctly though.
Thanks!
Try this, .htaccess
for Rewrite
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [QSA,L]
for Redirect (add R=301)
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [R=301,QSA,L]
Edit:
If you want to rewrite all urls (including /food)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
It will allow you to access site like,
http://xxxxx.com/food/ will point to http://xxxxx.com/public_html/food/
http://xxxxx.com/sample/ will point to http://xxxxx.com/public_html/sample/
http://xxxxx.com/anything/cool/?product=123 will point to http://xxxxx.com/public_html/anything/cool/?product=123/
i am struggling to get my site htaccess work... but no luck..
below is the code
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/? /profile.php?username=$1 [L,NC]
</IfModule>
this works fine no issues.. but when i add something it will not work.. in following cases -
1) For example when i type example.com/dinesh it will redirect me to www.example.com (always home page instead i want it to be www.example.com/dinesh
2) now i have another users.php with two parameters the thing is when i pass one parameter it should execute this rule profile.php but when i pass two parameters then it should take me to user.php i tried so many combination but this is not working.
if any expert can give me atleast some tips that will be great.
As for 1), it probably doesn't have anything to do with the above rules. If /dinesh exists, you should look in there. If it doesn't exist, you should look into /profile.php, that's probably what's redirecting you to the home page.
As for 2), your rule:
RewriteRule ^([^/]+)/? /profile.php?username=$1 [L,NC]
Matches the URI: /something/else, because the regular expression doesn't have an end-of-string match. It matches the first something, and that's good enough. If you add a $ to the end, it won't match /something/else.
RewriteRule ^([^/]+)/?$ /profile.php?username=$1 [L,NC]
Or, you can place the other rule that routes to user.php before the one that routes to profile. but it's better to have the $.
My problem is very simple.
I have several folders in my root:
[folder1]
[folder2]
[folder3]
And my domain is something like: http://johndoe.net
I keep my website in [folder1], so basically I access it by typing http://johndoe.net/folder1
My goal is this:
I want to type http://johndoe.net and I get the content of http://johndoe.net/folder1 but the address has to remain http://johndoe.net
I want to type http://johndoe.net/folder1 and see my website, but my address has to change to http://johndoe.net
Seems easy enough, but I failed finding the solution after several hours of searching.
So far, the only thing I achieved is redirecting from http://johndoe.net to http://johndoe.net/folder1 by placing this bit of code in my .htaccess in my root:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^johndoe\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.johndoe\.net$
RewriteRule ^/?$ "http\:\/\/johndoe\.net\/folder1\/" [R=301,L]
When I type http://johndoe.net, I get http://johndoe.net/folder1 in my address bar
but what I need is my address to remain http://johndoe.net
Could anyone help me with this?
You're going to use two .htaccess files to solve this problem. Here's what you put in them, and where they go.
File #1. This goes in your base folder (the one that contains [folder1],[folder2], etc)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((www\.)?johndoe.net)$
RewriteRule ^(.*)$ /folder1/$1 [L]
File #2. This goes in [folder1]
RewriteEngine Off
Try something like this
RewriteRule ^(.*)$ /folder1/$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^johndoe\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.johndoe\.net$
RewriteRule ^/?$ "\/folder1\/" [L]
you need internal rewrite, So it's enough to rewrite / to /folder1/