简体   繁体   中英

Create a rewrite rule in .htaccess for php file

I am very new to .htaccess, I am having some problem with file action. Example:

http://www.domain.com/upload_pic.php?action=save_pic

I wrote the .htaccess rule like

RewriteEngine On
RewriteRule ^sabc([a-zA-Z0-9!@#$-_]*)$ upload_pic.php?action=$1

I want the desired result like:

http://www.domain.com/sabc/save_pic

How can I get the desired result, please correct my .htaccess line.

Change it to:

RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1

The .+ will capture one or more characters after the / and be captured into $1

There are several other guides on the web already, but to understand it in better way as you are beginner @AMY , I have writen this for you. Hope this will work for you.

Most dynamic sites include variables in their URLs that tell the site what information to show the user. Typically, this gives URLs like the following, telling the relevant script on a site to load product number 7.

http://www.domain.com/upload_pic.php?pic_id=7

The problems with this kind of URL structure are that the URL is not at all memorable. It's difficult to read out over the phone (you'd be surprised how many people pass URLs this way). Search engines and users alike get no useful information about the content of a page from that URL. You can't tell from that URL that that page allows you to buy a Norwegian Blue Parrot (lovely plumage). It's a fairly standard URL - the sort you'd get by default from most CMSes. Compare that to this URL:

http://www.domain.com/sabc/7/

Clearly a much cleaner and shorter URL. It's much easier to remember, and vastly easier to read out. That said, it doesn't exactly tell anyone what it refers to. But we can do more:

http://www.domain.com/sabc/user-navnish/

Now we're getting somewhere. You can tell from the URL, even when it's taken out of context, what you're likely to find on that page. Search engines can split that URL into words (hyphens in URLs are treated as spaces by search engines, whereas underscores are not), and they can use that information to better determine the content of the page. It's an easy URL to remember and to pass to another person.

Unfortunately, the last URL cannot be easily understood by a server without some work on our part. When a request is made for that URL, the server needs to work out how to process that URL so that it knows what to send back to the user. URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand.

To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "upload_pic.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^sabc/?$    upload_pic.php    [NC,L]    # Handle requests for "sabc"

A couple of quick items to note - everything following a hash symbol in an .htaccess file is ignored as a comment, and I'd recommend you use comments liberally; and the "RewriteEngine" line should only be used once per .htaccess file (please note that I've not included this line from here onwards in code example).

The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:

  • RewriteRule - Tells Apache that this like refers to a single RewriteRule.

  • ^/sabc/?$ - The "pattern". The server will check the URL of every request to the site to see if this pattern matches. If it does, then Apache will swap the URL of the request for the "substitution" section that follows.

  • upload_pic.php - The "substitution". If the pattern above matches the request, Apache uses this URL instead of the requested URL.

  • [NC,L] - "Flags", that tell Apache how to apply the rule. In this case, we're using two flags. "NC", tells Apache that this rule should be case-insensitive, and "L" tells Apache not to process any more rules if this one is used.

# Handle requests for "sabc" - Comment explaining what the rule does (optional but recommended)

The rule above is a simple method for rewriting a single URL, and is the basis for almost all URL rewriting rules.

RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1

Hope this will be helpful for you to understand URL rewriting @AMY.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM