How to edit this url with mod_rewrite - apache

I have a godaddy linux server and I am wanting to edit my url's
Here is an example of 3 url's from my website
www.website.com/b.php?n=30&t=big
www.website.com/b.php?n=20&t=medium
www.website.com/b.php?n=10&t=small
I would like to be able to change them to
www.website.com/30/big
www.website.com/20/medium
www.website.com/10/small
MY IMAGE CODE
echo '<img src="gifs/' . $_GET["t"] . '/' . $_GET["n"] . '.gif">';

You can change them by changing all the links on your site from the /b.php?n=30&t=big style links to the /30/big style links. Then you can put these rules in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/(.*) /b.php?n=$1&b=$2 [L,QSA]
this will change the URI's back to the ones that route through b.php.
In the event that you have old URL's floating around the internet and they need to be changed to the new ones, you can use these in the same htaccess files:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /b\.php\?n=([^&]+)&t=([^&\ ]+)
RewriteRule ^/?b\.php$ /%1/%2 [L,R=301]
This will redirect the browser (or google index bot) to point permanently to the new URLs.

Related

LiteSpeed (or apache) rewrite to hide SUBFOLDERS domain.com and www.domain.com from mirror.com

I've mirrored a webpage with httrack (wget doesn't have multi connection)
Problem is this page has resources in two domains at the same time:
domain.com
www.domain.com
So, my scenario is root folder /var/www/mirror/ with subfolders /var/www/mirror/domain.com and /var/www/mirror/www.domain.com/
When you load the mirrored page's index in mirror.com, the url you see is https://mirror.com/domain.com/ but also you're redirected to https://mirror.com/www.domain.com/ as soon as you click in any content (see postdata at the end)
I've managed to hide one of the subfolders when you load the index in /var/www/mirror/index.html (going to mirror.com) with this code:
RewriteCond %{REQUEST_URI} !^/domain.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain.com/$1
RewriteRule ^(/)?$ domain.com/index.html [L]
But when I add the same for the second subdomain (www.):
RewriteCond %{REQUEST_URI} !^/domain.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain.com/$1
RewriteRule ^(/)?$ domain.com/index.html [L]
RewriteRule ^(.*)$ /www.domain.com/$1
RewriteRule ^(/)?$ www.domain.com/index.html [L]
the page breaks (error loading stylesheets, resources, etc).
I've tried leaving [L] only at the end, with domain.com in the last RewriteRule but also with www.domain.com in the last RewriteRule, nothing works.
PS: In the real page, only index works with domain.com, as soon as you click in any link, you're redirected forever to browse in www.domain.com, but I would love to have my mirror as domain.com always, even when my mirrored resources are linking to /var/www/mirror/www.domain.com subfolder, if that's possible.
Edit to add some examples:
When I load mirror.com, I want index to be mirror.com/ and nothing else (this works ok with my first .htaccess example)
When I click in any link, mirrored content send me to mirror.com/www.domain.com/someContent.html, but I want to see in browser's url mirror.com/someContent.html
If I load some real subfolder, ie. mirror.com/www.domain.com/tags/someContent.html, I want to see in the url mirror.com/tags/someContent.html
Apache mod_rewite can not see the content of the page i.e. it can not alter URL links contained within the page. You could try use Apache mod_proxy_html which can modify URL links contained within the page. See below for further info.
http://apache.webthing.com/mod_proxy_html/

How to redirect any URL to my index.html using apache?

I want my website to point at my index.html no matter what the URL is.
http://localhost/WebsiteName/
http://localhost/WebsiteName/whatever
http://localhost/WebsiteName/whatever/whatever-1/whatever-2/whatever-3/etc
By using this rewrite code in my apache config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]
My URL works correctly when URL is any of these:
http://localhost/WebsiteName/
http://localhost/WebsiteName/whatever
But breaks when it is like this or further extended:
http://localhost/WebsiteName/whatever/
http://localhost/WebsiteName/whatever/whatever-1/whatever-2/whatever-3/etc
It acts as if there was another folder "whatever" in the directory whenever I use one of the URLs that break.
I don't want the URL to change, I just want it to point at my index.html no matter what it is.
Based off what you have there, all of those url's should go to your index.html. The index.html is assumed to be at the DOCUMENT_ROOT.
I'm not sure what your problem is, but maybe instead of having a condition for rewriting non-files. Stop rewriting when it's a file. I notice any changes on my website, with either config, but maybe it can work for yours.
RewriteEngine on
### If a uri leads to an actual file or directory (presumably with it's own index.html), then don't rewrite any further
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
### everything else
RewriteRule . /index.html [L]

mod_rewrite directory heirarchy, rules specific to rewritten URLs?

I have directories and subdirectories similar to below:
Each file and folder here is preceded by localhost/dir/
/common/style.css
/common/js.js
/common/top.php
/common/bottom.php
/images/items/1.png
/images/items/2.png
/images/items/3.png
/index.php
/items.php
/specificitem.php
On every page in the default directory, everything in the common folder is included (each page includes top.php and bottom.php, top.php includes js.js and style.css). specificitem.php relies on $_GET to grab data to populate that page, and is directed to from items.php.
In my .htaccess file, I rewrite the URLs for specificitem.php like the below:
RewriteEngine on
RewriteBase /dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule items/john specificitem.php?id=1 [NC,L]
RewriteRule items/jane specificitem.php?id=2 [NC,L]
RewriteRule items/jill specificitem.php?id=3 [NC,L]
...
When directing to these pages, no links work, styling and Javascript are broken/not loaded. Links are in the form ./somelink.php, and when on one of the item pages (eg. localhost/dir/items/john), ./somelink.php tries to direct to localhost/dir/items/somelink.php when this page doesn't exist. Stylesheets and Javascript are also attempted to be found at localhost/dir/items/common/ when likewise this doesn't exist. Changing the links to /somelink.php instead tries to go to /localhost/somelink.php when this doesn't exist.
What have I done wrong in my .htaccess file, or what is missing such that I can apply link rules to rewritten links only? My links, extra files, images are loaded properly for any page without a rewritten URL.
First have your rules like this:
RewriteEngine on
RewriteBase /dir/
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^items/john/?$ specificitem.php?id=1 [NC,L,QSA]
RewriteRule ^items/jane/?$ specificitem.php?id=2 [NC,L,QSA]
RewriteRule ^items/jill/?$ specificitem.php?id=3 [NC,L,QSA]
Then to fix links/css/js/images paths you need to add this just below <head> tag of your page's HTML:
<base href="/dir/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

.htaccess redirect a clean image url

I am trying to do clean URLs for images, where the url also contains a description of the image (for seo purposes). The only important part of the file name is the start and the extension.
I am trying to get like image URLs such as:
mywebsite.com/seo-images/123-some-description.jpg
to do a clean url rewrite where the image is actually located at
mywebsite.com/uploads/123.jpg
file types can be .jpg or .png or .gif etc.
I also have thumbnails:
mywebsite.com/seo-images/t123-some-description.jpg
to do also do a clean url rewrite in the same way and place
mywebsite.com/uploads/t123.jpg
I have tried putting the following .htaccess file in the seo-images directory
RewriteEngine On
RewriteBase /seo-images/
# match all numbers upto - then get the extension after .
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([_0-9a-zA-Z-]+/)?-([_0-9a-zA-Z-]+/)?([_0-9a-zA-Z-]+/)\.$ /uploads/$1.$3 [R=301,L]
so far, no good.
I have a .htaccess file in the root that does other clean mod_rewrites.
The "some-description" can be one word or many words separated by - so basically everything after the - and before the . should be disregarded and the new filename used to rewrite using the new filename in a different directory
I think you would use rather this simpler rules set in /seo-images/.htaccess, we rewrite only if %{REQUEST_FILENAME} is neither a file nor a directory, not the inverse:
RewriteEngine On
#RewriteBase /seo-images/
# match all numbers upto - then get the extension after .
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]+)-.*\.([[:alnum:]]+)$ /uploads/$1.$2 [L]
Without redirect, we use only an internal rewrite, Google will always think about mywebsite.com/seo-images/123-some-description.jpg instead of mywebsite.com/uploads/123.jpg .
Or you prefer absolutely rewriting:
RewriteEngine On
#RewriteBase /seo-images/
# match all numbers upto - then get the extension after .
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]+)-.*\.([[:alnum:]]+)$ /uploads/$1.$2 [R=301,L]
It does this:
/seo-images/123-some-description.jpg => /uploads/123.jpg
/seo-images/t123-foo-bar.png => /uploads/t123.png

.htaccess for subfolder

I have the following structure
-www
\-subfolder
In www I have my main site's index.php.
In subfolder I have a sort of admin UI and in there I'd like to have another index.php for the admin UI.
Currently my requests from within /subfolder/index.php get redirected to www/index.php and basically the pages of my admin UI don't show up.
This is my .htaccess file:
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]
Can you help me? I've tried several options in other answers but as I'm no so advanced a web developer, I couldn't get any to work.
#TerryE, Sorry if I have come off as crude.
I am using a local setup for testing.
I have installed Vertrigo server, which gives me Apache server.Running on Windows7 OS. The server is installed in Program files\VertrigoServ\Apache folder.
My public folder is www. In there I have my main site definition. . The site is accessed locally via 127.0.0.1/index.php or 127.0.0.1/
I have site localization so URLs are constructed as /$lang/$page e.g. HOME
In index.php of main site I have the following:
$page = trim( ( isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : 'home' ), '/' );
$lang = trim( ( isset( $_GET[ 'lang' ] ) ? $_GET[ 'lang' ] : 'en' ), '/' );
$langs = array( 'en', 'fr', 'ru' );
And upon this data I get to open the pages this way:
include 'html/'. $lang . '/' . $page . '.php';
All my main site's pages lie in www/html/$lang/
$_SERVER['REQUEST_URI']) gives /en/home for page HOME.
127.0.0.1/en/home WORKS
All navigation works perfectly for the main site.
However I have created an admin UI which lies in folder www/admin - one level below in www.
And in there I don't have any localization. I just have EN as language.
So at the top of the index.php in admin folder I have again
$page = trim( ( isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : 'home' ), '/' );
However, here navigation is as follows HOME
and upon this I get to construct the pages in the index.php in admin folder as follows:
include 'html/ . $page . '.php';
the pages lie in www/admin/html
This does not work at all. Whenever I press home link in admin UI, I get redirected to my main site (non-existing page). If I add RewriteRule ^subfolder/ - [L] in .htaccess, I get HTTP 404 NOT Found error.
127.0.0.1/admin/home DOES NOT WORK. Neither does any other navigation from within admin. Thank you for your willingness and patience to help me!
I assume from this that you only have a single .htaccess file in your www directory.
Think about what the rule
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]
does when interpreted in a Perdir context from www: take any URI of the form someDir/somePage and replace it by index.php?lang=someDir&page=somePage, so this will intercept and rewrite any /subfolder/index.php.
This isn't well documented but Perdir processing will preferentially use the lowest .htaccess file setting RewriteEngine On on the request path. So if you add an admin-specific .htaccess file in the "subfolder" subfolder, this will preempt the www one and circumvent this problem.
Postscript comments
Veni and other posters get in a Q&A when the real issue is one of "how do I debug my .htaccess rules if I my website is hosted by a shared service?" The reason that I add the shared service qualification is that if you have root access to your LAMP config then you can turn on Rewrite logging and the logfile at a debug level of 4-6 will give you enough forensics to work out what is going on.
However, the large majority of hobby / small service users buy their services on a shared basis and here they don't have root access and the hosting provider disables such logging for performance reasons so they have a binary feedback -- the rules work or they don't. I use a shared service and my approach (described here) is to set up a VM which mirrors this configuration for as a test and integration environment -- and in this I have such root access. However, this is probably far too complicated for most users. This is really a wider topic that merits its own Q / discussion.
On specific points:
If your structure is /(lang|admin)/page, then why do you have this rule because it can cause havoc on perdir retries.
RewriteRule ^$ index.php [QSA,L]
Better something like the following to force a redirect to a default language (assume the lang list is EN and IT in this example:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^/(en|it|admin)/
RewriteRule ^(.*) http://yourdomain/en/$1 [R=301,L]
Are you aware of the redirect restart /looping issues? (search for REDIRECT_STATUS)
I'm new to Stackoverflow, but not to sorting out this sort of s**t. I've got a couple of detailed articles on my blog on this.
Postscript comments -- yet more
OK some general help.
Don't us fixed IPs put an alias for 127.0.0.1 in your windows\system32\drivers\etc\hosts
You can turn on rewrite logging in your VertrigoServ Apache config and this gives you a detailed audit of where you have problems
It also helps if you have a little diagnostic stub to help you understand what is going on and this is what I came up with for index.php in the test directories:
<?php header( "Content-type: text.plain" ); echo "main: "; var_export($_GET);
But you really need for each of the following cases:
- to handle the URI exists (and stop rewrite loops)
- the defaults for / and /admin/
- admin/*
- */*
- * (and the language defaults)
and this is what I came up with. I've kept it simple. We could use complex regexps to fold some of these but why bother. You may need to add more QSAs if needed.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^$ index.php [QSA,L]
RewriteRule ^admin/?$ admin/index.php [QSA,L]
RewriteRule ^admin/(.*)$ admin/index.php?page=$1 [L]
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]
RewriteRule ^(.*)$ index.php?lang=en&page=$1 [L]
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^subfolder/(.*)$ subfolder/index.php?page=$2 [L]
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]
You could prevent requests to you subfolder from being rewritten with the change below
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
#prevent requests to your subfolder from being rewritten
RewriteCond %{REQUEST_URI} !^/subfolder/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2 [L]