Rewrite pages with a certain url to another htaccess - apache

What I need to do it rewrite a url from one thing to another eg:
http://www.domain.com/page_one/blah //to
http://www.domain.com/page_two/blah
I've tried a few script I've found around the internet but I'm terrible with .htaccess and can never understand or get it right.

If you want to change only this specific url, use this:
RewriteEngine On
RewriteRule ^page_one/blah?$ http://www.domain.com/page_two/bla [L]
If you want to change the url for every file into the "page_one"-folder, this will help you:
RewriteEngine On
RewriteRule ^page_one/([^/]+)$ http://www.domain.com/page_two/$1 [L]
The RewriteEngine On activates the RewriteEnigine, so that you are able to use RewriteRules.
The ([^/]+) in the second solution means "every file, but no folders (the slash is excluded)". This is stored in $1 and used to create the new url.
Edit
[L] stops the script from using the other rules (if you have some)

Related

Mod rewrite redirect every subdirectory to the same file

I have a website with a products.html file. Now, inside this file, I will have some Javascript code that checks the url to show the correct products/categories. This are some examples of the urls:
example.com/products
example.com/products/
example.com/products/shoes
example.com/products/shoes/
example.com/products/shoes/adidas
example.com/products/shoes/adidas/
example.com/products/shoes/adidas/adidas-predator-20.3
example.com/products/shoes/adidas/adidas-predator-20.3/
So basically, I want that everytime someone goes to the any of the above urls, I want to get the file products.html, I'll manage the rest with JS.
Also I want to be able to test this on my local machine. In this case, my URLs will be:
localhost/websites/myWebsite/products
localhost/websites/myWebsite/products/
localhost/websites/myWebsite/products/shoes
localhost/websites/myWebsite/products/shoes/
localhost/websites/myWebsite/products/shoes/adidas
localhost/websites/myWebsite/products/shoes/adidas/
localhost/websites/myWebsite/products/shoes/adidas/adidas-predator-20.3
localhost/websites/myWebsite/products/shoes/adidas/adidas-predator-20.3/
So I think we would need two htaccess files right?
You may use this single rule that will work on localhost and on production host as well:
RewriteEngine On
RewriteRule (?:^|/)products(?:/|$) products.html [L,NC]
With your shown samples could you please try following, you should place .htaccess file on same level where folder products is present. Also place the products.html in same level too.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/(?:products)/?((?:shoes)/?((?:adidas)/?((?:adidas-predator-20\.3)/?)?)?)?$ [NC]
RewriteRule ^(.*)$ products.html [L]
If these are the only rules you will have then change L to END as an additional note here.

mod_rewrite: hide real urls but keep available as different files

Possible this question has already been answered but I didn't find any answer after hours of searching.
I need to put the site under "maintenance mode" and redirect/rewrite all requests to site_down.html, but at the same time I need the site to be available if I enter the address like files are in a subfolder.
ex:
if I type http://example.com/login.php I need site_down.html to be displayed.
but if I specify http://example.com/test/login.php I need real login.php do be displayed.
I need this to be done with rewrite, so copying everything to another directory isn't a solution.
I tried a couple dozens of combinations, but I'm still unable to achieve what I need
This is one version of my .htaccess file ():
DirectoryIndex site_down.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^test\/(.*)$ $1 [S=1]
RewriteRule ^(.*\.php)$ site_down.html
RewriteRule .* - [L]
</IfModule>
This code should rewrite all requests with "test/*" to "parent folder" and skip next rewrite rule and then terminate rewriting at RewriteRule .* - [L]. If there is no "test/" in url - all request should be rewritten to site_down.html
What am I doing wrong?
Could you suggest any valid solutions, please?
Thank you.
Essentially, you are searching for 2 rules. One rule will translate a virtual subdirectory to the working files. The other rule will translate the url to the working files to a splash page. We just have to make sure that if the first rule matches, the second rule doesn't match. We can do this by making sure " /test/" (including that leading space) was not in THE_REQUEST (or the string that the client sent to the server to request a page; something in the form of GET /test/mypage.php?apes=bananas HTTP/1.1). THE_REQUEST doesn't change on a rewrite, which makes it perfect for that. Skipping a rule like you did usually doesn't have the effect you expect, because mod_rewrite makes multiple passes through .htaccess until the resulting url doesn't change anymore, or it hits a limit and throws an error. The first time it will skip the rule, but the second time it will not do that.
RewriteCond %{THE_REQUEST} !\ /test/
RewriteRule \.php site_down.html [L]
RewriteRule ^test/(.*)$ $1 [L]

htaccess page to page redirect and seo friendly urls

i have a problem with a htaccess files and i cannot figure it what is the problem.
The site has url rewriting for seo purposes in place so:
www.website.com/page/seo-friendly-url
is rewritten to
www.website.com/page.php?seo=seo-friendly-url
this is done with the following
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
Now the problem is that i have to redirect some pages that are already indexed by the search engines to their new destination as they are no more available, for example:
www.website.com/page/seo-friendly-url
has to be redirected to
www.website.com/page/another-seo-friendly-url
I have tried something like this but it is not working
Rewriterule ^page/seo-friendly-url$ page/another-seo-friendly-url [R,NC,L]
also this one is not working
Rewriterule ^page/seo-friendly-url$ page.php?seo=another-seo-friendly-url [R,NC,L]
This seems pretty stupid but i can't find the problem :-/
Thank you for your help
Ema
Edit, for anubhava:
Hi,
no i have already set the rewriting for that.
What i'm trying to achieve is redirect an already rewrited link.
Let me explain myself better:
At the moment i have this url that is indexed by Google (or any other search engine) in the form of a beautified url (seo friendly). The url has this form:
www.website.com/page/seo-friendly-url
I have already set a rule in the htaccess so the previous link is rewritten and goes to a php page with a query string that is used to display some content.
The page and the query are in this form:
www.website.com/page.php?seo=seo-friendly-url
So basically i'm using the last part of the first url as a query parameter for the second url.
This is achieved (and works) through the following code here below:
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
So far so good.
Now what i need to achieve is to redirect this url, that has been deleted:
www.website.com/page/seo-friendly-url
to go to a new page
www.website.com/page/another-seo-friendly-url
Of course the same rules applies to this new url (www.website.com/page/another-seo-friendly-url -->is already rewrited to--> www.website.com/page.php?seo=another-seo-friendly-url)
What do i need to do to do the reewriting right?
Thanks
You need this extra rule before your existing rule:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+page\.php\?seo=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
Rewriterule ^page/([\w-]+)$ page.php?seo=$1 [NC,L,QSA]
Just add redirects like this:
RewriteRule page/seo-friendly-url /page/new-url [R=301,L]
Important: this rules have to be above your existing rewrites because of the L flag in your rewrites
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l
Edit
You want to redirect the old URL to avoid duplicate content (rewrite=internal, redirect=HTTP 301)
Maybe you are open for solutions thinking in another direction.
I would try to handle this in the application, no through rewrites. Right now the GET parameter seo is handled in page.php. Isn't it an idea to extend this in that way one product can be identified through multiple seo aliases? If one product has to be taken off a similar one will then own this alias (simply a change of one row in the database).
As I don't know what software you are using this may be not possible.

Apache .htaccess Rewrite Query String with slashes

I am trying to figure out Apache's Mod Rewrite and so far it's not working. Here is what I am trying to do.
I have an index.php in the root directory of my site that is a template for all pages. I want to be able to organize my files in directories and the query string will have slashes.
So...
http://www.example.com/page.html
should be...
http://www.example.com/index.php?url=page
and...
http://www.example.com/directory/page.html
should be...
http://www.example.com/index.php?url=directory/page
I've got that working but I want to be able to go n directories deep...
http://www.example.com/directory1/directory2/page.html
should be...
http://www.example.com/index.php?url=directory1/directory2/page
I know I could just put a bunch of rewrites for however many directories I deep I want to go, but is there a single line RewriteRule that will put anything after the first / will be considered a query string?
This is what I have so far...
RewriteEngine on
RewriteRule ^([a-z-]+)\.html$ index.php?url=$1
RewriteRule ^faculty/([a-z-]+)\.html$ index.php?url=faculty/$1
Behind the scenes, PHP is including the file that is in the location that the url is specified.
This is bad idea to include file which name was passed from user. At least implement very STRONG validation, otherwise prepare to be hacked.
If you still want to use this -- here is the rule:
RewriteRule ^(.+)\.html$ index.php?url=$1 [QSA,L]
this should redirect anything with a ending .html to index.php?url=anything
www.example.com => index.php?url=
www.example.com/test1/test2 => index.php?url=test1/test2
RewriteRule ^([a-z-]+)\.html$ index.php?url=$1 [L,NC]

Apache Mod-Rewrite Question

I have a PHP scripted named index.php inside a folder named blog. There are three different views.
http://www.myDomain.com/blog/index.php
http://www.myDomain.com/blog/index.php?tags=list of categories
http://www.myDomain.com/blog/index.php?post=name of post
I would like to change the view based on the URL.
/blog redirects to number 1 above
/blog/name-of-category redirects to numbe 2 above
/blog/name-of-category/name-of-post redirects to number 3 above.
Right now I have the following mod_rewrite rules.
RewriteRule ^blog$ blog/index.php [L]
RewriteRule ^blog/(.+)/(.+)$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+)$ blog/index.php?tags=$1 [L]
This does not work, and I'm not sure why. Right now it always redirects to the last URL:
blog/index.php?tags=$1
And the GET data contains "index.php."
Also, if add a forward slash to the final rule like so:
RewriteRule ^blog/(.+)/$ blog/index.php?tags=$1 [L]
All redirects work fine. The problem is, I'm required to have a forward slash at the end of the URL if I want the category view.
Any ideas what's happening here? how I can fix this?
Thanks for the replies. I figured out that my problem was a side effect of having my scripts inside the folder named "blog". Here's what index.php looked like:
<?php
define ('BASE_PATH', "../blog/");
include_once(BASE_PATH . 'controller/Controller.php');
$controller = new Controller();
$controller->invoke();
See the problem? Because my script's base path was "blog", mod_rewrite was rewriting all my references inside the program. By renaming my script folder to blogScript, it fixed the problem.
In a regular expression, . matches any character (including a / character) so try doing ^blog/([^/]+)$ instead to match any character except a /.
You could write it as follows.
RewriteRule ^blog/?$ blog/index.php [L]
RewriteRule ^blog/(.+?)/(.+?)/?$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+?)/?$ blog/index.php?tags=$1 [L]