how to rewrite a url with two variables using .htaccess - php

i want rewrite it with a .htaccess i have this url:
../index.php?page=details&id=123456
like this:
../detail/123456
but I do not really know how to do this. Could you help me to rewrite this url?
or give me a simple example that I can understand how it works

RewriteRule ^(.*)/([0-9]+)$ index.php?page=$1&id=$2&%{QUERY_STRING}
The first (.*) selects any string, e.g. "details". ([0-9]+) catches a number, in your case "123456"
Finally
&%{QUERY_STRING}
ensures, that additional parameters are added to your backendscript, too.

This should work:
RewriteEngine On
RewriteRule ^detail/([^/]*)$ /index.php?page=details&id=$1 [L]

Here is an example I use for my webpage when it's in a subdirectory.
# "learn/degree/index.php?d=mathematics" shows as "learn/degree/mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
To complete it, remember to write at the beginning of the page this:
RewriteBase /
RewriteEngine On
Here's the 'full' .htaccess I use in my page to give you some idea.
#Redirect any www. to the page without it. Helps to keep user logged.
RewriteBase /
RewriteEngine On
rewriteCond %{HTTP_HOST} ^www.newfutureuniversity.org [NC]
rewriteRule ^(.*)$ http://newfutureuniversity.org/$1 [R=301,L]
Options +Indexes
Options +FollowSymlinks
# Finally working. Rewrite the user so "/student/Username" will internally be "/student/?user=Username"
RewriteRule ^student/([a-zA-Z0-9_-]+)$ student/index.php?user=$1
# Rewrite the disciplines so "learn/degree/1" will internally be "learn/degree/index.php?dis=1"
RewriteRule ^learn/degree/([1-4])$ learn/degree/index.php?dis=$1
# Rewrite the degrees so "learn/degree/mathematics" will internally be "learn/degree/index.php?d=mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
# Rewrite the degrees so "learn/subject/2/5/7" will internally be "learn/subject/index.php?class=2&div=5&section=7"
RewriteRule ^learn/subject/([0-9])$ learn/subject/index.php?class=$1
RewriteRule ^learn/subject/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2
RewriteRule ^learn/subject/([0-9])/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2&section=$3
You have to make the links to ../detail/123456, then the script interpretes it internally as ../index.php?page=details&id=123456. If you don't want this, then you are looking for a redirect.

Try this
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
# RewriteBase / add this if necessery, commented intentionally
RewriteRule ^detail/([0-9]+)$ index.php?page=details&id=$2
</IfModule>

Related

Redirect URL as well as Change Query String Apache

I have two conditions:
http://localhost/restexample/api/con/2/
rewrites to
http://localhost/restexample/RestController.php?phone=single&id=2
and
http://localhost/restexample/api?number=12345
redirects/rewrites to
http://localhost/restexample/RestController.php?phone=all&no=12345
I am getting proper response in 1st case but not in second case.
My .htaccess file is:
# Turn rewrite engine on
Options +FollowSymlinks
RewriteEngine on
# map neat URL to internal URL
RewriteRule api/con/([0-9]+)/$ "RestController.php?phone=single&id=$1" [nc,qsa]
RewriteCond %{QUERY_STRING} number=(\d+)$
RewriteRule "^api" "RestController.php?phone=all&no=%1" [nc,qsa,R=301]
Somebody please help.
Output of second file:
Try these rules:
Options +FollowSymlinks
RewriteEngine on
# map neat URL to internal URL
RewriteRule ^api/con/([0-9]+)/?$ RestController.php?phone=single&id=$1 [NC,L,QSA]
RewriteCond %{QUERY_STRING} (?:^|&)number=(\d+)$
RewriteRule ^api/?$ RestController.php?phone=all&no=%1 [NC,L]

RewriteRule for all .php pages with constant querystring

I'm working on a project which all pages need to have a querystring assumed id. for example following pages:
example.com/alfa.php?id=1
example.com/beta.php?id=30
exapmle.com/gamma.php?id=50
I'm using the following .htaccess to make my URLs beautiful.
RewriteEngine On
## BEAUTIFUL URL
DirectoryIndex default.php
Options -Multiviews -Indexes +FollowSymLinks
#RewriteBase /
DirectorySlash off
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
This code will make the following convertion:
example.com/alfa.php?id=1 to example.com/alfa?id=1
Assuming that X.php is everytime changing (X) but id is always constant, What is the best content of .htaccess file?
I tried several methods, But what is the best?
and one thing more, How can I convert URL into example.com/alfa/1 AND example.com/beta/30
You're trying to match against the query string (all the stuff after ?) in the regex pattern of your rule. The query string isn't part of that match, only the URI. But since you don't want the query string to be touched, just don't match against it since it'll get appended automatically:
RewriteEngine On
## BEAUTIFUL URL
DirectoryIndex default.php
Options -Multiviews -Indexes +FollowSymLinks
#RewriteBase /
DirectorySlash off
# remove trailing slash
RewriteRule ^(.*)\/$ $1 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w/-]+)$ $1.php [L,T=application/x-httpd-php]
For example.com/alfa/1 and example.com/beta/30:
RewriteCond %{THE_REQUEST} [A-Z]{3,9}\ /([\w/-]+)(\.php)?\?id=([0-9]+)
RewriteRule ^ /%1/%3 [L,R=301]
RewriteRule ^([\w/-]+?)/([0-9]+)$ $1.php?id=$2 [L,QSA,T=application/x-httpd-php]
Something else you can try doing is getting rid of your rules and just using Multiviews, which you have turned off. Multiviews and mod_negotiation was made for stuff like this.

.htaccess url rewrite not formatting properly

Currently I working through my localhost in MAMP. I have read and research for the proper way to allow .htaccess file url rewrite and was successful. But now, the file is not formatting the links at all as desired. For example I have three pages index.php, about.php and contact. I am using MVC for the frame working of my site. Is this a problem with the code in the .htaccess file or my localserver?
Currently links look like this when going from one page to another:
localhost/mvc/index.php?
localhost/mvc/index.php?p=about
localhost/mvc/index.php?p=contact
.htaccess should format the links to look like this:
localhost/mvc/index/?
localhost/mvc/about/?
localhost/mvc/contact/?
.htaccess:
<IfModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
# Set the base to this directory:
RewriteBase /mvc/
# Redirect certain paths to index.php:
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
</IfModule>
Replace your existing .htaccess code fully with this code:
Options +FollowSymLinks -MultiViews
DirectoryIndex index.php index.html
# Turn mod_rewrite on
RewriteEngine On
# Set the base to this directory:
RewriteBase /mvc/
# Redirect /mvc/index.php?p=page to /mvc/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mvc/index\.php\?p=([^\s]+) [NC]
RewriteRule ^ %1/? [R=302,L]
# Redirect /mvc/index.php to /mvc/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mvc/index\.php\s [NC]
RewriteRule ^ /mvc/ [R=302,L]
# Internally forward certain /mvc/page/ to /mvc/index.php?p=page
RewriteRule ^(about|contact|this|that|search)/?$ /mvc/index.php?p=$1 [L,QSA,NC]
Replace
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
with :
RewriteRule ^(about|contact|this|that|search)/\?$ index.php?p=$1
I just escaped the ? with a slash because, otherwise, you don't do this, the question mark will be interpreted and will means that the slash before it is optinal.

Htaccess Redirect Rewrite Not working together

I want to remove the query strings from the urls using Htaccess, I used the following code for changing the urls, It did but after redirection to that url, I am getting 404 error.
and there is rewrite statement also if i use that only, then the new url works without 404 error, but the old urls doesnt get automatically redirected to new urls.
Here is the htaccess and url the I am modifying
Options FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/profile\.php$
RewriteCond %{QUERY_STRING} ^user_id=([0-9]*)$
RewriteRule ^(.*)$ http://www.meenmipage.com/user/%1? [R=302,L,NC]
Actual Url was:
http://www.meenmipage.com/profile.php?user_id=2
and modified was:
http://www.meenmipage.com/user/2
If i remove the above code and just use the rewrite statement as like this one:
RewriteRule ^user/([^/]*)$ /profile.php?user_id=$1 [NC,L]
Then the new modified url works and the old one also works
Please tell me what to do?
I think you need to remove $ at the end of this line:
RewriteCond %{REQUEST_URI} ^/profile\.php
because if the request URI has user_id=2 then this condition will not match
Try This !
-----------
Options -MultiViews
Options +FollowSymlinks
RewriteEngine on
rewritecond %{QUERY_STRING} ^user_id=([0-9]*)
rewritecond %{http_host} ^www.meenmipage.com [nc]
RewriteRule ^([0-9]+)$ //www.www.meenmipage.com?user_id=$1 [L,QSA]

Rewrite URLs for Specific Language via PHP & htaccess

I need some htaccess rules which rewrite URIs like this:
http://sub.domain.tld/en/about?a=1&b=2
to this:
http://sub.domain.tld/about.php?lang=en&a=1&b=2
or more simple:
http://sub.domain.tld/about.php?a=1&b=2&lang=en
No difference...
But the user should see the first URI not the converted one (shouldn't be redirected).
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+) [NC]
RewriteCond %{QUERY_STRING} ([^/]+) [NC]
RewriteRule .* %2.php?%3&lang=%1? [L]
Maps silently
http://sub.domain.tld/LangCode/FileName?key1=val1&key2=val2
With or without trailing slash. Always displayed in browser's address bar,
To:
http://sub.domain.tld/FileName.php?key1=val1&key2=val2&lang=LangCode
For permanent redirect, replace [L] with [R=301,L]
Here is the .htaccess code :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule en/about?a=(.*)&b=(.*)$ about.php?lang=en&a=$1&b=$2

Categories