Php url rewrite and redirect in htacess - php

I want to redirect my site to friendly.
I have my website pull data from database and adress is /post?id=1
and i want to change it to /post/1
I already wrote the code for rewrite but i cant make sense from google research how to redirect to /post/1.
my code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^post/([^/.]+)/?$ post.php?id=$1 [L]

Use:
Options +FollowSymLinks
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+post\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /post/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^post/([^/.]+)/?$ post.php?id=$1 [L,QSA,NC]

This one worked for me
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ post.php?id=$1 [NC]
if you goto url /post/check1/
the htaccess file internally call post?id=check1

Related

I want to redirect an SEO friendly URL to a non-SEO friendly but keep the URL SEO friendly [duplicate]

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome

PHP redirect to another page with dynamic url and get data [duplicate]

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome

apache - rewrite dynamic url into static url on addon domain is not working

I am working on seo of my website,after reading some articles i found out about seo friendly urls so i decided to change all dynamic url into static url.
for eg: most of the urls are like www.abc.com/final.php?id=1&name=name-of-file but i want to change the urls into static urls like www.a.com/name-of-file/1
I tried to edit .htaccess file but its not working.
my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteRule final/id/(.*)/name/(.*)/ final.php?id=$1&name=$2
Try it like this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\d]+)$ final.php?id=$2&name=$1 [QSA,NC,L]
You can use:
Options +FollowSymLinks
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+final\.php\?id=([^\s&]*)&name=([^\s&]*) [NC]
RewriteRule ^ /%2/%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/? final.php?id=$2&name=$1 [NC,L,QSA]

PHP rewrite url in htaccess have to point other url

I want to rewrite some url by using .htaccess rewrite
I have url something like this:
domain.com/index?/pid=10550
and want to rewrite users to
domain.com/index.php?pid=10550
So, what will happen is users will access that url in backend
I'm new to php and have read some blogs like this have no luck
Also, I have tried this
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^domain.com/index?/$ /domain.com/index.php? [L]
it says The page isn't redirecting properly
and some times shows me a directory
You can not match against querystring in RewriteRule's pattern. You will have to match against {THE_REQUEST} variable (a full request line sent by a client to the server) using a RewriteCond
RewriteEngine on
#Remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
#redirect /index.php/?pid=123 to /index.php?pid=123
RewriteCond %{THE_REQUEST} /index\.php/\?pid=([^\s]+) [NC]
RewriteRule ^ /index?pid=%1 [L,R]
This will redirect /index.php/?pid=123 to /index.php?pid=123 .

Send subdomain as post data

Having spent the past few hours looking for a solution, I thought I'd ask here.
I'm looking for a way to process links such as:
http://subdomain.domain.com/page/subpage/
so they are passed to the server as:
http://domain.com/index.php?subdomain=subdomain&page=page&subpage=subpage
which can be taken by index.php to display the correct page information.
I can get the page and subpage to work but have no clue how to get the subdomain. Note: if there is no subdomain, I would like that field to either be left blank or contain a default value.
Any assistance would be much appreciated. :)
Edit:
Just to clarify, I want the first URL to be what the user sees and can input into the address bar and the second URL to be the page that is loaded by the server when the user navigates to the first URL.
My .htaccess file
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
# Remove 'www'
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://resolutiongaming.com/$1/ [L,R=301]
# Subdomain redirect
RewriteCond %{HTTP_HOST} ^resolutiongaming.com$
RewriteRule ^webdev/$ http://webdev.resolutiongaming.com [R=301,L]
RewriteRule ^artwork/$ http://artwork.resolutiongaming.com [R=301,L]
RewriteRule ^music/$ http://music.resolutiongaming.com [R=301,L]
ErrorDocument 404 /error.php
You may try this in one .htacces file in root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^\.]+)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)/? [NC]
RewriteRule .* http://domain.com/index.php?subdomain=%1&page=%2&subpage=%3 [R=301,L]
It will redirect permanently:
http://subdomain.domain.com/page/subpage/ or http://www.subdomain.domain.com/page/subpage/
To:
http://domain.com/index.php?subdomain=subdomain&page=page&subpage=subpage
For the rule to work, the incoming URL scheme: /page/subpage/ must be kept.
To replace permanent redirection with silent mapping, remove R=301 from [R=301,L] or replace it with R for temporal redirection.

Categories