Hi how do I rewrite URL in .htaccess?
localhost/app/view/login/editPass.php
to
localhost/editPass.php?
when I come from my index
localhost/index.php
I know I have to use, something like
RewriteEngine On
RewriteRule ^(.*)
So do I need to have the .htaccess file with my index or in the folder app?
The .htaccess should be on the parent directory. RewriteRule ^/editPass.php?$ app/view/login/editPass.php
It must have the RewriteEngine On On top first. 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.
^/editPass.php?$
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.
app/view/login/editPass.php
The "substitution". If the pattern above matches the request, Apache uses this URL instead of the requested URL.
Related
Migrating a site from IIS to Apache and I'm having trouble with a RewriteRule I can't find the answer to.
I need to redirect from www.domain.com/pubs/books/P123.asp to www.domain.com/books.php?p=P123.
No matter what I try, I'm getting a 404.
Can someone give me a pointer? All the examples I seem to find have the dynamic part of the URL at the end. Do I first need to strip the .asp?
Drupal is also running on this site so there is some RewriteRules there to.
The latest I have tried is: RewriteRule /pubs/books/P(.*)$.asp /books.php?p=$1
First of all: Read the documentation! You can find it here.
It says:
Syntax: RewriteRule Pattern Substitution [flags]
and
In VirtualHost context, The Pattern will initially be matched against
the part of the URL after the hostname and port, and before the query
string (e.g. "/app1/index.html").
In Directory and htaccess context,
the Pattern will initially be matched against the filesystem path,
after removing the prefix that led the server to the current
RewriteRule (e.g. "app1/index.html" or "index.html" depending on where
the directives are defined).
If you wish to match against the
hostname, port, or query string, use a RewriteCond with the
%{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables
respectively.
(in other words: A pattern starting with a slash will never match if in .htaccess context)
First of all, make sure mod_rewrite is enabled and the rewriteengine is on (put RewriteEngine on in your .htaccess) and allowed there (see docs).
Assuming this is in a .htaccess in your www-root, this should work:
RewriteRule ^pubs/books/P(.*)\.asp$ books.php?p=$1 [L]
The syntax ^...$ makes sure that this is the entire filesystem path. We wouldn't want to match http://example.com/some/more/paths/pubs/books/P123.asp here and rewrite that.
Maybe I just don't know how to ask the question to find the answer but what I want to do is an .htaccess redirect from an old URl, shop.nilandsplace.com/camp to nilandsplace.com/store/camping. Not to make it more difficult, but I would like to know what it is that I am doing so I can learn this
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^shop.nilandsplace.com$
RewriteRule ^camp(.*)$ http://nilandsplace.com/store/camping$1 [R=301,L]
The top line turns on the rewrite engine. We need to do this before rewriting any URLs.
The second line checks that we're visiting the shop.nilandsplace.com domain.
The third line redirects any requests from /camp to http://nilandsplace.com/store/camping. The bit in brackets at the end of the third line tells Apache to make this a 301 redirect (so search engines know the page has moved), and to do the redirect straight away, bypassing any upcoming rewrite rules for this request.
The code will also redirect subdirectories of camp (anything after camp), for example shop.nilandsplace.com/camp/tent-poles > nilandsplace.com/store/camping/tent-poles.
You can learn more about the mod_rewrite module (which powers the rewrite engine) on the Apache Docs.
I have searched on google and here for a tutorial to help me rewrite my urls.
I would like somebody to explain what I must write and why.
I have this url:
http://iescup.eu/tournaments.php?tourney[id]=1
http://iescup.eu/tournaments.php?tourney[id]=2
http://iescup.eu/tournaments.php?tourney[id]=3
and so on
I would like to have this url:
http://iescup.eu/#!/tourneys/1
http://iescup.eu/#!/tourneys/2
http://iescup.eu/#!/tourneys/3
and so on
Sincerely
Rune Naundrup Dahl
So you type http://iescup.eu/tournaments.php?tourney[id]=1 in your browser's URL address bar. The request /tournaments.php?tourney[id]=1 gets sent to the server iescup.eu. On that server, these rules in the htaccess file in the document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^tourney\[id\]=([0-9]+)$ [NC]
RewriteRule ^/?tournaments.php$ /#!/tourneys/%1? [L,R=301,NE]
The %{QUERY_STRING} variable is matched against and the numeric ID is grouped and backreferenced by %1. The rule's target has a ? at the end to remove the query string and the NE flag is used so the # doesn't get encoded.
The rule redirects the browser to http://iescup.eu/#!/tourneys/1 thus changing the URL address bar. The browser then sends another request to iescup.eu, /. Note that the #!/tourneys/1 fragment is never sent to the server. Fragments are client side only and is used to determine how content should be dealt with (also used by javascript).
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.
Problem
I need to redirect some short convenience URLs to longer actual URLs. The site in question uses a set of subdomains to identify a set of development or live versions.
I would like the URL to which certain requests are redirected to include the HTTP_HOST such that I don't have to create a custom .htaccess file for each host.
Host-specific Example (snipped from .htaccess file)
Redirect /terms http://support.dev01.example.com/articles/terms/
This example works fine for the development version running at dev01.example.com. If I use the same line in the main .htaccess file for the development version running under dev02.example.com I'd end up being redirected to the wrong place.
Ideal rule (not sure of the correct syntax)
Redirect /terms http://support.{HTTP_HOST}/articles/terms/
This rule does not work and merely serves as an example of what I'd like to achieve. I could then use the exact same rule under many different hosts and get the correct result.
Answers?
Can this be done with mod_alias or does it require the more complex mod_rewrite?
How can this be achieved using mod_alias or mod_rewrite? I'd prefer a mod_alias solution if possible.
Clarifications
I'm not staying on the same server. I'd like:
http://example.com/terms/ -> http://support.example.com/articles/terms/
https://secure.example.com/terms/ -> http://support.example.com/articles/terms/
http://dev.example.com/terms/ -> http://support.dev.example.com/articles/terms/
https://secure.dev.example.com/terms/ -> http://support.dev.example.com/articles/terms/
I'd like to be able to use the same rule in the .htaccess file on both example.com and dev.example.com. In this situation I'd need to be able to refer to the HTTP_HOST as a variable rather than specifying it literally in the URL to which requests are redirected.
I'll investigate the HTTP_HOST parameter as suggested but was hoping for a working example.
It's strange that nobody has done the actual working answer (lol):
RewriteCond %{HTTP_HOST} support\.(([^\.]+))\.example\.com
RewriteRule ^/terms http://support.%1/article/terms [NC,QSA,R]
To help you doing the job faster, my favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
You use this tool when you want to check the URL and see if they're valid or not.
I think you'll want to capture the HTTP_HOST value and then use that in the rewrite rule:
RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/terms http://support.%1/article/terms [NC,R=302]
If I understand your question right, you want a 301 redirect (tell browser to go to other URL).
If my solution is not the correct one for you, try this tool: http://www.htaccessredirect.net/index.php and figure out what works for you.
//301 Redirect Entire Directory
RedirectMatch 301 /terms(.*) /articles/terms/$1
//Change default directory page
DirectoryIndex
According to this cheatsheet ( http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/png/ ) this should work
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1
Note that i don't have a way to test this so this should be taken as a pointer in the right direction as opposed to an explicit answer.
If you are staying on the same server then putting this in your .htaccess will work regardless of the server:
RedirectMatch 301 ^/terms$ /articles/terms/
Produces:
http://example.com/terms -> http://example.com/articles/terms
or:
http://test.example.com/terms -> http://test.example.com/articles/terms
Obviously you'll need to adjust the REGEX matching and the like to make sure it copes with what you are going to throw at it. Same goes for the 301, you might want a 302 if you don't want browsers to cache the redirect.
If you want:
http://example.com/terms -> http://server02.example.com/articles/terms
Then you'll need to use the HTTP_HOST parameter.
You don't need to include this information. Just provide a URI relative to the root.
Redirect temp /terms /articles/terms/
This is explained in the mod_alias documentation:
The new URL should be an absolute URL beginning with a scheme and hostname, but a URL-path beginning with a slash may also be used, in which case the scheme and hostname of the current server will be added.
It sounds like what you really need is just an alias?
Alias /terms /www/public/articles/terms/