create SEO friendly URL using rewriterule - php

I try to rewrite my SEO URLs to some real GET requests, to handle in my PHP file.
I want to have these 2 cases to work:
mysite.com/company-profile -> index.php?action=company-profile
mysite.com/faq/howcanijoin -> index.php?action=faq&anchor=howcanijoin
I got the first case to work using the rule:
RewriteRule ^([A-Za-z0-9-]+)$ index.php?action=$1
For the second I tried also. I put this rule before the previous one:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2
But it's not working. Any suggestions? If I understand correctly inside each parenthesis goes variables $1, $2 etc?

Do this
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)$ /index.php?action=$1&anchor=$2

This?
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2 [QSA,L]

Related

Redirect old dynamic url to a new pattern using htaccess

A normal link of my site may look like this
mydomain.com/categorylist/8/Special_Single_Songs.html
I'm planning to change the URL pattern to something like
mydomain.com/categorylist/8-Special-Single-Songs.html
How can I redirect the old URL pattern to the new one using .htaccess redirect rule?
My .htaccess Rewrite rule look like this
RewriteRule ^categorylist/([0-9]+)/([0-9a-z]+)/([0-9]+)/(.*)\.html$ /index.php?pid=$1&sort=$2&page=$3 [L]
Try this code
RewriteRule ^categorylist/([0-9]+)/([0-9a-z]+)/([0-9]+)/(.*).html$ /index.php?pid=$1&sort=$2&page=$3 [L]
RewriteRule ^categorylist/([0-9]+)\-([0-9a-z]+)/([0-9]+)/(.*).html$ /index.php?pid=$1&sort=$2&page=$3 [L]
try this & put it on top after RewriteEngine on
RewriteRule ^categorylist([^_]*)_([^_]*_.*).html$ $1-$2 [L,NE]
RewriteRule ^categorylist([^_]*)_([^_]*).html$ /$1-$2 [L,NE,R=301]
note : big number of underscores can be a problem

How to rewrite specific URL using htaccess

I've been breaking my head on this for quite some time and I don't see the solution.
I want to rewrite a URL with a GET language parameter to a more clean URL.
For instance:
http://www.example.com?lang=en
Needs to be:
http://www.example.com/en
The above works fine with this rewrite rule:
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L]
But I can't get it to work on URLs like these:
http://www.example.com/contact.php?lang=en
http://www.example.com/about.php?lang=en
That need to be:
http://www.example.com/en/contact.php
http://www.example.com/en/about.php
Anyone have an idea what I'm missing in my rewrite rule to make this work?
You will need an additional rewrite rule for handling /en/about.php:
RewriteEngine On
RewriteRule ^(en|nl|fr|de)/([\w-]+\.php)$ $2?lang=$1 [L,QSA]
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L,QSA]

ApiRest Beautify url with rewrite paramaters

I'm building a RestApi and I have a problem with urls. I want to transform "api.php?request=users" into "api/users". But I also want it to be dinamic. For example: If a user types "api/users/13" I want it to be tha same as "api.php?request=users&id=13". Is there any way to make it dinamic? Do I have to create every case individually? How could this be done in each of these two cases? Thanks so much in advance for your help!!
the best way is change .htaccess file like this:
RewriteEngine On
RewriteRule ^api/([^/])/([^/])/([^/]*)$ /api.php?request=$1&id=$2&state=$3 [L]
The original URL:
http://www.domain.com/api.php?request=mike&id=3&state=active
The rewritten URL:
http://www.domain.com/api/mike/3/active
update: if you don't know the number of parameter add one more rewrite for each 'level':
RewriteRule ^api/([^/])/([^/])/([^/])$ /api.php?request=$1 [L]
RewriteRule ^api/([^/])/([^/])/([^/])$ /api.php?request=$1&id=$2 [L]
RewriteRule ^api/([^/])/([^/])/([^/]*)$
/api.php?request=$1&id=$2&status=$3 [L]
Regards.
I think you must add a new RewriteRule, because you need different variables.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)/?$ $1.php?request=$2
RewriteRule ^(.*)/(.*)/(.*)/?$ $1.php?request=$2&id=$3

mod rewrite, htaccess and gettign the values

the incoming URL
domain.com/12/3
should be re-written to
domain.com/?w=12&h=3
htaccess
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?w=$1&h=$2 [QSA]
the php
<?php
echo $_GET['h'];
?>
Result
404 page
I've tried using htaccess to change the result of the url and then retrieve the value from the URL, could someone help me out?
Maybe you need to escape - like:
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_\-]+)$ index.php?w=$1&h=$2 [QSA]
PS. I hope the above URL /12/3 is just for example because your regex accepts only a-z
RewriteEngine On
RewriteRule ^[a-z]{2,2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Essentially, I'm pretty sure you need a / in front of index.php
I like to use this generator for my mod-rewrite rules.
http://www.generateit.net/mod-rewrite/
Try changing your rewriterule to the following:
RewriteEngine on
RewriteRule ^([a-z0-9]{2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Your example has two digits as the first parts of the path, which won't be matched by [a-z].
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?w=$1&h=$2 [QSA]
The above sorted it. Thanks for the replies

shorten url problem using htaccess

I need to change
http://localhost/engineering/management/administrator/modules/course/view.php
to
http://localhost/engineering/course_view.php
using htaccess
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)_(.*)\.php$ management/administrator/modules/$1/$2.php
RewriteRule ^(.*)_(.*)\.php$ management/user/modules/$1/$2.php
Works fine for this,
But for the below url its not working
http://localhost/engineering/management/user/modules/data/edit.php
Please try with this code:-
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)_(.*)\.php$ management/administrator/modules/$1/$2.php
RewriteRule ^(.*)_(.*)\.php$ management/user/modules/$1/$2.php
Hope it helps.
when you use this one!
RewriteRule (.*)_(.*)\.php management/administrator/modules/$1/$2.php
because it's possile ro rewrite other URLs(that you've mentioned), you can add special part to it to avoid such thing. e.g:
RewriteRule swd(.*)_(.*)\.php management/administrator/modules/$1/$2.php
and url:
http://localhost/engineering/swdcourse_view.php
and for second one, use other characters than swd!!
RewriteRule ^usr(.*)_(.*)\.php$ management/user/modules/$1/$2.php

Categories