.htaccess RewriteRule won't work! - php

I'm using php switch[_get] in my menu system to create url.com/?p=page and I'd like that to change into url.com/page.html. But I can't make it work, maybe some of you know the right settings for this.
I'm currently using this as .htaccess:
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^start(.*)\.html$ ?p=start$
Thank you!
Update:
I tried with the $1 but still the url is: ?p=start when I want it to be /start.html

Looks like you're missing the $1 in your last rule:
RewriteRule ^start(.*)\.html$ ?p=start$1
^^^^
EDIT After new information, try:
RewriteCond %{QUERY_STRING} p=([a-z0-9]+) [NC]
RewriteRule . /%1.html [L]
This captures the p= parameter from the querystring and uses it to rewrite to page.html

Try
RewriteRule ^start(.*)\.html$ ?p=start$1 [L]
See the one (1) at the end.

htaccess works the other way around.
It makes things like /start.html proxy to ?p=start however, you still have make the links themselves target /start.html.
So change all the <a href="?p=start"> to <a href="/start.html">.

you need
RewriteCond %{QUERY_STRING} ^p=(.+)$ [NC]
RewriteRule ^$ /%1.html? [R=301,L]
R=301 is to change the url in the browser, but
you still need to update all links on the site:
<a href="?p=start"> to <a href="/start.html">
Edit: Try the updated one (it has a ? after html)

Related

Redirect one page to another with parameters via htaccess

I want to redirect from a page to another with parameters with htaccess.
For example:
From: www.websitename.co.uk/mylink.php?u=*username*
To: www.websitename.co.uk/mylink/*username*
I tried this but it's not working.
RewriteEngine on
RewriteRule www.websitename.co.uk/mylink.php?u=*username* www.websitename.co.uk/mylink/*username* [R=301]
Where am I doing wrong?
Working fine for me , hope this will work fine.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/mylink\.php$
RewriteCond %{QUERY_STRING} u=([a-zA-Z]+)
RewriteRule .* http://websitename.co.uk/mylink/%1? [R=301]
Rewrite rules use regex like this:
RewriteEngine on
RewriteRule ^/mylink.php?u=(.*) /mylink/$1 [R=301]
Reference: https://httpd.apache.org/docs/current/rewrite/intro.html

.htaccess rewrite to parent when parameter is given

I have a problem with a rewriterule in my .htaccess, I would like to redirect like this:
www.mywebsite.com/category?p=2
to
www.mywebsite.com/category
where category is variable.
I have my rewrite engine on and other redirects are working fine. This is what what I got so far:
RewriteRule ^/([a-z_\/]*)\?? /$1 [R=301,L]
What is the best way to accomplish this?
Your request looks strange, but anyway, this should do it:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=
RewriteRule /?([a-zA-Z0-9]+)/? /$1? [R=301,L]
Use this:
RewriteEngine On
RewriteRule ^category$ /category?p=2 [L]
It will give you the following URL:
www.mywebsite.com/category

url rewrite and redirect - php

I recently developed a web site using php which accepts a id as its input via get.
http://website.com/profile.php?id=123. its the old fashion way, I've seen sites use pretty url's like http://website.com/username.html
So in order to archive this, i managed to do some research and end up with this code blow.
Also i'm passing the username in the query string.
http://website.com/profile.php?id=123&username=test_user
the parameter ID is only used as a input for the profile.php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /profile.php [NC]
RewriteCond %{QUERY_STRING} ^id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%2.html [R=301,L]
The final output i seek is http://website.com/username.html
Is their anything wrong in this ? this doesn't seem to work. Am i doing something wrong ? if so please be kind to let me know where my error lies. Thank you.
Try this rule:
RewriteEngine On
RewriteRule ^profile/([^/\.]+)/([^/\.]+).html/?$ profile.php?id=$1&username=$2 [L]
then you should be able to use
http://website.com/profile/123/username.html
as the URL. You can ignore the profile part but I don't recomment it because it would clash with other URLs of your website.
If you only need the username, use
RewriteRule ^profile/([^/\.]+).html/?$ profile.php?username=$2 [L]
and the URL would be
http://website.com/profile/username.html
You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /profile.php\?id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^ /%1/%2.html? [R=301,L]
RewriteRule ^([0-9]+)/([^/.]+)\.html$ profile.php?id=$1&username=$2 [L,QSA,NC]

RewriteRule not redirecting

Due to request, every page needs to be prefixed with "index.php"; if the path was first /it, now it needs to be /index.php/it.
I have tried changing base url in settings.php. But it doesnot works.Tried rewriting in .htaccess also. I have tried the following code
RewriteBase /
RewriteCond %{HTTP_HOST} ^http://www.example.com/index.php [NC]
RewriteRule ^(.*)$ http://www.example.com/index.php [L,R=301]
But it doesnot works. Somebody help me please. Thanks in advance..
Make sure you have mod_rewrite enabled and try with this
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://www.example.com/index.php/$1 [L,R=301]
Remember that the $1 refers to the (.*) you have captured in the URL
You are not using your capture group (.*). You can refer to it using $1
RewriteRule ^(.*)$ http://www.example.com/index.php/$1 [L,R=301]

mod_rewrite: rewrite existing files to friendly links

I went through a bunch of websites and tutorials yet can't find a solution.
Following snippet works and http://example.com/page/pot return a pot.php content
RewriteEngine on
RewriteRule ^page/([^/]*)$ $1.php?page=$1 [L]
I can't get it to work the other way around
RewriteEngine on
RewriteRule ^([^/]*)/page$ $1.php?page=$1 [L]
Your current approach will cause infinite looping since Apache re-injects rewritten URI back for further rule processing.
You need to use THE_REQUEST variable for that like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page/[^.]+\.php\?page=([^\s&]+) [NC]
RewriteRule ^ page/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^page/([^/]*)/?$ $1.php?page=$1 [L,QSA,NC]
Try to use:
RewriteEngine on
RewriteRule ^([^/]+)/page$ $1.php?page=$1 [L]

Categories