mod_rewrite rewrite url using .htaccess - .htaccess

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).

Related

My htaccess passthrough rule redirects to the url instead

I'm trying to passthrough (not redirect!) an empty old page to its new location using an htaccess RewriteRule.
I essentially want the user to browse to mysite.com/page-old and to see that url in their browser but be delivered the content from mysite.com/page-new. The user should not be aware that the location changed.
RewriteEngine On
RewriteRule ^page-old/?$ /page-new [PT]
The actual result is that they are redirected to page-new instead.
I found the below on apache.org which seems to validate my code some, but this is giving me a 404 error.
Description:
Assume we have recently renamed the page foo.html to bar.html and now want to provide the old URL for backward compatibility. However, we want that users of the old URL even not recognize that the pages was renamed - that is, we don't want the address to change in their browser
https://httpd.apache.org/docs/trunk/rewrite/remapping.html
RewriteRule "^/foo\.html$" "/bar.html" [PT]
RewriteEngine On
RewriteRule ^example/my-stuff/$ /example/home/ [L,R=301]
check this answer as well
How to redirect a specific page using htaccess

Rewrite url in browser

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.

Is possible redirecting without change the url in .htaccess?

I have this url:
http://localhost/search/
This returns me this file:
http://localhost/search.html
Now I want the urls with this structure:
http://localhost/search/([^/]+)/([^/]+)/([^/]+)/?
will redirect me to the search.html file too. But without changing the url.
For example with this urls:
http://localhost/search/women/23/shoes/
http://localhost/search/
http://localhost/search/man/45/shirt/
would return the same file:
http://localhost/search.html
Note: the urls of man and women does not has any existing path in the server.
Any advice or help would be appreciated. If you need more info, let me know and I'll edit the post.
RewriteEngine on
RewriteRule ^search/ /search.html
Will just work fine. Unless you explicitly request an external redirect, a RewriteRule on the same domain will not do one, thus not changing the URL visible in the browser.
if you don't need the rest of url then you can use this
RewriteEngine On
RewriteRule ^search/(.*)$ /search.html [L]
if you need to other parameters of url then let me know
edited version, Niels Keurentjes has a point if you don't need the rest of url
RewriteEngine On
RewriteRule ^search /search.html

redirect rule for multiple pages

I have page not found erros in webmaster because the page/2/0 part of a url is due to smart paging module clean url feature (Drupal) now i uninstalled the smart paging module but these page not found errors are still there.
www.mysite.com/a/b/c/page/2/0,
www.mysite.com/a/d/e/page/3/0,
www.mysite.com/a/f/g/page/4/0,
www.mysite.com/a/h/i/page/5/0
and so on.
I want to redirect
www.mysite.com/a/b/c/page/2/0 to www.mysite.com/a/b/c
www.mysite.com/a/d/e/page/3/0, to www.mysite.com/a/d/e
www.mysite.com/a/f/g/page/4/0, to www.mysite.com/a/f/g
www.mysite.com/a/h/i/page/5/0 to www.mysite.com/a/h/i
with one redirect rule. How to do this
in short i want to remove the page/x/0 part from the url and redirect it to the remaining part of that url.
You want something like this:
RewriteEngine On
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/page/[0-9]+/0$ /$1/$2/$3 [L,R=301]
The literals here are after the first 3 path nodes: /anything/anything/anything/, then it must be followed by a page, then some numbers, then a zero. You can make it even more general, say after the "page", by changing the pattern to:
RewriteEngine On
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/page/ /$1/$2/$3 [L,R=301]

Using .htaccess to change what URL is being displayed, but without actually redirecting the content?

I have a RewriteRule setup to change
http://kn3rdmeister.com/blog/post.php?y=2012&m=07&d=04&id=4
into
http://kn3rdmeister.com/blog/2012/07/04/4.php
but that actually redirects where the browser is getting the page from. I want to still display
/blog/post.php?y=xxxx&m=xx&d=xx&id=xx
but have the browser show the simpler URL like
/blog/post/year/month/day/id.php
I read something somewhere about using ProxyPass, but I don't quite know what I'm doing :P
I want people to be able to visit either the post.php URL with the query strings, OR the clean URL with fancy shmancy subdirectories for the dates and get the same content — all while displaying the clean URL in the end.
You want something like this:
# First if someone actually requests a /blog/post.php URL, redirect them
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /blog/post\.php\?y=([0-9]{4})&m=([0-9]{2})&d=([0-9]{2})&id=([0-9]*)\ HTTP/
RewriteRule ^blog/post\.php$ /blog/%2/%3/%4/%5.php [R=301,L]
This will redirect the browser to the /blog/##/##/##/##.php URI, that will show up in their address bar. Once they get redirected, the browser will then send a request for /blog/##/##/##/##.php and your server then needs to internally rewrite it back:
# We got pretty URLs, but need to rewrite back to the php request
RewriteRule ^blog/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^\.]+)\.php$ /blog/post.php?y=$1&m=$2&d=$3&id=$4 [L]
This changes everything back internally so that the file /blog/post.php can handle the request.

Resources