right now my url looks like this:
http://domain.com/en/c/product%2C-product2%2C-product3/82
where last number is category numer.
And im trying to rewrite it and redirect user to url which should look this one:
http://domain.com/82/product-product2-product3
The clue is I want to hide "en/c/" part and clean url from commas and blank spaces. I'm completely green in rewriting.
Any ideas?
You can use these 2 rules in your root .htaccess for that:
RewriteEngine On
RewriteBase /
RewriteRule ^en/c/([^,\s]*)[,\s]+([^,\s]*)/(\d+)/?$ $3/$1$2 [NC,L,NE,R=302]
RewriteRule ^(en/c)/([^,\s]*)[,\s]+(.*)/(\d+)/?$ $1/$2$3/$4 [NC,L]
In order for this to work, we need to tell the server to internally redirect all requests for the URL "url1" to "url2.php". We want this to happen internally, because we don't want the URL in the browser's address bar to change.
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 "url2.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.
The .htaccess file is a configuration file for the server. If there are errors in the file, the server will display an error message (usually with an error code of "500").
If you are transferring the file to the server using FTP, you must make sure it is transferred using the ASCII mode, rather than BINARY. We use this file to perform 2 simple tasks in this instance - first, to tell Apache to turn on the rewrite engine, and second, to tell apache what rewriting rule we want it to use. We need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^url1/?$ url2.php [NC,L] # Handle requests for "url1"
Related
I'm running apache2 on Linux and I've not been able to find a solution to this.
I want to change the URL shown in the browser, but not change any real paths etc for files.
Eg:
http://127.0.0.1/school/northern/
http://127.0.0.1/school/southern/
I'd like the URL to appear as the following for both:
http://127.0.0.1/school/
But I don't want to change any paths to relating to northern or southern, they must remain as is.
I've tried adding a .htaccess in the northern folder as follows:
RewriteEngine On
RewriteRule ^(.*)/(.*) /$1 [L,R=301]
This forces a redirect back to http://127.0.0.1/school and the files aren't there so it fails.
Is this possible ?
Thanks
How do I convert this .htaccess file to an app.yaml file?
Here is the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!.*?public).* index.php [QSA,L]
I need to do this to run an app using PHP on Google App Engine.
The reason I'm asking this question, is because Google specifically recommends a code example in their official documentation that is stored in Git Hub called Dr Edit. The Dr Edit code example has a .htaccess file, but no app.yaml file. And, in the READ ME file, the very first step for setting up the application, is to create a Google App Engine application. So I guess Google has provided a code example that insinuates it will run on Google App Engine, but it won't.
Supposedly Google is monitoring Stack Overflow for issues related to GAE, so I hope they read this.
Here is information about how to simulate Apache mod_rewrite $_GET['q'] routing for a PHP App Engine app, in case that's helpful.
As the name .htaccess implies, it can control access to a directory. The .htaccess files are read on every request. I guess that means that every HTTP request to the domain name must go through the .htaccess file. So it seems like the .htaccess file is like a gatekeeper. From Wikipedia, it seems that the .htaccess file can be used to block certain IP address from loading a web page.
The first line of the .htaccess file is: RewriteEngine On
That line of code: RewriteEngine is an Apache Directive which turns the Apache Rewriting Engine on or off.
So that .htaccess file is looking at every request, taking the request, then pushing out a different request.
It seems that the app.yaml file can not directly take one URL request, and push out a different request, BUT the app.yaml file can take a URL request, and then run a PHP script, that will change the request.
So, in the app.yaml file, the section that takes the URL, then redirects to a script, would look like this:
Catch the incoming URL requests and cause the mod_rewrite.php file to run.
- url: /Put the incoming URL you want to monitor, catch and redirect here
script: mod_rewrite.php
Redirect URL requests with app.yaml
The second line of the .htaccess file is:
RewriteCond %{REQUEST_FILENAME} !-f
This is a test, looking for a match in the incoming URL request. The -f at the end is for:
-f (is regular file) Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.
Oh wait! There is an exclamation point in front of it. That's probably a test for logical not. So, if it's NOT a regular file?
Whatever that means. What is the difference between a regular file and everything else? What's an irregular file? lol idk. Anyway, it's looking for a file I guess.
From the Apache documentation, here is a quote:
Server-Variables: These are variables of the form %{ NAME_OF_VARIABLE
} where NAME_OF_VARIABLE can be a string taken from the following
list:
So that %{REQUEST_FILENAME} part of the second line is what Apache is calling a Server-Variable, and the specific Server-Variable is REQUEST_FILENAME.
Here is a quote from the Apache documentation:
REQUEST_FILENAME
The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time
REQUEST_FILENAME is referenced. Otherwise, such as when used in
virtual host context, the same value as REQUEST_URI. Depending on the
value of AcceptPathInfo, the server may have only used some leading
components of the REQUEST_URI to map the request to a file.
So I guess the .htaccess file is looking for something that looks like a path to files on your local computer?
Finally there is the Rewrite Rule Apache RewriteRule
What is the QSA at the end for? I guess it's called a flag, and:
Using the [QSA] flag causes the query strings to be combined
QSA seems to stand for Query String Append. If the incoming URL has a query string on the end of it, how do you want it dealt with?
The [L] prevents any further rewriting rules to be appended
Appache L rewrite flag
Appache Documentation QSA
The character (^) is called a caret. I don't know why it's used at the beginning. The syntax for the Appache Rewrite directive is:
RewriteRule pattern target [Flag1,Flag2,Flag3]
So the caret is part of the pattern being detected. That target is simply index.php. So it looks like if lots of different possible requests simply get routed back to the index.php file, which is the very first thing the application runs.
The asterisk at the end is probably a wildcard for any extension.
I think you're out of luck :-( Rewrite is an Apache module but AppEngine is based on Jetty, so you must redesign for that. You might be able to use one of the Bundled Servlets, Filters, and Handlers but none of them are a direct substitute for Apache Module mod_rewrite. Some people have used Apache as a front end before Jetty, but that is a clumsy approach. Sorry.
My main htaccess file does a bunch of things for my site to function correctly.
I have added redirects for pages that have moved. I don't have root access to the server and using .htaccess is my only option.
Is it possible to include separate files for the redirects in the .htaccess file so I can keep them separate and write programatically to the additional files that hold my redirects?
Basically I want to reference separate files from my .htaccess to manage rules dynamically and also neaten up one long .htaccess file with a few smaller files.
I also want to add redirect rules on the fly as things change on the site within my application.
You can use a RewriteMap http://httpd.apache.org/docs/2.3/rewrite/rewritemap.html
Let's say your map file looks like this and is called moved.map:-
/about profile
/page/that/has/moved new/location
You .htaccess would need something like this:-
RewriteMap moved txt:moved.map
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond ${moved:%1|NOT_PRESENT} !NOT_PRESENT [NC]
RewriteRule .? ${moved:%1} [NC,R=301]
This will redirect with a 301 status code http://your.domain.com/about to http://your.domain.com/profile and redirect http://your.domain.com/page/that/has/moved to http://your.domain.com/new/location
You can then programmatically create moved.map.
I hope that helps.
If you are using .htaccess files then don't bother with RewriteMap -- it only applies if you have root access to the server or vhost config, which is never the case when you purchase a shared service offering.
If you are constrained to use .htaccess files then you have two options:
The first is to do what some packages do and that is to get your application to rewrite the .htaccess file based on a rewrite map that you maintain within in it. The best way to do this is to have "bookends" in your .htaccess file e.g.
##++AUTOMATIC rewrite rules
<rules inserted by your app>
##--AUTOMATIC rewrite rules
And when an update occurs have your app read in the .htaccess, swap out the section between ##(++|--)AUTOMATIC rewrite rules, write it back to a temp file, then move the temp file to .htaccess (this makes the rewrtie-back atomic on *nix OSs).
The second which might work if you know some regexp regular pattern which covers the rewrites (this is often the case) then use a rule to map them to a redirector script which looks up the new target and itself issues a:
$server = $_SERVER['HTTP_HOST'];
header( "Location: http://$server/$newTarget?$parameters", TRUE, 301 );
Note the 301 redirect -- this means that client browsers should cache this and remember this in future.
This is a strange one...
A while back I managed to write a .htaccess redirect that worked so that the URL was read like: www.website.com/mt?page=index - and what the real URL of this page was www.website.com/PageParser.php?file=index.php
The problem has been that the FTP system of my webhost hides .htaccess files even though they are allowed and do operate - and so I have checked back on local copies I have of my .htaccess files and none of them have the code as to how this works - and I've forgotten how I did it!!
Essentially, I am using wildcards so that anything after mt?page= will actually be showing PageParser.php?file= but without having the PageParser.php showing within the URL (and this is the important bit, because the index.php on my site root is actually sent through PageParser.php first so that anything which shouldn't be there is wiped out before the end user sees it) - so how can .htaccess redirect/rewrite the URL so that any link to /mt?page= show the file located at /PageParser.php?file= without changing the URL the user sees?
RewriteEngine On
RewriteRule ^(.*)mt?page=(.*)$ $1PageParser.php?file=$2
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^mt$ /PageParser.php?file=%1.php [NC,L]
This rule will rewrite (internal redirect) request for /mt?page=hello to /PageParser.php?file=hello.php without changing URL in browser.
Your source URL example (www.website.com/mt?page=index) has index while target URL (www.website.com/PageParser.php?file=index.php) has index.php. The above rule will add .php to the page name value, so if you request /mt?page=hello.php it will be rewritten to /PageParser.php?file=hello.php.php.
If there is a typo in your URL example and page value should be passed as is, then remove .php bit from rewrite rule.
The rule will work fine even if some other parameters are present (e.g. /mt?page=hello&name=Pinky) but those extra parameters will not be passed to rewritten URL. If needed -- add QSA flag to rewrite rule.
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
P.S.
Better write no explanation (I knew it/I did it before .. but now I forgot how I did it) than having these "excuses". While it may be 100% true, it just does not sound that great.
I need some help with .htaccess. I've been using URL rewrites, but not conditions, and I'm pretty sure I need them seeing as I can't get this working.
The URL structure I need is -> forum/catnamehere (which shows the page called catnamehere)
and child structure -> forum/catnamehere/fornamehere (which shows the child page of the cat)
However, somehow I fail to do this, making me believe I need conditions.
You should be able to do this with two rules.
Put your .htaccess file in the forum directory, and add the following:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)(/?)$ complexForumPath.php?catName=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)(/?)$ complexForumPath.php?catName=$1&subPage=$2 [QSA]
The first rule will grab stuff like forum/abc123_
And the second rule will grab forum/abc123_/abc123_