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.
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
I just started .htaccess so I do not know too much about it, I try to google it but failed so I post my question here, I am sorry if the question is silly for you. My question is.
I use this code in .htaccess page:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
when I enter this url:
http://localhost/learn/php/htaccess/page/number
it works fine. but I also want to add index.php (page name) in the url. I tried this:
http://localhost/learn/php/htaccess/index.php/page/number
AND
http://localhost/learn/php/htaccess/index.php?page/number
but failed. How can I do this?
I simply want to do that if I add another page then I am not sure but I have to use this code:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
RewriteRule ^(.*)/(.*)$ another.php?x=$1&y=$2
but how can I open the another.php page with this format?
thanks
Try adding a few conditions to your rules:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
As for routing to another page, you'll need to distinguish the difference between the two routes. For example, given a url:
http://localhost/learn/php/htaccess/foo/bar
where should it get routed to?
index.php?n=foo&p=bar
or
another.php?x=foo&y=bar
?
htaccess knows nothing about the content, only the URL and a regex pattern to match it, so unless you differentiate between the two, you can't route the same thing to two different scripts.
You can just make it easy like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?data=$1 [QSA]
in your index.php you put this code
$data = explode("/",$_GET['data']);
$page = $data[0];
$number = $data[1];
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]
I want make two slim app, one for api calling and another one for website users.
I put my main php file into 'src' directory, and the structure is as below:
/
---.htaccess
---/src(here are files)
-----.htaccess
-----index.php(Slim)
-----/Controllers
-----/Views
-----/Models
-----/api
-------.htaccess
-------index.php(Slim)
---/public
-----/js
-----/css
-----/fonts
i want to make my url from this:
http://localhost/project/src/api/foo
to
http://locahost/project/api/foo
I don't know how to write the first .htaccess(root)
i try to redirect to subdir like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^src/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ src/ [QSA,L]
but it always returns 404 Not found by Slim. i thought it did redirect to subdir but the slim router wont get correct url, am i right?
and others looks like this:(I just copy the documentation from SlimFramework)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA]
any ideas would help me a lot! thank you!
Try this rule in your /project/.htaccess:
RewriteEngine On
RewriteBase /project/
RewriteCond !/src/ [NC]
RewriteRule ^(.*)$ src/$1 [L]
Make sure this is your first rule.