in .htaccess I want to convert
from: website.com/xi/abc
to: website.com/x/?s=abc
The user types in website.com/xi/abc
and directory abc doesn't exist, and I just want a nicer looking url where abc is just a parameter, and becomes a parameter for index.html within directory x, parameter name is s, and value abc
The below doesn't quite work. I am trying to make the /abc be just abc and be passed on
RewriteEngine On
RewriteCond %{QUERY_STRING} ^xi/(.*)$
RewriteRule ^(.+)$ x/index.html?s=$1 [QSA,L,NE]
here is a second attempt which also doesn't work:
RewriteEngine On
RewriteCond "%{DOCUMENT_ROOT}/xi/(.*)" -f
RewriteRule "%{DOCUMENT_ROOT}/x/index.html?s=$1" [QSA,L,NE]
I swapped %{QUERY_STRING} for %{REQUEST_URI} in the RewriteCond in order to have the condition satisfied. Then, I prepended the RewriteRule matcher with xi/ in order to filter out that part:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/xi/(.*)$
RewriteRule ^xi/(.+)$ x/index.html?s=$1 [QSA,L,NE]
# http://website.com/xi/abc -> http://website.com/x/index.html?s=abc
# http://website.com/other/abc -> No rewrite
In this simple case you can even omit the RewriteCond:
RewriteEngine On
RewriteRule ^xi/(.+)$ x/index.html?s=$1 [QSA,L,NE]
Edit: I used this .htaccess tester.
Related
I'm aware this has already been asked, but I've tried countless solutions but I'm still not getting the desired result.
I've a page which shows a list of names and the variables data are the starting letter (variable l which can also be a number) and the page number (page_no) and the page number is optional as it should not be displayed in the page 1.
The original URL is the following
https://www.example.com/allnames.php?l=A&page_no=1
I've the following rule
RewriteCond %{QUERY_STRING} ^l=([\w]+)$
RewriteRule ^allnames.php$ /allnames/%1? [R=301,L]
which works fine for page 1, but it doesn't include the page_no variable, so I tried this one but I'm unable to understand what's wrong.
RewriteCond %{QUERY_STRING} ^l=([\w]+)?page_no=([0-9]+)$
RewriteRule ^allmovies.php$ /allmovies/%1/%2? [R=301,L]
the result expected would be, for example
allnames/A or allnames/A/1 for letter A, page 1
allnames/S/8 for letter S, page 8
And the following is the content of the htaccess file
RewriteEngine On
Options +FollowSymLinks -MultiViews
# to prevent loops
RewriteCond %{ENV:REDIRECT_STATUS} !200
#remove index.php
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/index.php [L]
# allnames.php
RewriteCond %{QUERY_STRING} ^l=([\w]+)$
RewriteRule ^allnames.php$ /allnames/%1? [R=301,L]
RewriteRule allnames/ /allnames.php?l=$1&page_no=$2 [L]
RewriteCond %{QUERY_STRING} ^l=([\w]+)?page_no=([0-9]+)$
The regex in the condition (CondPattern) does not allow for the & delimiter between URL parameters. You are also allowing the l parameter value to be entirely omitted (which would result in an ambiguous URL) or contain multiple characters (which you've stated should be a single "letter").
You would seem to need something like the following instead:
RewriteCond %{QUERY_STRING} ^l=([\w])&page_no=([0-9]+)$
But note that this will fail if the l parameter value contains more than one character (or is omitted entirely).
the result expected would be, for example
allnames/A or allnames/A/1 for letter A, page 1
Note that you would still need your first rule (without the page_no URL param) to get allnames/A. You could instead make page_no optional in the second rule, but you would still get a trailing slash (ie. allnames/A/) if that is an issue?
I have the following .htaccess file
RewriteEngine on
RewriteRule ^about$ about.html [NC]
RewriteRule (.*) index.php?url=$1 [L]
# Prevent loops
RewriteCond %{ENV:REDIRECT_STATUS} !200
# Map new URI to file
RewriteRule code/(.*) code.php?id=$1 [L]
first rule replace about with about.html - works
second rule
should have allowed me to write www.example.com/test-me - but when I var_dump $_GET["url"] I get: index.php while I expected $_GET["url"] to return test-me.
third rule:
not working - return 404 error. I expected file code.php on root to return with id as a URL param.
What I am trying to achieve is that when someone types www.example.com/lesson/lesson-name this will trigger the file code.php on the root and plant lesson-name as $_GET['lesson_name']
.
The second rule is triggering a the rewrite engine to start over and since you have no preceding condition (unlike the directive below) it ends up rewriting index.php on the second pass.
Your directives are also in the wrong order. The second rule that catches everything needs to go last. However, as stated, this rule catches everything - including all requests for static resources etc. So, you'll probably need additional conditions to prevent this.
Try the following instead:
Options -MultiViews
RewriteEngine on
RewriteRule ^about$ about.html [NC]
# Map "/lesson/<lesson-name>" to "/code.php?lesson_name=<lesson-name>"
RewriteRule ^lesson/(.*) code.php?lesson_name=$1 [L]
# Map everything else - for example "/index.php?url=test-me"
# Exclude static resources
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [L]
I'm trying to rewrite my URLs with unique slug like Instagram or Facebook.
Example: facebook.com/joe
My URLs are like that: website.com/user.php?username=joe
I try this rule in .htaccess:
RewriteEngine On
RewriteRule ^([a-z]+)$ user.php?username=$1 [L]
But it doesn't work, it redirects on /user.php.
It works if I use this rule:
RewriteEngine On
RewriteRule ^u/([^/]*)$ /user.php?username=$1 [L]
But result is website.com/u/joe and I prefer without u.
Any idea?
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ user.php?username=$1 [NC,L]
The .* in the pattern ^(.*)$ matches /anything and the parentheses help capture the anything part as a $1 variable used in the substitution URL as user.php?username=$1.
In case if you need multiple parameters you can simply add &%{QUERY_STRING} after $1 to separate and add them to the end of your query string.
for example : if you pass website.com/joe?age=31 the result will be website.com/user.php?username=joe&age=31.
Finally, the flag NC simply makes the rule non case-sensitive, so it matches /Joe or /JOE as well.
Hello I want to rewrite two values to static value on a link.
Sometimes link is like this: http://website.com/?s=page2
or like this: http://website.com/?m=page1&s=page2
I want to rewrite these links to these:
http://website.com/page2
http://website.com/page1/page2
You can use these 2 rules in your root .htaccess:
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# single parameter rule
RewriteRule ^([^/]+)/?$ ?s=$1 [L,QSA]
# two parameters rule
RewriteRule ^([^/]+)/([^/]+)/?$ ?m=$1&s=$2 [L,QSA]
How can I allow visitors to use this link (www.example.com/news?id=34) in order to see (www.example.com/index.php?page=news&id=34)
Right now I am hiding the extensions of my PHP files in order to make the links look nice. When visitors go to (www.example.com/news) they can see the page (www.example.com/index.php?page=news). However, when they go to (www.example.com/news?id=12) they get a 404 error.
At this point, PHP does not recognize the id parameter. Here is what I currently have in my .htaccess file
Options +FollowSymlinks
RewriteEngine on
# catch request with no querystring like /Home
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
# make other requests with a non-empty query string go to /index.php?id=XXXXX
RewriteCond %{QUERY_STRING} ^.*$
RewriteRule ^$ /index.php?id=$1 [QSA,L]
Your second test pattern (^$) only matches the case where the user doesn't put any path information in, but does include a query string (e.g. www.example.com/?id=12). Also note that the backreference $1 has no value in that rule either.
The simplest solution is to just combine the two rules, since you can use the QSA flag to do the work of appending the id=# part for you:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [QSA,L]
The condition of your first rule fails as the query is not empty. Try it without that condition but with the following condition instead:
RewriteCond $1 !=index.php
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]