About RewriteRule and .htaccess - php

I have a problem:
I want to rewrite this:
example.com/pages/?page=start
or
example.com/pages/?page=news
to:
example.com/start
or
example.com/news
So I want the GET from the subpage to be displayed as a main page, does anyone know how?

If you want that the user enter something like example.com/start or example.com/news but the server should process it like example.com/pages/?page=start or example.com/pages/?page=news this is done as follows:
RewriteEngine On
RewriteRule ^/?([^/]+) /pages/?page=$1 [QSA,L]
Be careful here because example.com/news/first-news will also be rewritten to example.com/pages/?page=news and /first-news will be lost!

Related

.htaccess create user friendly url and ridirect to it

i tryed to search everywhere for this problem but i didnt found nothing.
I want to make make a url seo friendly so i used this code:
RewriteEngine on
RewriteRule ^Homepage index.php [NC,L]
Then i want to redirect to it so i tryed to write this code:
RewriteRule ^index.php$ http://localhost/siti/socialmark/Homepage [R=301,L]
The error it's a loop of redirections, can someone help me?
SORRY FOR MY BAD ENGLISH!
The rewrite rules don't just make the URL string look different, it actually directs the user to the file at the end of the path even if you don't see it in the address bar. If Homepage is a directory containing index.php, even if that php file name doesn't appear in the URL, then it's causing a loop because it's directing you to a directory with an index.php.
The rule is executed every time that page loads. So, you're redirecting to a page which runs the redirect script, so it runs the rule to redirect again, and that causes the loop. What you want to do is create a condition that says "Don't run this code if the requested page is http://localhost/siti/socialmark/Homepage"
Something like this (you may have to adjust it)
RewriteBase /
RewriteCond %{REQUEST_URI} !=/siti/socialmark/Homepage
RewriteRule ^Homepage index.php [NC,L]
For more details, see the caveats and example here:
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_l

how to use htacces rewrite code for 3 diffrent files

i trying to make 3 diffrent files diffrent url i use currently below code whis is work perfect for me.
RewriteRule ^([a-zA-Z0-9-/]+).html$ file.php?file=$1
and i feel i need more urls for 2 other files and i change this code as below but this not work it change url fine but it not redirect me to contact.php its redirect to file.php as above code work.
RewriteRule ^([a-zA-Z0-9-/]+).html$ contact.php?contact=$1
so i need to help for make 3 files diffrent urls.
How is apache supposed to tell the difference between a request that's supposed to go to file.php or a request that's supposed to go to contact.php? They look completely identical. You'll have to preface it with something unique, like:
RewriteRule ^file/([a-zA-Z0-9-/]+).html$ file.php?file=$1 [L]
RewriteRule ^contact/([a-zA-Z0-9-/]+).html$ contact.php?contact=$1 [L]
So the URLs will look like:
http://example.com/file/foo.html
http://example.com/comtact/bar.html

.htaccess rule does rewrite URL in address bar

I have a website where my links look like this
http://www.domain.com/index.php?lang=English&inc=canyoning
I managed to write rewriteRule like this:
RewriteRule (German|English)\/(.*) http://www.domain.com/index.php?lang=$1&inc=$2 [NC,R]
Now my links look like this: <a href="http://www.domain.com/English/canyoning">...
This works, but I can see non user friendly URL in browser address bar. How can I tell browser to use link like /English/canyoning in URL and not index.php?lang=English&inc=canyoning?
And second: I would like to use forms on page.
There is no difference, whether I use form method =GET or POST, No variables come to destination site.
I guess there is my rewriteRule wrong. How to fix those issues?
Thank you for help!
This is because you are doing a redirect not a rewrite.. (The R flag indicates a Redirect)
So remove the R flag should fix your issue.
You may also need to remove the hardcoded domain. As you are doing a rewrite you cant rewrite to a different domain.
ie. Change
RewriteRule (German|English)\/(.*) http://www.domain.com/index.php?lang=$1&inc=$2 [NC,R]
To
RewriteRule (German|English)\/(.*) /index.php?lang=$1&inc=$2 [NC]

how to redirect a link to another link using htaccess?

i have a link like:
www.website.com/index.php?id=123456
and i want to redirect to:
www.website.com/test.php
the problem is that the id can be different each time, could be id=123456 or id=632456
any ideas on how to solve this?
thanks
edit:
what if i want to redirect it to www.website.com/test.php?id=123456 or whatever that id is?
Something like this:
RewriteRule index\.php.* test.php
Or you can be more specific:
RewriteRule index\.php\?id=[0-9]+$ test.php
Or you can even send the id too:
RewriteRule index\.php\?id=([0-9]+)$ test.php?id=$1

mod rewrite problem with 2 parameters

I'm trying to rewrite the categoy file of my shop system, I also integrated a pagination so I need to rewrite 2 parameters. it almost works, otherwise I wouldn't be posting in here
this is the rewriteurl
RewriteRule ^shop/cat/(.*)/page/([0-9]+)$ /cmstut/shop/cat.php?cat=$1&page=$2 [QSA,L]
This is the url
http://localhost/cmstut/shop/cat/32/page/2
the cat works but not the page and when I print the querystring I get this:
cat=32/page/2
What did I do wrong? I was expecting something like cat=32&page=2 so I could catch the page and show the right page for the pagination.
You’re probably having two rules where the second one looks like this:
RewriteRule ^shop/cat/(.*)$ /cmstut/shop/cat.php?cat=$1 [QSA,L]
This rule will cause that a request of shop/cat/32/page/2 will be rewritten wrong. You need to use a more specific pattern like this:
RewriteRule ^shop/cat/([^/]+)$ shop/cat.php?cat=$1 [QSA,L]
RewriteRule ^shop/cat/([^/]+)/page/([0-9]+)$ shop/cat.php?cat=$1&page=$2 [QSA,L]

Categories