My .htaccess file currently looks something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]*)/$ ?page=$1 [L]
It makes URLs friendly from something like domain.com/?page=2 to domain.com/page/2/.
However, is there a way that I can edit this .htaccess file to make it redirect URLs containing ?page=2 (or any page number) to it's nicely formatted URL (/page/2/) automatically? My current one just allows the friendly URL version to "exist", but in no way enforces a redirect to it.
Also, is there a way I can redirect ?page=1 or /page/1/ just to the main directory/home?
EDIT:
After using what I received as an answer from Jon Lin, I was able to solve the second part of my question. Considering that ?page=1 automatically redirected to /page/1/, all I had to do was redirect /page/1/ to the homepage:
RewriteCond %{THE_REQUEST} \ /+DIRECTORY/(?:page|)/1/?
RewriteRule ^ /DIRECTORY/? [L,R]
Add this right below the RewriteEngine on line:
RewriteCond %{THE_REQUEST} \ /+(?:index\.php|)\?page=([0-9]+)
RewriteRule ^ /page/%1/? [L,R]
Related
To get straight to the point.
I have this URL: http://example.com/?open=encyclopedia&letter=s&term=storm and I want this shortcut URL - http://example.com/storm - to redirect to the first URL: http://example.com/?open=encyclopedia&letter=s&term=storm
I have around 1.000 encyclopedic terms and I want this redirection to work for each term entered. For Example: if a visitor enters http://example.com/Storm to automatically be redirected to the page here: http://example.com/?open=encyclopedia&letter=s&term=storm OR http://example.com/Dried_Plant to http://example.com/?open=encyclopedia&letter=d&term=dried+plant
I prefer some htaccess solution to this, if possible.
If not, give what you can give.
I have no example code for this, since I do not know where to start from.
I suggest you redirect it all to your page encyclopedia. And then consider with php what you can do with it (like finding the first letter or change with _ or others)
You can use:
RewriteEngine on
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ encyclopedia?term=%{REQUEST_URI} [NE,L]
With correct first letter (letter=), change the last line with:
RewriteRule ^(.) encyclopedia?letter=$1&term=%{REQUEST_URI} [NE,L]
#Croises answer is good if your link looks like following
mysite.com/?open=encyclopedia&letter=s&term=/storm
REQUEST_URI is adding a trailing slash. Your link don't have one:
mysite.com/?open=encyclopedia&letter=s&term=storm
I think this is what you are looking for
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .(.+)$
RewriteRule ^(.) ?open=encyclopedia&letter=$1&letter=%1 [NC,L]
I have came accross a problem that every .htaccess query I've tried wasn't worked out ! I have URLs like this:
http://www.example.com/index.php?x=product
And I want to change it to a user friendly URL like this:
http://www.example.com/product/
Or it can be:
http://www.example.com/product.php
I've tried this code below:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]
Now, it is redirecting perfectly, but this is not the problem. I've used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?x=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.
You don't need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.
To have it SEO friendly make sure its in this format
http://www.example.com/product/ not http://www.example.com/product.php
if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn't dynamic although it is to get better pagerank)
Insert this in your .htaccess
RewriteRule /(.*)/$ products.php?x=$1
the (.*) pulls the elements out and puts them in variables $1
Hi I have a website eg website.co.uk
I would like a htaccess file using the rewriterule to make the urls better
Current URL
Website.co.uk?campaign=holiday&affiliate=1
the parameters (holiday & no 1) will change, depending on where the inbound link comes from
sometimes there will only be 1 parameter which would be the campaign like below
Website.co.uk/holiday
sometimes there will be 2 parameters
Website.co.uk/holiday/100
I have get variables in my php to get the variables on an index.php page
$campagin = $_GET['campaign'];
$affiliate = $_GET['affilaite'];
The first one has only 1 parameter I.e their might not be an affiliate
Or the May be 2 parameters I.e a campaign and an affiliate
the page they are redirecting to is index.php
In my php it checks if the gets are set but just struggling with rewrite rule
I totally understand your concern, and there is this site, where you could randomly create your .htaccess file based on your requirements.
http://www.htaccessredirect.net/
Give it a try!
In the htaccess file in your document root:
RewriteEngine On
# for redirecting query string URLs to nicer path URLs
RewriteCond %{THE_REQUEST} \ /+(?:index\.php|)\?campaign=([^&]+)&affiliate=([0-9]+)
RewriteRule ^ /%1/%2? [L,R]
RewriteCond %{THE_REQUEST} \ /+(?:index\.php|)\?campaign=([^&]+)
RewriteRule ^ /%1? [L,R]
# internally rewrite path URLs back to query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([0-9]+)|)$ /index.php?campaign=$1&affiliate=$2 [L]
I got stuck at this point here, and I'm breaking my head trying to figure out why its not working!
This is want to get the url to look like
example.com/news/top/
from
example.com/index.php?view=news&task=top
but i need it that way that "task" can be something diffrent like
example.com/index.php?view=news&task=newest
example.com/index.php?view=news&task=help
example.com/index.php?view=news&task=write
....
current .htaccess looks like:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?view=$1&task=$2
If I visit www.example.com/news/ -> it only loads the index.php but does not pass ?view=news
If I visit www.example.com/news/top/ -> it works just fine.
But I need them both to work, adding a new line to .htaccess like :
RewriteRule ^news/ index.php?view=news
then visiting www.example.com/news/ works fine, but visiting www.example.com/news/top/ it will not be possible to get the value of task.
Please help me!
Thank you in advance.
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
# Redirect access to index.php
RewriteCond %{THE_REQUEST} \ /index\.php\?view=([^&]+)&task=([^&\ ]+)
RewriteRule ^ /%1/%2/? [L,R=301]
# rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?view=$1&task=$2 [L]
The first redirects requests like /index.php?view=news&task=foo to /news/foo/. Then the second rule internally rewrites requests like /news/foo/ back to /index.php?view=news&task=foo.
If all of your links already look like /news/foo/, then you won't need the first rule.
I'm no htaccess veteran, but something like this might work:
RewriteRule /news/(.*) /index.php?view=news&task=$1 [L]
This should rewrite all requests to /news/abc to index.php?task=news&view=abc. The backreference $1 stands for the first pattern in the RegEx that is surrounded by parentheses.
I have a website that I'm trying to change the URLs on. All of the URLs start with http://domain.com/?
For example, http://domain.com/?index
I just want to remove the question mark. I don't care if it appears in the address bar, I just want my users to be able to access the pages on the site without having to type the question marks.
So, if a user wants to access http://domain.com/?index, I want them to be able to access it by typing http://domain.com/index.
Is this possible using .htaccess?
I've searched around and tried a few different things for a few days now and still can't figure out a way to accomplish what I'm trying to do.
Any help is appreciated.
Thanks.
Try:
RewriteEngine On
# Match against the request instead of the URI
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^&\ ]+)&?([^\ ]*)
RewriteRule ^$ /%1?%2 [L,R=301]
This takes URI's like http://example.com/?path/to/file.txt and redirects the browser to http://example.com/path/to/file.txt. The browser will display that URL in the location bar instead. This is, of course, assuming that if someone actually goes to that URL, that there is something there to be served other than a 404.
EDIT
To internally map none-query string URLs to the one with a query string:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?$1 [L]
Try using :
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ /?$1 [NC]
in your .htaccess file ,
let me know if it works.