i need help in URL rewriting of my own. My URL structure is looks like
http://domain.com/products-details.php?id=14
What i want it to be something like http://domain.com/products/14/
What i have tried so far is this in my .htaccess file
ErrorDocument 404 /myproject/notfound.php
Options +FollowSymlinks
RewriteEngine on
RewriteBase /myproject/
RewriteRule ^products/([^/]+)$ products-detail.php?id=$1 [QSA]
The above rule works fine without the trailing slash at the end, but i do want the slash at the end.
Further more:
It would be great if the non-rewrite request gets redirected to the rewrite ones. Thank you:)
You need a new rule for redirect:
ErrorDocument 404 /myproject/notfound.php
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /myproject/
RewriteCond %{THE_REQUEST} /products-detail\.php\?id=([^\s&]+) [NC]
RewriteRule ^ products/%1? [R=302,L,NE]
RewriteRule ^products/([^/]+)/?$ products-detail.php?id=$1 [NC,L,QSA]
Related
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]
I have problem using .htaccess to rewrite some of the friendly URL.
The root URL is http://www.example.com/my/
The working call url is http://www.example.com/my/Post.php?id=2
Both .htaccess file and Post.php are located in the sub-directory http://www.example.com/my/
But I wanted it to be like the following:
http://www.example.com/my/job/kuantan/?id=2
The .htaccess configuration as below:
DirectoryIndex index.php
Options All -Indexes
#ErrorDocument 404 /404.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^job/kuantan/([0-9]+) /Post.php?id=$1 [L,QSA, NC]
BUT, once i deployed the above .htaccess configuration file, it prompted 500 internal error message as well.
Is something missing or wrong in the .htaccess configuration? Please advice and really appreciated if someone there could give a hand. Thanks.
You should just need a single rule:
RewriteRule ^/my/job/kuantan/$ /my/Post.php
The query string is carried over by default.
Assuming that .htaccess and Post.php are located in the sub-directory /my/:
DirectoryIndex index.php
Options All -Indexes
#ErrorDocument 404 /404.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^job/kuantan/([0-9]+)$ Post.php?id=$1 [L,QSA,NC]
Remove all spaces between []. Change /Post.php?id=1 to Post.php?id=$1.
You can have this code in /my/.htaccess:
DirectoryIndex index.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /my/
RewriteCond %{THE_REQUEST} /Post\.php [NC]
RewriteRule ^ job/kuantan [R=302,L,NE]
RewriteRule ^job/kuantan/?$ Post.php [L,NC]
Query String is automatically carried over so there is no need to capture it in rule.
Target is to create friendly URL, and I got stuck here.
My link below:
Turpināt lasīt..
My .htaccess
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^blog/([0-9]+)/?$ blog.php?id=$1
No problems in configuration, simple rewrite rules work. What's wrong here?
Thanks
Ok try this code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+blog\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /blog/%1? [R=301,L]
RewriteRule ^blog/([0-9]+)/?$ /blog.php?id=$1 [L,QSA]
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
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§ion=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§ion=$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>