.htaccess url rewriting - php

I need a rewrite rule to rewrite the following:
www.abc.com/page/xyz to www.abc.com/xyz
remove **"page"** from url

I think it's more like this:
RewriteBase /
RewriteRule ^(.*)$ page/$1 [L]
If I understand what you're trying to do.

Like so:
RewriteBase /
RewriteRule ^page(/.*)?$ $1 [L]

If you only want to redirect page/xyz then you can use:
RewriteEngine on
RewriteRule ^/page/xyz$ /xyz [L]
If you want to redirect anything within the page directory, then use:
RewriteEngine on
RewriteRule ^/page(/.*$)$ $1 [L]

Related

redirecting a php url with 2 parameter

I am trying to set up my api to have 2 endpoints, and the simplest way I have seen to do this is to have a .htaccess file that would redirect something like www.website.com/api/category/platform to display the same as www.website.com/api/index.php?category=platform . The issue I am now running into is that I want it to include a second parameter such as www.website.com/api/index.php?category=platform&filter=price to be the same as www.website.com/api/category/platform/price or even www.website.com/api/platform/price
The current .htaccess file is
RewriteEngine On
RewriteRule ^/?category/([^/d]+)/?$ index.php?category=$1 [L,QSA]
I figured it out and I was able to use something along the lines of
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ project3/api/index.php?category=$1&filter=$2 [L]
You can use smthing like this.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?category$ $1category/ [R=301,L]
RewriteRule ^ - [L]
RewriteRule . index.php [L]

.htaccess redirection with query string on url

I have a website www.example.com/blog/abc. I need it to redirect to www.example.com/blog/a.php?a=abc
Below is my .htaccess file
RewriteEngine On
RewriteBase /
RewriteRule ^blog/(.*)$ blog/a.php?a=$1 [R=301,L]
What am i doing wrong ?
Try:
RewriteRule ^blog/(.*)/?$ blog/a.php?a=$1
Instead of:
RewriteRule ^blog/(.*)$ blog/a.php?a=$1

Php htaccess RewriteRule how to fix

i get the $_GET['id'] from url to show a page, to htaccess i have the follow code to show like this shop/123 and not shop.php?id=123
RewriteEngine on
RewriteRule shop/(.*)/ shop.php?id=$1
RewriteRule shop/(.*) shop.php?id=$1
My problem is .. If i add to url shop/123/test/test/test this still working again and i have problems with the share links etc.
How i can fix it to takes only one parametes else to go to 404?
Sorry for my English
You can use this :
RewriteEngine on
RewriteRule ^shop/([^/]+)$ /shop.php?id=$1 [L]
This will rewrite /shop/foobar to /shop.php?id=foobar, but will not rewrite /shop/foobar/ with a traling slash or extra path-info .
If id is only numerical, you can change the rewrite rule to this:
RewriteEngine on
RewriteRule shop/(\d+)/ shop.php?id=$1
RewriteRule shop/(\d+) shop.php?id=$1
If you also have alphabets in id:
RewriteEngine on
RewriteRule shop/([a-zA-Z\d]+)/ shop.php?id=$1
RewriteRule shop/([a-zA-Z\d]+) shop.php?id=$1
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
RewriteRule ^shop/([0-9]+)/(.*) shop.php?id=$1 [L]
RewriteRule ^shop/([0-9]+)/(.*)/ shop.php?id=$1 [L]
Hope this will work. Thanks.

What am I doing wrong with this .htaccess?

I have the following .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)/(.*)/(.*)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L,R=301]
When I access my website using http://www.domain.com/testpage/ it gives me the 404 Not Found error. What am I doing wrong?
PS: currently the index.php files just echo the pageLevel1, pageLevel2 and pageLevel3 values.
Remove the leading slash from Rewrite pattern
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/(.*)/(.*)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L,R=301]
First of, I don't know why are you using the 301 redirection at all? If you want to accept all characters in the url you just need to have a catch-all rewrite rule like this:
RewriteRule ^(.*)$ index.php?param=$1 [L]
If you want to catch the parameters like you defined, the rewrite rules can be like this:
RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([A-Za-z-]+)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L]
UPDATE:
The completed rules in your case (three page levels), with URL slugs which accepts letters and numbers can be like:
RewriteRule ^([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L]
This way you can access pages like:
www.domain.com/test
www.domain.com/test/another-test
www.domain.com/test/another-test/new-level

.htaccess short and friendly url

I'm looking for the right .htaccess rule to make a Facebook.com/user URL like
What I want if I access for example
www.domain.com/StackOverFlow
It will be rewrite to
www.domain.com/index.php?category=StackOverFlow
Or
www.domain.com/category.php?item=StackOverFlow
Any help? I've tried some question here, but they doesn't explicit do this behavior.
Thanks in advance
Edited:
Forgot to say that I only want to be redirected using this rule, anything that after the domain, doesn't have any extension
For example it should redirect www.domain.com/categoryDescription but no www.domain.com/categoryDescription.php or www.domain.com/categoryDescription.html
The example below takes the URI after .com/ and assigns that to $1.
You can do the same for "item"
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?category=$1 [L]
<IfModule mod_rewrite.c>
RewriteEngine on
# Rewrite URLs of the form 'x' to the form 'index.php?category=x'.
RewriteRule ^(.*)$ index.php?category=$1 [L,QSA]
</IfModule>
Try:
RewriteRule ^StackOverFlow$ index.php?category=StackOverFlow [NC,L]
or:
RewriteRule ^StackOverFlow$ category.php?item=StackOverFlow [NC,L]
In generally:
RewriteRule ^([^/]*)$ index.php?category=$1 [NC,L]
or:
RewriteRule ^([^/]*)$ category.php?item=$1 [NC,L]

Categories