I have this .htaccess
RewriteEngine On
RewriteRule ^custom index.php?page=value [NC,L]
It works if index.php and .htaccess are located in the same folder.
I'd like to place the .htaccess file in the root and make the content placed in a newFolder react to the htaccess rules. Say:
.htaccess -> root level
index.php -> root/newFolder level
I did try:
RewriteEngine On
RewriteRule ^custom newFolder/index.php?page=value [NC,L]
No reaction at all (404) :(
I guess the solution must by simple, but...
Any help will be appreciated.
Let's make sure we're on the same page. ;) In my server root directory I have a .htaccess file and in that file I have:
RewriteEngine On
RewriteRule ^(.+/)?custom $1index.php?page=value [NC,L]
The RewriteRule has three components: a match pattern, a target, and a list of flags. The match pattern can contain plain text or a regular expression. The target can contain plain text or text with intermingled variables captured in the regular expression match pattern. Both match pattern and target are relative to the directory the .htaccess file is located in.
The match pattern is compared to the text in the URL that is after the current directory. If there's a successful match then the target is called from the directory the .htaccess file is in. If information is captured in the match pattern then the variables in the target are first interpolated in the target.
In this example the .htaccess file is in the server root therefore the match pattern is compared to the URL text after: http://localhost/
To test it out, here's the PHP code I put in an index.php file:
<?php
echo 'page value: ' .$_GET['page'] . '<br>';
echo 'script loaded: ' . $_SERVER['PHP_SELF'];
?>
When I navigate to http://localhost/custom in my browser: the regular expression match pattern is compared to the string "custom", there's a successful match so the target index.php?page=value is executed, and I get the index.php page in the root directory loaded. The displayed page looks like this:
page value: value
script loaded: /index.php
Next I create a new directory called "newFolder" in the server root and copy the index.php file to the newly created directory.
When I navigate to http://localhost/newFolder/custom in my browser: the regular expression match pattern is compared to the string "newFolder/custom", again there's a successful match, this time the target newFolder/index.php?page=value is executed, and I get the index.php page in the newFolder directory loaded. The displayed page now looks like this:
page value: value
script loaded: /newFolder/index.php
So, likewise, I can create any number of directories in the root directory and new index.php files in those directories, and when navigating to "custom" in those directories, this .htaccess file ought to load the corresponding index.php file in the current subdirectory.
~~~
I'm going to go out on a limb here and assume you want to load pages dynamically, therefore capture "custom" as a dynamic page name and dump it in the page field (replace "value") and send that to the index.php file in the current subdirectory.
RewriteEngine On
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.+/)?(.+)$ $1index.php?page=$2 [NC,L]
As mentioned, the parentheses capture dynamic text and dump it in the target string where the dollar sign numbers are. So the text captured in the first set of parentheses replaces the "$1" and the text captured in the second set replaces "$2". I also added the RewriteCond statement to prevent this rule from double-dipping. :) If I don't this rule gets executed for both the "custom" call AND the redirection to index.php.
So now when I navigate to http://localhost/newFolder/whoopdeedoo, the regex match pattern is compared to "newFolder/whoopdeedoo", there's a successful match, the target newFolder/index.php?page=whoopdeedoo is executed, and I get:
page value: whoopdeedoo
script loaded: /newFolder/index.php
~~~
At the risk of droning on, here's some background info. You mentioned this works in the root directory:
RewriteEngine On
RewriteRule ^custom index.php?page=value [NC,L]
But it does not work when navigating your browser to subdirectories. After reading the comments, it appears you discovered this next bit works for subdirectories, specifically "newFolder":
RewriteEngine On
RewriteRule ^newFolder/custom newFolder/index.php?page=value [NC,L]
But now it no longer works for the root directory. So you could simply include both:
RewriteEngine On
RewriteRule ^custom index.php?page=value [NC,L]
RewriteRule ^newFolder/custom newFolder/index.php?page=value [NC,L]
However there's a couple things here that jump out at me. One is that rather than hardcoding directory names in my root .htaccess I'd rather put .htaccess files in each subdirectory and take over the URL processing there.
But if for whatever reason you prefer to maintain one .htaccess file in the root directory, then, what jumps out at me is the duplication in the rules. So, alternatively, you can use regular expressions to make the subdirectory name a captured variable, like shown at the top of this answer:
RewriteEngine On
RewriteRule ^(.+/)?custom $1index.php?page=value [NC,L]
The regex capturing parentheses replace the text "newFolder/", and inside the parentheses we're saying, "capture one or more characters followed by a forward slash". Then, if there's a successful match, the value captured replaces the "$1" in the target. The reason this also works in the root is because the question mark following the parentheses makes the regex inside the parens optional. So if there's nothing before "custom" we're still good for a match. In that case the "$1" gets replaced with an empty string.
Try with this rewriterule:
# Activate RewriteEngine
RewriteEngine on
# Rewrite the URL requested by the user
# Entry: folder/clients/name/
# Output: folder/clients.php?id=name
RewriteRule ^folder/clients/(\w+)/?$ /folder/clients.php?id=$1
Explanation of this rewriterule:
First part of the rewriterule:
^ Top expression
folder/clients/ The requested URL string begins with folder/clients/
(\w+) Capture any letters that follow and stores it in $1
/? Optional backslash at the end of the URL
$ End of the expression
Second part of the rewriterule:
clients.php?id= Text string
$1 The first capture we saw in the first part
Related
I'm having trouble understanding the .htaccess file and the formatting; basically my question is how would I accomplish a link like this; /tag/hashtag/category
so I would have one index file in the tag directory which would grab the hashtag and category? and regardless of the input of the hash-tag and the category it would still take me to the index file in the tag directory.
Sorry for the real basic question/request.
Here is the content of your .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^tag/([^/]+)/([^/]+)$ /tag/index.php?hash=$1&cat=$2
([^/]+) means that we want to capture a string witch contains characters that are not /, so we get all characters between two slashes.
I am working on a website in which I write code for htaccess but the thing which I wanted to do is not happening. I have url which is:
http://www.example.com/demo.php?id=234&title=ask%20me%20a%20question
I converted to below url using htaccess:
http://www.example.com/234/ask%20me%a%question
htaccess code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([0-9]{4})/([a-z]+)/$ demo.php?url=$1&url2=$2
So. the problem is converted url is search for related file in subdirectory instead of server root i.e; public_html. I want to know how could this problem will solve.
Plz help me. Thanks.
The second parameter in your request requires that characters other than a-z be included, but you are limiting it to a-z.
In addition, you are requesting 234 in the URI, but checking for 4 numbers in the first parameter.
As such, change your rule to the following:
RewriteRule ^([0-9]{3,4})/([^/]+)/?$ demo.php?url=$1&url2=$2 [L]
Changes
Allow 3 or 4 numbers in the first parameter. If you want to be more flexible, you can change it to ([0-9]+).
Check for all characters other than / in the second parameter.
Make the trailing slash optional using /?.
Add the L flag to stop rewriting if the rule is matched (always good to have for when you add other rules).
I am trying to trap old URL's of the form:
http://www.example.com/mpn_engine.php%3Ffamilyname%3Djiyalal+goswami%26menuopt%3D2%26submenuopt%3D1%26Search%3Dstuff
In my .htaccess file, with the help of various wise StackOverflowers as RegEx is alien to me, I have arranged to catch the PHP script 'mpn_engine.php' (both .php3 and newer .php copies) wherever it might be found (in any sub folder) and redirect visitors to the index page.
RewriteRule (^|/)mpn_engine\.php$ /index.html? [L,NC,R=301]
RewriteRule (^|/)mpn_engine\.php3$ /index.html? [L,NC,R=301]
The odd thing I am finding is that the above seems to work providing I seek after the php files exactly, or if I supply conventional parameters of the form:
http://www.example.com/lang/mpn_engine.php?x=fred
but as soon as I substitute a percent mark for the question mark, i.e. something like the following:
http://www.example.com/lang/mpn_engine.php%x=fred
The Rewrite fails, & and I get unpredictable results, usualy a a 404 but occassionally a 'Bad Gateway'.
How can I rewrite this ReWriteRule to catch this .php file in any folder it might be looked for and with any trailing characters, including a percent sign, and redirect it gracefully to the index page?
Thanks!
Your question has a number of sub-questions:
If you want to "catch this .php file in any folder it might be
looked for" then as long as your .htaccess file is in the root folder of your website (and not in a subfolder), then you are covered.
If you want to cover ANY trailing character, then you can make one of two changes to your rewrite rule:
Remove the ending $:
RewriteRule (^|/)mpn_engine\.php /index.html? [L,NC,R=301]
or
Add a wildcard after "php":
RewriteRule (^|/)mpn_engine\.php(.*)$ /index.html? [L,NC,R=301]
In the first case, if the $ present, this tells Apache to ONLY match if "php" is at the end of the URL. In the second case, this tells Apache to match if "php" is followed by zero or more of any other characters at the end of the URL. In either case, you do not need your second rewrite rule concerning "php3" -- either of these above will match for those instances as well.
The reason your first example with the "%" worked but subsequent attempts gave 404 errors is because the server translates "%3F" to "?", and "?" has a special meaning for web servers and is essentially ignored by your regex matcher -- thus the server acts as if "php" is the final part of the URL, and the rewrite succeeds.
I have noticed that many websites use urls that end in
website.com/index.php?var="value"&var2="value2"
and I was wondering how I could make it so that instead of having that be the end of the URL have this instead:
website.com/value/value2
and then have it so that instead of searching for "/value/value2" inside of the servers root folder it would instead just open index.php and then inside the PHP coding have a function that would get what the URL is. Either as a string "/value/value2" or an array "value" "value2" it doesn't matter but just some way of getting those variables. This would be so that the URL could be cleaned up and easy to tell where you were in the website.
Also if there is a way of doing this would it be possible for style.php that is in the same folder as index.php (but has a PHP header setting it to output CSS) that would be called in the head of index.php using <link rel="stylesheet" type="text/css" url="style.php" /> or whatever the syntax for that is, to be able to obtain that same variable so that the css styling could be changed according to the URL.
You can use rewriting of urls in .htaccess file
Check this.
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /?var=$1&var2=$2 [L]
There are three parts to this:
RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3.
This part is a regex, and the rule will be run only if the URL matches this regex. In this case, it says - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. then again bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference.
Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored.
Refer Beginner's Guide to mod_rewrite.
Also tutorial for same.
You need to re write the URL.. if u are using apache you would have to add changes in the .htaccess file. Check this and this manual.
If using apache, enable mod_rewrite and use .htaccess
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
If using nginx, use nginx_rewrite_module http://nginx.org/ru/docs/http/ngx_http_rewrite_module.html
And inside your index.php parse $_SERVER['REQUEST_URI'] variable, it will contain requested url.
This can be achieved easily. Everything after the question mark are called $_GET variables. So you can call $_GET['var'] or $_GET['var2'] to get their values.
For example. I have the URL: http://www.example.com?username=username&password=password
Now i can take that url and make it so:
<?php
$user = $_GET['username'];
$pass = $_GET['password'];
$newUrl = 'http://www.example.com/' . $user . '/' . $pass;
echo 'Link text here';
?>
This results in a formatted url based on $_GET variables: http://www.example.com/username/password
I can't seem to get my .htaccess file to route the urls to my site correctly. I have a number of languages people can choose from wanting URL's like:
http://www.domain.com/en/
http://www.domain.com/en/contact
But I can't seem to get the page 'contact' working when writing a rule to get the 'en' variable.
RewriteRule /([^/]+)/([0-9]+)/ index.php?language=$1
I use that to grab the language code but how could I get the contact page to work?
EDIT:
Apparently I needed some QSA option but now the language get variable grabs contact as the variable with the en
RewriteRule ^(.*)$ index.php?language=$1 [QSA,L]
With this rule the site:
http://www.domain.com/en/contact
Returns:
en/contact
EDIT2
What I am trying to accomplish is the directory structure:
/
/contact
/about
Having these folders in the root but grabbing and ignoring the /en/ language variable. So I don't need a second variable for &page=contact, I need it to route into the directory folder.
Try combining your two expressions, although you need to modify the second group - [0-9]+ will only match numbers, not words like contact. Try this:
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?language=$1 [QSA,L]
The QSA option allows a query string to be appended to the clean URL, perhaps something like this:
http://www.domain.com/en/contact?to=support&subject=Hello
In response to your comment, this expression should do the trick:
RewriteRule ^([^/]*)/([^/]+)/?$ $2/index.php?language=$1 [QSA,L]
In the rewritten rule, $2 holds contact, for example, and $1 holds en. The former is used as the directory, and the latter as an argument in the query string.