Htaccess Rewriting part of url - php

I have this adress :
localhost/wattathome/view?slug=multiples-services
And I would like to rewrite it like that :
localhost/wattathome/view/multiples-services
or even better :
localhost/wattathome/multiples-services
But everything I try with Rewriterule ends up with a 404 error.
Please help me D:
Thanks in advance.

If i understnad correctly your code
RewriteRule ^view/([A-Za-z0-9-]+)/?$ view.php?slug=$1 [NC,L]
will get view?slug=multiples-services and rewrite it to
localhost/wattathome/view/multiples-services/ (note the end slash)
Try removing the ending slash and see what will happen
EDIT 1
Ok les try this one (remember, view.php must be in root folder):
RewriteRule ^view/([^/]+)$ view.php?slug=$1 [NC,L]
Make sure that you have all your redirection rules above any rewrite rule.
So, your htaccess should look like:
- options
- redirections
- rewritting

Related

htaccess rewrite rule for url varible as parent folder

I already create a .htaccess in my folder and would like to make the URL:
www.example/good/page
toward:
www.example/page?type=good
my current document is written as:
RewriteRule ^(\w+)$\/page page.php?type=$1 [NC,L]
but it didn't work, and I don't know how to check it is correct or not
would anyone able to provide some example for that?
thanks!
Make sure your rewite engine is on, and it is rewriting the base. Change your code like the following:
RewriteEngine on
RewriteBase /
RewriteRule ^/?([^/]+)/page?$ "page.php?type=$1" [L,QSA]
And then your rewrite rule must look like this.
The RewriteRule basically means that if the request is done that matches ^/?([^/]+)/page?$ (matches any URL except the server root), it will be rewritten as page.php?type=$1 which means a request for page.php be rewritten as page.php?type=good).
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite (example.com/good/page?id=2 will be rewritten as page.php?type=good&id=2.
L means if the rule matches, don't process any more RewriteRules below this one.
Try this and let me know. Accept the answer if it worked for you.

URL structure change using .htaccess rule

I have no knowledge in regex and never done regex redirections.
htaccess
but... I am looking for a way to redirect this URL structure:
http://www.example.com/mypage/param1/
to this URL structure
http://www.example.com/mypage/?key1=param1
and, if possible, only if param1 contains # or %40
I saw this answer:
How to change URL structure with htaccess?
but that's the reveresed direction for my question
RewriteEngine on
RewriteCond $1 #
RewriteRule ^mypage/((?!index).+)$ /mypage/?key=$1 [NC,L,R]
Remove the R flag if you dont want the url to change.
This will redirect an url of the form :
/mypage/.*#.*
to
/mypage/?key=.*#.*

Rewrite rule is not working in apache

I am new to url rewriting, and saw many tutorials for url rewriting like,
url-rewriting-for-beginners
I have used the following code in .htaccess,
RewriteEngine On
RewriteRule ^/raipur/([0-9])$ /viewRestaurant.php?id=$1 [NC,L]
and testing on this page urlRewritetest.php.
code of urlRewritetest.php
click
but its not working, I am using ipage's server, and also when i try to see the log using RewriteLog, the page through Internal Server Error,
Can anyone please suggest my mistake, thank you
What if you remove the first slash in your RewriteRule? You may also want to add "+" after the digits to match more than one digit.
RewriteEngine On
RewriteRule ^raipur/([0-9]+)$ /viewRestaurant.php?id=$1 [NC,L]

.htaccess writerule to remove question marks and equal mark

today i tried to create .htaccess file that replacing ? and = with / ,
test.php code:
<?php
echo $_GET['myparam'];
?>
.htaccess:
i used this writerule:
RewriteEngine On
RewriteBase /
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
so navigated to: www.web.com/test/myparam/123 (Manually) and the php script is worked,
i changed the myparam value to abc instead of 123 : www.web.com/test/myparam/abc
and then it redirects to 404 not found page...(the server don't know that abc is not directory when integer works when string 404)
!so what i want to do:!
www.web.com/ test .php? inttParam = 1 & strrParam = stringhere & p = 1
TO
www.web.com/ test/inttParam/1/strrParam/stringhere/p/1
and when i use $_GET['p'] it will work.
i changed the myparam value to abc instead of 123 and it didn't work
Well of course it won't work since your rule is matching only numbers in the end:
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
change your rule to this to make it work with anything:
RewriteRule ^test/myparam/([^/]+)/?$ test.php?myparam=$1 [NC,QSA,L]
To make it recursion based generic rule to convert /test/inttParam/1/strrParam/stringhere/p/2 to /test.php?p=2&strrParam=stringhere&inttParam=1`:
RewriteRule "^(test)(?:\.php)?/([^/]+)/([^/]*)(/.*)?$" /$1.php$4?$2=$3 [NC,L,QSA]
RewriteEngine on
# do not affect files
RewriteCond %{REQUEST_URI} !(\..{2,4})$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ index.php?parameter1=$1&%1 [L]
Something like that perhaps?
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
I don't know if that means that you also want to make a redirection (presumably 301) from the one with GET params to the one with slashes.
If so, I would recommend to use the previous answer from anubhava and handle any 301 redirection with PHP. Actually, you can redirect with "RewriteRule" using the "R" flag, but I admit that I woundn't know how to solve this particular case.
Anyway, I think there is no need, unless old URLs with GET params were working well at SEO and you didn't wan't to lose ranking.

.htaccess RewriteRule for Flat Links

I am pretty new to using the RewriteRule, so I am likely missing something obvious, but I have a PHP script that takes URL variables like this:
{baseurl}properties.php?prop=Property-Name
I would like to create RewriteRules so that anyone who types in this script name/variable combo would have their URL rewritten to:
{baseurl}/properties/Property-Name
As well as ensuring that anyone who types in the flat-link url, actually calls the script with the right variable name and value.
I have been referring to this link and I have found related threads:
Mod_rewrite flat links
Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work
But, I am obviously doing something wrong, as I cannot get this URL to work the way I want. I am currently using the following code, which appears to do nothing (aside from rewriting the URL to include the www, and redirect requests for index.php to the site root):
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^baseurl.com$ [NC]
RewriteRule ^(.*)$ http://www.baseurl.com/$1 [R=301,L]
RewriteRule ^index.php / [R=301,L]
RewriteRule ^properties/([0-9A-Za-z]+)/$ /properties.php?prop=$1
The issue is clearly with the last RewriteRule, assuming nothing above is affecting it. Again, I am likely doing something ridiculous. Can someone please explain what I am doing wrong?
Thanks for your help.
At a quick glance, it appears that you forgot to include the dash in your regular expression and you included trailing slash. Use this instead:
RewriteRule ^properties/([0-9A-Za-z-]+)$ /properties.php?prop=$1
If you look at your rule ^properties/([0-9A-Za-z]+)/$ you see that it needs to end with a forward slash. You can either remove that or make it optional like ^properties/([0-9A-Za-z]+)/?$.

Categories