Rewrite URLs for Specific Language via PHP & htaccess - php

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

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]

.htaccess underscores to hyphens, but first parameter should not be replaced, it is dynamic parameter

I need to replace underscores to dashes. I have tried this code and tried to change some parameters here, but i'm not yet friedly with .htaccess syntax so i failed to do it myself:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule !\.(html|php)$ - [S=6]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=underscores:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscores:Yes]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) /$1 [R=301,L]
My previous .htacces code was this, it was working fine, but there was no need to replace underscores to dashes till now:
# Added for SME compatibility
Options +FollowSymLinks
# Ignore Indexation of apache
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules for ReRouting to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
I have URL like this: http://mydomain.tld/nl_BE/get_quote , the language (1st parameter) should NOT be replaced with dash, also language parameter can change, so i cannot hardcode language name in .htaccess file.
So i want to have URL something like this:
http://mydomain.tld/nl_BE/get-quote/some-random-title
http://mydomain.tld/nl_NL/about-us/some-random-text
Thanks in advance!
Rather than those multiple rules you can use this generic recursive rule:
Options +FollowSymLinks
RewriteEngine On
## RECURSION based rule
# if there is one underscore left then redirect
RewriteRule ^([a-z]{2}_[a-z]+)/([^_]*)_([^_]*)/?$ /$1/$2-$3 [NC,L,R=302]
# if there are more than one underscores then "repeatedly" replace it by -
RewriteRule ^([a-z]{2}_[a-z]+)/([^_]*)_(.*)$ $1/$2-$3 [L,NC]

URL Rewrite and Redirect rules

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]

How to rewrite subsites with parameter to html only

My old website has a url like "/index.php?s=subsite" and "/?s=subsite".
Now I added the following lines to ".htaccess" file, to make them shorter:
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\.(html)?$ index.php?s=$1&%{QUERY_STRING} [L]
Open a site with "/subsite.html" works fine, but also the old url "/index.php?s=subsite" and "/?s=subsite" works too. Is there a way, to allow "/subsite.html" only and redirect the old requests to it?
Replace your code with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?s=([^\s]+) [NC]
RewriteRule ^ /%1.html? [R=301,L]
RewriteRule ^([^.]+)\.html$ /index.php?s=$1 [L,NC,QSA]
Add this rule to your htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=([^& \]+)&?([^\ ]*)
RewriteRule ^ /%1.html?%2 [L,R=301]
This matches against the request, tries to capture the s query string parameter, then redirects the browser to that parameter followed by an ".html" and the rest of any query string that was there.

how to rewrite a url with two variables using .htaccess

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>

Categories